You are on page 1of 26

Microprocessor Course

LECTURE(03)

Addressing Modes
&
Data Movement Instructions
1
Addressing Modes

2
Chapter Contents

 Data-Addressing Modes

 Program Memory-Addressing Modes

 Stack Memory-Addressing Modes

3
Data-Addressing Modes
 General form of any assembly line
LABEL OPCODE OPERANDS ;COMMENT
DATA1 DB 23H ; Data1 is byte of 23h
START: MOV AL, BL ; Copy BL into AL

 Label (1-35 characters): Indicates a memory location


 Opcode: Indicates which operation to perform
 Operands (0-3): Indicates the data, comma separated, destination comes
first

 Data-addressing modes are the operand format (i.e., How to get the
data)

4
Register Addressing
 Data is within the program-visible registers
 Example:
MOV AX, BX ; Copy contents of BX to AX
 Not allowed forms:
 Segment to segment MOV
 MOV to CS
 Mixing different widths (like, MOV AL, BX)

5
Immediate Addressing
 The source is a constant representing the data itself
 Could be a byte, a word, and a doubleword
 Example:
MOV AL, 22H ; Copy the hex no. 22h to AL

 Some assemblers need the number sign (MOV AL, #22h)


 A hex number starting with a letter should be preceded with zero (MOV AH,
0F2H)
 For ASCII, use apostrophe (MOV BL, ’A’)

6
Immediate Addressing (Cont’d)
 Write an assembly program that uses immediate addressing to place 0 and 1
into AX and BX, respectively. Then, uses register addressing to copy AX to SI
and BX to DI

7
Immediate Addressing (Cont’d)
.MODEL TINY ; Single segment model
.8086 ; 8086 microprocessor
.CODE ; Start of code segment
.STARTUP ; Start of program
MOV AX, 0 ; Place 0 into AX
MOV BX, 1 ; Place 1 into BX
MOV SI, AX ; Copy contents of AX into SI
MOV DI, BX ; Copy contents of BX into DI
.EXIT ; Exit to DOS
END ; End of program

8
Memory Models
Memory Model Size of Code Size of Data

TINY Code + Data < 64KB Code + data < 64KB

SMALL Less than 64KB Less than 64KB

MEDIUM Can be more than 64KB Less than 64 KB

COMPACT Less than 64KB Can be more than 64KB

LARGE* Can be more than 64K Can be more than 64KB

HUGE** Can be more than 64K Can be more than 64KB


(*) For the LARGE model, the largest arrays size can not exceed 64 KB.
(**) For the HUGE model, an array may have a size greater than 64 KB and hence can span
more than one physical segment
9
Direct Addressing
 The data is in the memory.
 The memory address is indicated directly in the instruction
 Could transfer byte, word, or doubleword
 The offset of the memory address could be provided in an actual form,
like [1234h], or a symbolic form, like DATA1

 Example:
MOV DS:[1234h], CL ; copy CL to memory location DS:[1234]
MOV AX, DATA1 ; copy content of memory DATA1 to AX
 Effective address (EA) = Segment*10h + offset
 No memory-to-memory transfer
10
Direct Addressing (Cont’d)
 Write an assembly program that stores two hex numbers (Data1=10h and
Data2=20h) into data segment. Then, moves the first number to AL and the
second number to AH. Finally, saves AX back to data segment (to DATA3).

11
Direct Addressing (Cont’d)
.MODEL SMALL ; Single small code and data segments
.Data
DATA1 DB 10H ; Place 10h into memory location DATA1
DATA2 DB 20H ; Place 20h into memory location DATA2
DATA3 DW 0 ; Place 0 into memory location DATA3
.CODE ; Start of code segment
.STARTUP ; Start of program
MOV AL, DATA1 ; Copy DATA1 into AL
MOV AH, DATA2 ; Copy DATA2 into AH
MOV DATA3, AX ; Copy AX into DATA3
.EXIT ; Exit to DOS
END ; End of program
12
Register Indirect Addressing
 The data is in the memory.
 The memory address is within one of the processor registers
 Could transfer byte, word, or double word
 Base and index registers are usually used (For BP, default is SS, whereas
for any other register, default is DS)
 Example:
MOV AX, [BX] ; Copy content of memory location addressed by BX into AX

 Effective address
 EA = Segment*10h + content of the register

13
Base-plus-index Addressing
 The data is in the memory.
 The memory is addressed by Base + Index register
 Could transfer byte, word, or double word
 BX, BP are used as Base registers and SI, DI are used as Index registers
(For BP, default is SS, whereas for any other register, default is DS)
 Example:
MOV [BX+DI], CL ; Copy CL to memory location pointed to by BX + DI

14
Base-plus-index Addressing (Cont’d)
 Effective address
 EA = Segment*10h + Base + Index

 For Intel: MOV [BX][DI], CL

15
Register Relative Addressing
 The data is in the memory.
 The memory is addressed by a register (base or index) + displacement
 Could transfer byte, word, or double word
 Displacement could be positive or negative
 Example:
MOV AX, [BX+4] ; Copy memory location pointed to by BX + 4 to AX

 Effective address
 EA=Segment*10h + Base + Displacement

16
Program Memory-addressing Modes
 For JMP, CALL, and Branch instructions (addressing the code segment
not the data segment)
 Direct: Either using actual addresses or labels
JMP AGAIN

 Relative: Relative to IP
JMP [2]

 Indirect: Offset is within a register


JMP AX

 Double Indirect: Offset is in a memory location addressed by a register


JMP [BX]

17
Stack Memory-addressing Modes
 Data is stored into stack using PUSH and removed using POP
 CALL and RET indirectly store and remove from stack for procedure and
function call and return
 Stack is growing downwards

18
Data Movement Instructions

19
MOV Instruction
 For the MOV and all other instructions, the assembler automatically
generates the corresponding machine language.
 General form
LABEL MOV Destination, Source ;COMMENT
Label1: MOV AL, BL ; Copy BL into AL

20
MOV Instruction (Cont’d)
 Memory to memory MOV is not allowed

 Segment register to segment register MOV is not allowed

 MOV a register to CS is not allowed

 MOV immediate to CS is not allowed

21
Push/POP Instructions
 PUSH
 Stores data to the stack
 Operand to be pushed could be a register, memory location, or a segment
register
 POP
 Retrieves data from the stack
 Operand to pop into could be a register, memory location, or a segment
register (No POP to CS nor ES)
 General form
LABEL PUSH/POP Operand COMMENT
Label2: PUSH BX ; Store BX to the stack

22
Push/POP Instructions (Cont’d)
 Stack Pointer (SP)
 Ironically used to calculate the address to push to or pop from
 Automatically updated after PUSH or POP

23
Miscellaneous Data Transfer
 XCHG: Exchanges two registers or a register with a memory location.
 General form
LABEL XCHG OPRD1, OPRD2 COMMENT
Label5: XCHG AL, CL ; Exchange AL and CL

 No memory-to memory exchange


 No segment registers exchange

24
Miscellaneous Data Transfer (Cont’d)
 IN: Transfer data from an I/O device to the microprocessor.

 OUT: Transfer data from the microprocessor to an I/O device.

 AL, AX, and EAX should only be used for the transfer

 General form
LABEL IN AL/AX/EAX, PORT COMMENT
LABEL OUT PORT, AL/AX/EAX COMMENT
Label6: IN AX, 61h ; Input a word from port 61h to AX
Label7: OUT DX, AL ; Output a byte from AL to the port DX

25
26

You might also like