Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Assembly Language
Addressing Modes
1. Immediate Addressing
Value to be stored in memory immediately follows the operation code in memory Or The instruction itself dictates the value to be stored in memory Example MOV A, #20h Accumulator will be loaded with the value that immediately follows; in this case 20h
1. Immediate Addressing
It is very fast Value to be loaded is included in instruction The value to be loaded is fixed at compile time its not very flexible
1. Immediate Addressing
Examples
Important
MOV A, #FH
; gives error
2 . Direct Addressing
Value to be stored in memory is obtained by directly retrieving it from another memory location
Example MOV A, 30H
The above instruction will read data from RAM address 30H and put that data in A.
10
2 . Direct Addressing
Generally fast, although the value to be stored is not included in the instruction
Its quickly accessible since value is stored in 8051s Internal RAM Much more flexible than immediate addressing. Value to be loaded is whatever it is found at that location which may be variable Entire 128 bytes of RAM (0 to FF) can be accessed
11
2 . Direct Addressing
Examples
12
3. Indirect Addressing
Also called indirect register addressing Very powerful and flexible addressing
Example
MOV A, @R0
This instruction analyzes the value of R0. A will be loaded with value from RAM whose address is found in R0 For example if R0 holds value 40H and RAM address 40h holds value of 67H. Then A will have value of 67H
13
3. Indirect Addressing
14
3. Indirect Addressing
This mode makes data access dynamic instead of static (as in case of direct addressing)
Only R0 and R1 can be used Since these registers are 8 bit, use is restricted to internal RAM only
15
4. Register Addressing
In register addressing mode, we directly use the value stored in register 8051 has 8 working registers (of the currently active register bank) R0 to R7 Examples MOV A, R7 MOV A, R5 Contents of R7 and R5 will be moved in above instructions
16
4. Register Addressing
17
18
6. Absolute Addressing
Used only with ACALL and AJMP instruction It allows branching within 2K limit (11 bit)
19
7. Long Addressing
Used only with LCALL and LJMP instruction Allows branching within 64K limit (16 bit)
20
8. Index Addressing
Used with JMP and MOVC instruction Use base register (either PC or DPTR) and an offset (the accumulator) in forming the effective address
MOVC A, @A + DPTR JMP @A + DPTR It is used for look up table
Previously we studied how its stored Well cover its usage later on
21
Instruction Types
22
Types of Instructions
1. 2. 3. 4. 5.
23
MOV Summary
MOV [Destination],[Source]
Examples
MOV A,#55H ; A will be loaded with 55H MOV R2, A ; Load value of A in R2
24
1. Arithmetic Instructions
25
Example
26
High byte and Low Byte are added separately using 2 instructions If there is a Carry out from addition of low bytes, it has to be added in sum of high bytes
27
28
Note: There is no subtraction instruction without borrow in 8051 (many microprocessors have it)
30
31
15h Carry = 0
33
14h Carry = 0
35
F0h = 1111 0000. Taking 2s complement (by inverting and adding 1) = 0000 1111 + 1 = 0001 0000 = 10h Carry = 1. Showing it is a negative number
37
38
39
A = 40h = 64 B=0
40
41
42
A = 4h B=0
43
44
45
Lower 4 Bits represent decimal number (0 to 9) Remaining bits are zero Example: 9 will be represented as 0000 1001 1 byte can represent 1 number only
Packed BCD
46
Each 4 bits represent a decimal number (0 to 9) 1 byte can store more 2 digits Example: 79 will be represented as 0111 1001 Utilizes space more efficiently than unpacked BCD
1. Arithmetic Instructions
Important
Play with arithmetic instructions and see the results Good to revise Binary Arithmetic first
49
2. Logical Instructions
Left Right
SWAP
50
51
52
53
2. Logical Instructions - OR
OR Format
ORL destination, source
54
2. Logical Instructions - OR
OR Example
mov a,#74h orl a,#0Fh
55
2. Logical Instructions - OR
OR Example
mov a,#74h orl a,#0Fh
56
57
58
59
Result operand = 0
The operand may be Accumulator, carry flag (C), any IO Port Bit etc
60
61
The operand may be Accumulator, carry flag (C), any IO Port Bit etc
62
63
64
65
66
67
68
69
70
71
72
3. Data Transfer
MOV
Addressing Modes
(Already Covered)
73
75
77
Stack Pointer (SP) stores the address of current stack location. At power up, SP = 07
RAM locations 08h to 1Fh can be used for stack RAM locations 20h to 2Fh are bit-addressable locations More space if SP is set to point to 30h to 7Fh i.e. general purpose RAM
79
expression must be
Number of a register
Example ; Push contents of R3 ; Push contents of R1 ; Push value stored at memory location 55h
81
expression must be
Number of a register
Retrieves the value the last value stored by PUSH Automatically decrements the SP
82
mov a,75h
84
CLR
CLR bit Clears the bit i.e. makes the bit = 0 Examples
SETB
SETB bit Sets the bit i.e. makes the bit = 1 Examples
85
ANL
anl c,P1^7
ORL
orl c,P3^5
CPL
CPL P0^0
86
5. Program Branching
Conditional
87
RETI
88
fn2:
89
All required registers are stored before calling SR and retrieved after the SR has finished
90
PSW A B R0 R7
PUSH them in stack before calling SR POP them from stack after returning from SR
91
expression2 may be
Number Address
96
mov a,#01h
main:
Jmp $
R0 to R7
First it decrements the value of register and then checks if the value is zero or not
98
100
Example
Expression may be
102
jmp $
Example
Mov a,#0Fh Label: DEC A mov p0, a JB P0^3, Label What is this program doing?
104
Example
Mov a,#01h Label: INC A mov P0,a JNB P0^3, Label What is this program doing?
105
JNC Label
106