110 likes | 231 Views
ECE 291. Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE Office: 463 CSRL. Spring 2000. Defining Constant Data & Structures. Label OPCODE OPERAND(S) Comments
E N D
ECE 291 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE Office: 463 CSRL Spring 2000 ECE 291 -- Spring 2000
Defining Constant Data & Structures Label OPCODE OPERAND(S) Comments 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 ECE 291 -- Spring 2000
Important 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 ECE 291 -- Spring 2000
Important 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, DS:[1234H] • 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 ECE 291 -- Spring 2000
Ambiguous MOVs • MOV AL, [DI] is an unambiguous move of a byte from memory location DS:DI 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 + 2*EDI + 100H], CX (16-bit move) • MOV AL, [EBP + 2*EDI - 2] (8-bit move) • MOV EAX, ARRAY[4*ECX] (32-bit move) ECE 291 -- Spring 2000
Example: • 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. ECE 291 -- Spring 2000
Example 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, ES:[046CH] ;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 ECE 291 -- Spring 2000
Declaring & initializing new structures: NAME1CLIENT<’John Smith’, ’123 Main Street’, ’Toledo’, ’OH’, ’77777’> Data 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 ECE 291 -- Spring 2000
Operations on Structures - Use <struct_name.field> 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 ; ES:[DI] = AL; DI=DI +/-1 REP ; causes CX to decrement till 0 and it repeats the ; instruction it prefixes as many times ECE 291 -- Spring 2000
STACK • 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 <word> : 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 <word> : the low-order 8-bits are removed from location SP; the high-order 8-bits are fetched from SP+1; SP is incremented by 2 to point to the next element at the top of the stack. ECE 291 -- Spring 2000
Stack 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 PTR[BX] ;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 ECE 291 -- Spring 2000