You are on page 1of 20

ELE 3230

Microprocessors and Computer


Systems

Part 5
Data Addressing Mode
(Brey: Ch3; Hall: pp30-35, 49-50, 89-90;
Triebel: pp52-69; Also check “addressing
mode in Index)
ELEG 3230 - Part 5 1
Outline
aIntroduces various data addressing modes of the
8088 for memory retrieval and storage.
`Register addressing mode,
`Immediate addressing mode,
`Direct addressing mode,
`register indirect addressing mode,
`Based addressing mode,
`Indexed addressing mode,
`Based indexed addressing mode,
`String addressing mode, and
`Port addressing mode.

ELEG 3230 - Part 5 2


Data Addressing Modes of 8088
a Instructions can have their operands (ie the data on which the
instruction operates) stored in memory, in registers or in an I/O port.
Format of Label Nemonic/Directive Operands Comment
Instruction: FRED: ADD AL,0FH ; Add 0FH to register AL

a The addressing mode indicates where the operands are located.


a The Intel 8088 has about 9 data addressing modes: register,
immediate, direct, register indirect, based, indexed, based indexed,
string, and port addressing. (definitions in different books may vary)
a Only register and immediate addressing modes do not need to access
the external data bus to read (write) from (to) memory or IO space.
a The assembler decodes which addressing mode is being used from
the syntax of the operand field.

Ref: 1. Ch3-1 of Brey’s


2. Check Hall’s pp33-35 and MOV instruction starting pp48
ELEG 3230 - Part 5 3
1. Register Addressing Mode
a Instruction gets its source data from a register.
a Data can be either 8 or 16 bits in length
a Data resulting from the operation is stored in another register.

Examples:
Destination Source
MOV AX, BX ; copy the 16-bit content of BX to AX
MOV AL, BL ; copy the 8-bit content of BL to AL
MOV SI, DI ; copy DI into SI
MOV DS, AX ; copy AX into DS

Note: The following register to register transfers are not permitted:


MOV BL, BX ; Not permitted (mixed sizes)
MOV CS, AX ; Not permitted (CS cannot be the destination) (?)
MOV ES, DS ; Not allowed (segment register to segment register forbidden)
ELEG 3230 - Part 5 4
2. Immediate Addressing Mode
MOV AL, 0Ah ; load 0Ah into AL

a Immediate data is coded directly in the instruction’s machine code.


a The data is put in the operand field as a content.
a The constant in the operand field may be of byte or word length.
a Some assemblers need a “#” symbol before the constant (Not needed in
MASM nor Turbo Assembler). The constant may be in hexadecimal,
decimal, binary or even text. The default format is decimal.

` Hexadecimal numbers are usually denoted by appending an H (or h) and


preceded by 0 if they start with a letter e.g. 0Fh, 0A8h, 0F34Bh
` Binary numbers are usually denoted by appending a B (or b).
` Text characters (their ASCII code) are enclosed by apostrophe(‘).

ELEG 3230 - Part 5 5


2. Immediate Addressing Mode (cont.)

Valid Examples:
MOV AL, 0Ah ; load 0Ah into AL
MOV BX, 1000h ; load 1000h into BX
MOV CL, ‘A’ ; load the ASCII code for ‘A’ (65) into CL

Invalid example:
MOV AL, 2AAh ; U
NOT allowed as the data exceeds the length of the destination register

ELEG 3230 - Part 5 6


3. Direct Addressing Mode
MOV [1234h], AL ; copies AL to 11234h; Assume DS=1000h

a The operand is stored in a MEMORY location, usually in the Data Segment.


a A 16-bit offset address is coded directly in the instruction. The offset and DS
(DS: Data Segment Register in CPU) form the 20-bit address where the
operand is located. The 20-bit address is calculated by multiplying DS by 16
(or equivalently appending a 0) and adding to the offset.
a Usually the 16-bit offset used in direct addressing is written as a label in an
assembly language program. Or the offset is enclosed in [ ] to indicate that it is
an address (not immediate data).
a The default segment register is assumed to be the DS unless explicitly over-
ridden using a colon, e.g. ES:[0001h].

ELEG 3230 - Part 5 7


3. Direct Addressing Mode (cont.)

Examples: (Assume DS=1000h, ES=2000h, FRED=4567h)

MOV AX, [20h] ; load the contents at address 10020h and 10021h
; into AL and AH, respectively
MOV [1234h], AL ; copy AL to address 11234h
MOV ES:[1234h], AL ; copy AL to address 21234h
MOV FRED, AL ; copy AL to address14567h (offset is calculated by the
; assembler)

ELEG 3230 - Part 5 8


4. Register Indirect Addressing Mode
MOV CX, [BX] ; copies a word from memory with location specified by BX
a Register Indirect Addressing uses a register instead of a constant (as in direct
addressing) to specify the 16-bit offset address of the operand.
a The offset address of which the data is located may be in any of the following
registers: BP, BX, DI, SI. The [ ] is needed to denote register indirect
addressing mode.
a The DS register is the default segment address register (except BP, which
uses SS as the default register).
a In cases of ambiguity, assemblers need the presence of the BYTE PTR or
WORD PTR directives to indicate the size of the data address by the memory
pointer.
e.g. MOV [DI], 10H is ambiguous since the assembler does not know whether to
save 10H to memory as a byte or a word. Instead, we shall use
MOV BYTE PTR [DI], 10h ; save 10h to memory
MOV WORD PTR [DI], 10h ; save 0010h to memory
ELEG 3230 - Part 5 9
4. Register Indirect Addressing Mode
(cont.)
a Registers indirect addressing is commonly used to access a table of data
in memory.

Examples (with BX=0222h, DS=1000h, SS=2000h, BP=0111h)

MOV CX, [BX] ; copy a word from address 10222h and 10223h to CX
MOV [BP], DL ; copy a byte from register DL to address 20111h

Note : BP defaults to SS

ELEG 3230 - Part 5 10


5. Based Addressing Mode
a The operand is located at the address given by (i) adding an 8- or 16-bit
displacement to either BX or BP and (ii) combining the result with a segment
register.
a The 8- or 16-bit displacement must be specified in the operand field and is
interpreted as a signed 2’s complement value. For the 8-bit case, the
displacement must be in the range -128 to +127; and for the 16-bit case, the
displacement must be in the range -32768 to 32767.

Examples (with DS=1000h, SS=2000h, BP=0222h, BX=0111h)


MOV AX, [BP-2] ; copy the content of 20220h and 20221h to AX
MOV [BX+777h], AX ; copy AL to 10888h and AH to 10889h

ELEG 3230 - Part 5 11


6. Indexed Addressing Mode

a Very similar to based addressing except that the index registers (SI or DI)
must be used instead.
a The operand is located at the address given by adding a signed 8- or 16-
bit displacement to either SI or DI and combining the result with a
segment register (DS by default).

Example (with DS=1000h, SI=222h, DI=111h)


MOV [DI-1], BL ; store the content of BL to 10110h
MOV BX, [SI+1000h] ; load BL with the contents of 11222h and
BH with the contents of 11223h

ELEG 3230 - Part 5 12


6. Indexed Addressing (cont.)

a Based addressing and Indexed addressing are also known as REGISTER


RELATIVE addressing as defined in Brey’s.
a Other syntax may be permitted to indicate the displacement.
a Note: The following examples are all equivalent. And they all result in the
same assembled binary code. (Assume FRED is a label which is
assigned by the assembler a constant value -- an address.)

Examples:
MOV [DI+FRED], BL ; This syntax is used above
MOV [DI]+FRED, BL
MOV FRED[DI], BL

ELEG 3230 - Part 5 13


7. Based Index Addressing (with
displacement)
a The base and index registers are added to give the segment offset of
where the operand is located.
a The base register (either BX or BP) is added to an index register (either
DI or SI) as POSITIVE integers only (each register lies in the range 0 to
65535). Q: what if the resultant offset >65535?
a By default, the segment address is derived from DS, except the BP
register, which is derived from SS.
a A signed displacement may also be included to calculate the offset.
Example (assumes SS=1000h, SI=3333h, BP=2222h)
MOV AX, [SI+BP] ; load the content of 15555h and 15556h to
; AL and AH respectively
MOV AX, [SI+BP+1111h]
; load the contents of 16666H and
; 16667h to AL and AH respectively
Note: This addressing mode includes the base-plus-index and base-relative-plus-
index addressing modes defined in Brey’s.
ELEG 3230 - Part 5 14
8. String Addressing Mode
a A string is a series of bytes or a series of words in sequential memory
locations.
a String instructions do not use any of the previous address modes.
a Strings may be up to 64K-byte in length.
a String addressing mode uses SI, DI, DS and ES registers.
a All the string instruction assume that SI points to the first byte of the string
to be processed and that DI points to the first byte of the destination
string.
a The use of register may be implicit in the instruction.

Example: (with DS=1000h, ES=2000h, SI=10h, DI=20h)


MOVSB ; Move string byte from 10010h to 20020h

Note: see string instruction in later chapters for more details.

ELEG 3230 - Part 5 15


9. Port Addressing Mode
a The Intel 8088 has separate memory and input/output space.
a Up to 65536 I/O ports are available
a The I/O ports may be addressed by a byte sized constant (limited to I/O
ports in the range 0 to 255)
Example:
IN AL, 40h ; put the content of I/O port 40H into AL
OUT 80h, AL ; send the contents of AL to I/O port 80h
a The I/O ports may be addressed using the register DX (full range of
65536 ports are accessible)
Examples:
IN AL, DX ; Load AL with the byte from port address given by DX
OUT DX, AX ; Send the word in AX to port address given by DX

Q: Is the instruction “MOV AL, DX;” valid?

ELEG 3230 - Part 5 16


Summary of Addressing Modes
Operand needed for an instruction may be located :
a immediately in the operand field. e.g. MOV AX, 1234h
a in a register (register addressing). e.g. MOV DS, AX;
a in memory at a specified offset. The offset may be specified by one of the
following (displacement is a constant or label expression):
‹ [displacement]
‹ [BP] ‹[BP+displacement]
‹ [BX] ‹[BX+displacement]
‹ [SI] ‹[SI+displacement]
See Brey’s Table 3-12
‹ [DI] ‹[DI+displacement]
for more examples
‹ [BX+SI] ‹[BX+SI+displacement]
‹ [BX+DI] ‹[BX+DI+displacement]
‹ [BP+SI] ‹[BP+SI+displacement]
‹ [BP+DI] ‹[BP+DI+displacement]
• The segment address is in DS (by default, except when BP is used)
a in memory locations given implicitly by string instructions.
a at input/output ports specified by a register or a constant.
ELEG 3230 - Part 5 17
Rules of combination of
Segment registers and Offset
a The microprocessor has a set of rules that define the segment register
and offset register combination used by certain addressing modes.
a However, the default can be overridden by using the segment override
prefix e.g. MOV CL, [BP], MOV CL,DS:[BP]

Default
Offset register Override Prefix
Segment register
IP CS Never
SP SS Never
BP SS DS, ES or CS
SI, DI (for non-string DS ES, SS or CS
instruction)
DI for string instructions ES Never

ELEG 3230 - Part 5 18


Other Addressing Modes
a Programming memory-addressing mode states how to modify the flow of
the program using Jump and Call instruction.
a See Jump and Call instruction for more information.

Example:
JMP CX ; Jump to the current code segment location addressed by
; the content of CX.

a Stack addressing mode uses the PUSH and POP instruction to transfer
data between the stack memory and registers or direct data.

Example:
POPF ; Remove a word from stack and place it into the flag
PUSH DS ; Copy DS to the stack
PUSH 1234h ; Copy a 1234h to the stack
ELEG 3230 - Part 5 19
Pros and Cons of Different Data
Addressing Modes
Principal Principal Example
Mode Algorithm Advantage Disadvantage
Register EA = R No memory Limited address
MOV AX,BX
reference space
Immediate Operand = A No memory Limited operand MOV CH, 3Ah
reference magnitude
Direct EA = A Simple Less flexible MOV [123h], AX
Register EA = (R) Large address Extra memory
MOV [BX], CL
indirect space reference
Base-Index EA = A + (R) Flexibility Complexity
MOV [BX+SI+30h], DX
Relative
Stack EA = top of No memory Limited POP DX
stack reference applicability

EA: effective address


R: Register
A: a constant
ELEG 3230 - Part 5 20

You might also like