8051 Programming PDF
8051 Programming PDF
Overview
mov R0, a
mov a, R0
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 ; use register bank 0
mov r0, #0x3C
mov @r0, #3 ; memory at 3C gets #3
; M[3C] 3
Uses DPTR register for 16-bit addresses:
mov dptr, #0x9000 ; dptr 9000h
mov a, @dptr ; a M[9000]
• B – address 0F0h
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
push a
Stacks
pop
push
stack pointer
stack
Address Modes
Exchange Instructions – two way data transfer
XCH a, 0x30 ; a M[30]
XCH a, R0 ; a R0
XCH a, @R0 ; a M[R0]
XCHD a, R0 ; exchange “digit”
Example:
SUBB A, #0x4F ; A A – 4F – C
mov a, r2
add a, #1 ; use add rather than increment to affect C
mov r2, a
mov a, r3
addc a, #0 ; add C to most significant byte
mov r3, a
Multiply
When multiplying two 8-bit numbers, the size
of the maximum product is 16-bits
FF x FF = FE01
(255 x 255 = 65025)
MUL AB ; BA A * B
Integer Division
DIV AB ; divide A by B
A Quotient(A/B), B Remainder(A/B)
Example:
mov a, #0x23
mov b, #0x29
add a, b ; a 23 + 29 = 4C (wanted 52)
DA a ; a a + 6 = 52
XRL – eXclusive OR
00001111
CPL – Complement ORL 10101100
10101111
00001111
XRL 10101100
10100011
CPL 10101100
01010011
Address Modes with Logic
ANL – AND a, byte
ORL – OR direct, reg. indirect, reg, immediate
XRL – eXclusive oR
byte, a
direct
byte, #constant
rl a
C
rrc a
mov a, #0A9h ; a A9
add a, #14h ; a BD (10111101), C0
rrc a ; a 01011110, C1
Swap
swap a
mov a, #72h
swap a ; a 27h
Bit Logic Operations
Some logic operations can be used with single bit
operands
ANL C, bit ANL C, /bit
ORL C, bit ORL C, /bit
CLR C
CLR bit
CPL C “bit” can be any of the bit-addressable RAM
locations or SFRs.
CPL bit
SETB C
SETB bit
Rotate and Multiplication/Division
clr c
rl a ;multiply by 2
inc a ;and add one
Program Flow Control
• Unconditional jumps (“go to”)
• Conditional jumps
• Call and return
Unconditional Jumps
• SJMP <rel addr> ; Short jump, relative
address is 8-bit 2’s complement number, so jump can be
up to 127 locations forward, or 128 locations back.
• LJMP <address 16> ; Long jump
• AJMP <address 11> ; Absolute jump to
anywhere within 2K block of program memory
• JMP @A + DPTR ; Long indexed jump
Infinite Loops
loop: mov a, P1
jz loop ; if a=0, goto loop,
; else goto next
; instruction
mov b, a
Conditional jumps
Mnemonic Description
JZ <rel addr> Jump if a = 0
JNZ <rel addr> Jump if a != 0
JC <rel addr> Jump if C = 1
JNC <rel addr> Jump if C != 1
JB <bit>, <rel addr> Jump if bit = 1
JNB <bit>,<rel addr> Jump if bit != 1
JBC <bit>, <rel addr> Jump if bit =1, clear bit
CJNE A, direct, <rel Compare A and memory,
addr> jump if not equal
Conditional Jumps for Branching
if condition is true condition
false
goto label
true
else
label
goto next instruction
jz led_off
setb C
if a = 0 is true mov P1.6, C
send a 0 to LED sjmp skipover
else led_off: clr C
mov P1.6, C
send a 1 to LED skipover: mov A, P0
More Conditional Jumps
Mnemonic Description
CJNE A, #data <rel addr> Compare A and data, jump if
not equal
CJNE Rn, #data <rel addr> Compare Rn and data, jump if
not equal
CJNE @Rn, #data <rel addr> Compare Rn and memory,
jump if not equal
DJNZ Rn, <rel addr> Decrement Rn and then jump
if not zero
DJNZ direct, <rel addr> Decrement memory and then
jump if not zero
Iterative Loops
For A = 0 to 4 do For A = 4 to 0 do
{…} {…}
Absolute call
acall <address ll> ; stack PC
; PC address 11
Long call
lcall <address 16> ; stack PC
; PC address 16
Return
• Return is also similar to a jump, but
– Return instruction pops PC from stack to get
address to jump to
ret ; PC stack
Subroutines
call to the subroutine
Main: ...
acall sublabel
...
...
sublabel:...
... the subroutine
ret
Initializing Stack Pointer
• The Stack Pointer (SP) is initialized to 0x07. (Same
address as R7)
• When using subroutines, the stack will be used to store the
PC, so it is very important to initialize the stack pointer.
Location 2F is often used.
External ADC 1
Clock
source Serial Timer 4
Valid (UART) 1 ADC 0
External 7
External 6 Timer 3
External Interrupts
• /INT0 (Interrupt 0) and /INT1 (Interrupt 1)
are external input pins.
• Interrupt 6 and Interrupt 7 use Port 3 pins 6
and 7:
INT 6 = P3.6
INT 7 = P3.7
These interrupts can be configured to be
– rising edge-triggered
– falling edge-triggered
External Interrupts