Title: ECE 291
1ECE 291
Lecture 7 More on Addressing Modes, Structures,
and Stack Constantine D. Polychronopoulos Profess
or, ECE Office 463 CSRL
Spring 2000
2Defining Constant Data Structures
Label OPCODE OPERAND(S) Comment
s DATA1 DB 23H define DATA1
as a byte of 23H DATA2 DW 1000H
define DATA2 as a word of 1000H ARRAY DB 17
DUP (?) define array of 17 byte-sized
entries LIST1 DW 10 DUP (?) define
array LIST1 of 10 word el. LIST2 DW 1,
2, 3, 4, 5 define array LIST2 of 10
elements DW 6, 7, 8, 9, 10 same as
LIST1 with initialized data
3Important Rules to Remember
- MOVS is the only instruction that allows
memory-to-memory data moves (all other MOV insts
use at least one register) - MOV instructions never affect the FLAG register
- Never use CS as a destination operand in a MOV -
you cannot write into the code segment! - MOV ES, DS is illegal - (segment-to-segment)
- MOV BL, DX is illegal - different sized data
moves - Immediate addressing
- MOV CX, 0 places 0000H into CX - same as MOV
CX,0000H - MOV CX, 200 places 200 decimal into CX
- MOV CL, 11001110B places a 11001110 binary
into CL - MOV AX, AB places ASCII BA into AX
4Important Rules to Remember (cont.)
- Direct addressing transfers data from/to memory
to/from AL, AX, or EAX - In MOV AL, 1234H, the direct address 1234H is
added to DS as a displacement to form mem.
address. Same instruction can be written as MOV
AL, DS1234H - For any addressing mode that uses BX, DI, SI to
address memory the data segment is used by
default (whose base is in DS). In 32-bit, this is
the case for EBX, EDI, ESI, as well as EAX, ECX,
EDX. - If the BP or EBP register is used to access
memory, the stack segment is used by default. - The OFFSET directive is used to get address of
an object - not contents - MOV BX, TABLE loads BX with contents of mem
loc. TABLE - MOV BX, OFFSET TABLE copies the offset address
of TABLE into BX
5Ambiguous MOVs
- MOV AL, DI is an unambiguous move of a byte
from memory location DSDI to AL - MOV DI, 10H is an ambiguous move since its not
clear whether it addresses a byte-size, word-size
or double mem location - the assembler cannot
determine size of 10H! - Solution Use the assembler directives
- BYTE PTR, WORD PTR, or DWORD PTR
- These directives are used only with instructions
that address memory through a pointer or index
register with immediate data! - Other legal MOV
- MOV EAX 2EDI 100H, CX (16-bit move)
- MOV AL, EBP 2EDI - 2 (8-bit move)
- MOV EAX, ARRAY4ECX (32-bit move)
6Example
- Suppose the processor clock is automatically
saved in offset 046CH of segment 0000. We will
write a program that reads the byte-size clock 50
times in succession and stores the clock values
in a user-defined array CARRAY.
7Example Program - code
Label Opcode Operand Comments .MODEL
SMALL .DATA start of DATA
segment CARRAY DW 50 DUP (?)
setup array of 50 bytes .CODE start of
CODE segment .STARTUP start of program
MOV AX, 0 set AX to 0 MOV ES,
AX address segment 0000 with ES MOV BX,
OFFSET CARRAY address CARRAY with BX
MOV CX, 50 load counter CX with 50 AGAIN
MOV AX, ES046CH get clock value
MOV BX, AX save clock value in CARRAY
INC BX increment BX to next element
LOOP AGAIN loop 50 times .EXIT exit
to DOS END end file
8Data Structures
Label Opcode Operand Comments CLIENT
STRUC
NAMES DB 32 DUP (?) 32 bytes of name
STREET DB 32 DUP (?) 32 bytes of street
CITY DB 16 DUP (?) 16 bytes of city
STATE DB 2 DUP (?) 2 bytes of state
ZIP DB 5 DUP (?) 5 bytes of zip code
CLIENT ENDS
9Operations on Structures
- Use ltstruct_name.fieldgt to address fields in
structures Example Clear street in array NAME1
MOV CX, 32 MOV AL, 0 MOV SI, OFFSET
NAME1.STREET REP STOSB
Where STOSB ESDI AL DIDI /-1 REP
causes CX to decrement till 0 and it repeats
the instruction it prefixes as many times
10STACK
- A very important memory region organized as a
LIFO structure and used extensively for storing
local data parameters as well as for recursion.
- Stack is addressed by SS and SP (ESP) and
manipulated through the PUSH (store) and POP
(get) instructions. - PUSH ltwordgt the high-order 8-bits are placed in
location SP-1 the low-order 8-bits are placed in
SP-2 and SP is decremented by 2 so that next
word is stored in the next stack location. - POP ltwordgt the low-order 8-bits are removed
from location SP the high-order 8-bits are
fetched from SP1 SP is incremented by 2 to
point to the next element at the top of the
stack.
11Stack Operations
POPF removes word from stack and places it
into FLAGS POPFD removes doubleword into
EFLAGS PUSHF copies the FLAGS into the
stack PUSH AX copies AX into the
stack POP BX loads BX with the top of the
stack PUSH DS copies DX onto the stack PUSH
1234H pushes constant 1234H onto the
stack POP CS ILLEGAL PUSH WORD PTRBX copies
word from data segment addressed by BX
PUSHA copies word contents of AX,CX, DX, BX,
SP, BP,DI and SI POPA removes opposite into
SI, DI, BP,SP,BX,DX,CX, and AX PUSHAD same as
PUSHA for doublewords POPAD same as POPA for
doublewords POP EAX removes dword from stack
into EAX PUSH EDI copies EDI onto the stack