Professional Documents
Culture Documents
Assembly Language
8051
This is an 8 bit microcontroller originally developed by Intel in 1980 It is the world's most popular microcontroller core, made by many independent manufacturers (truly multisourced) There were 126 million 8051s (and variants) shipped in 1993!!
2
8051 Summary
A typical 8051 contains: CPU with Boolean processor 5 or 6 interrupts: 2 are external 2 priority levels 2 or 3 16-bit timer/counters programmable full-duplex serial port (baud rate provided by one of the timers) 32 I/O lines (four 8-bit ports) RAM - ROM/EPROM in some models
3
Memory Organization
Separate data & program memory On-chip program memory (8K on-chip for 8052) On-chip data memory (256 bytes of internal RAM for 8052) Off-chip program memory (up to 64K) Off-chip data memory (up to 64K) On-chip special function registers
6
Register - Accumulator
8051 is an accumulator based Microcontroller It is an 8-bit register Accumulator can be the source or destination for logical operations and a number of special data movements Several functions apply exclusively to the accumulator(rotates,testing for zero etc.)
7
Register DPTR
16-bit register namely data pointer Serves as a base register in
Indirect jumps Table look up instructions External data transfers
SP content indicates the address of the last byte pushed on to the stack Stack grows up from 08h onwards
9
Register - PSW
It is a register with a group of status flags C-carry AC-auxiliary carry F0-flag0 RS1 & RS0 OV-overflow P-parity
10
Addressing Modes
Immediate Mode specify data by its value
Data is contained in the instruction eg:MOV A,#n
mov a, #0 mov a, #11 mov a, #77h mov a, #11111111b ;put 0 in the accumulator a = 00000000 ; put 11 decimal in accumulator a = 00001011 ; put 77 hex in accumulator a = 01110111 ; put 11111111 binary in accumulator a = 11111111
11
Addressing Modes
Direct Mode specify data by its 8-bit address
mov a, 70h Mov D0h, a ; copy contents of RAM at 70h to a ; put contents of a into PSW
12
Addressing Modes
Register Addressing either source or destination is one of R0-R7
mov R0, a mov a, R0
13
Addressing Modes
Register Indirect the address of the source or destination is specified in registers Uses registers R0 or R1 for 8-bit address:
mov 0xD0, #0 mov r0, #0x3C mov @r0, #3 ; use register bank 0 ; memory at 3C gets #3 ; M[3C] 3 ; dptr 9000h ; a M[9000]
Addressing Modes
Stack-oriented data transfer another form of register indirect addressing, but using SP
mov sp, #0x40 push 0x55 pop b ; ; ; ; Initialize SP SP SP+1, M[SP] M[41] M[55] b M[55]
M[55]
Note: can only specify RAM or SFRs (direct mode) to push or pop. Therefore, to push/pop the accumulator, must use acc, not a: push acc - is correct push a - is wrong
15
@Ri - 8 bit internal RAM location addressed indirectly through registers R1 or R0 #data - 8 bit constant included in the instruction #data16 - 16 bit constant included in the instruction
16
17
Instruction set
Arithmetic operations ADD A,Rn ADD A,direct ADD A,@Ri ADD A,#data ADDC ADDC ADDC ADDC A,Rn A,direct A,@Ri A,#data
18
20
Decimal Adjust
DA a ; decimal adjust a Used to facilitate BCD addition. Adds 6 to either high or low nibble after an addition to create a valid BCD number. Example: mov a, #0x23 mov b, #0x29 add a, b ; a DA a ; a
23 + 29 = 4C (wanted 52) a + 6 = 52
21
22
24
Rotate
Rotate instructions operate only on a rl a mov a, #0xF0 rl a ; a ; a 11110000 11100001
25
; a ; a ; a
A9 BD (10111101), C 0 01011110, C 1
26
Swap
swap a
; a
27h
27
dest source
;move byte to accumulator ;move accumulator to byte ;move byte to register of ;current bank ;move byte to internal RAM ;move byte to internal RAM ;with address contained in Rn ;move 16-bit data into data ;pointer
28
a[7..4] a[3..0]
33
34
Assembler directives
Supply data to the program and control assembly process
Assemble code & data to specified sections Reserve space in memory for uninitialized variables Control the appearance of listings Initialize memory Assemble conditional blocks Specify libraries from which assembler can obtain macros Examine symbolic debugging information
37
Directives cntd..
For storage control
ORG END DB DW value [value] value value
38
Directives cntd..
Listing control
LIST ON LIST LIST OFF NOLIST NLIST
Others
EQU INCLUDE value filename
39
EQU Used to create symbols that can be used to represent registers, numbers, and addresses
LIMIT VALUE SERIAL COUNT MY_VAL EQU EQU EQU EQU EQU 2000 LIMIT 200 + 'A' SBUF R5 0x44 Registers, numbers, addresses
40
System Defaults
Default file extensions
asm obj lst lib tsk hex s19 - input to the assembler - output from the assembler - listing file - library file - executable object code - Intel hex - Motorola s19
41
System Defaults
Number base designation
Binary - B Octal - O or Q Decimal - D or no base designation Hex -H ASCII - Single or double quotes X or X
42
Example program
org start: cpl call sjmp sjmp mov mov djnz djnz ret 8000h p1.7 delay start sleep r0,#0ffh r1,#0ffh r1,loop1 r0,loop2
; my code originates from 8000h the ; first External RAM address
; complement the Port1 7th Line which connected to LED ; call a subroutine to generate delay between Complements ; repeat the process ; if the program loop is over (never for this case) then wait in infinite loop ; my delay subroutine starts with var1 = 255 ; var2 = 255 ; var2 = var2 -1 , if var2 != 0 then loop ; if var2==0 then var1 = var1-1 and check if var1 != 0, if not reload var2 = 255 ; if a time of approximately (255 * 255 * 2) + 4 Clock cycles elapsed ; then you go back to main ; for a 89C52 with 11.0592 MHz Clock will generate a delay of approx 140 milliseconds ; delay = 1 / (11.0592 MHz /12 ) * ((255 * 255 * 2) + 4) = 1.085 Micro Seconds * 130054 = 140 ms
end
43
I request Electronics and communication ENGINEERING students to visit my blog for more abhishek1ek.blogspot.com and awhengineering.blogspot.com
44
45