Presentation is loading. Please wait.

Presentation is loading. Please wait.

CDP ECE 291 -- Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE.

Similar presentations


Presentation on theme: "CDP ECE 291 -- Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE."— Presentation transcript:

1 CDP ECE 291 -- Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE Office: 463 CSRL

2 CDP ECE 291 -- Spring 2000 Defining Constant Data & Structures Label OPCODE OPERAND(S)Comments DATA1DB 23H ; define DATA1 as a byte of 23H DATA2DW 1000H ; define DATA2 as a word of 1000H ARRAYDB 17 DUP (?) ; define array of 17 byte-sized entries LIST1DW 10 DUP (?) ; define array LIST1 of 10 word el. LIST2DW 1, 2, 3, 4, 5 ; define array LIST2 of 10 elements DW 6, 7, 8, 9, 10 ; same as LIST1 with initialized data

3 CDP 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

4 CDP ECE 291 -- Spring 2000 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 Important Rules to Remember: (cont.)

5 CDP 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)

6 CDP 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.

7 CDP ECE 291 -- Spring 2000 Example Program - code Label OpcodeOperandComments.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 MOVAX, 0;set AX to 0 MOVES, AX;address segment 0000 with ES MOVBX, OFFSET CARRAY ;address CARRAY with BX MOVCX, 50;load counter CX with 50 AGAIN: MOVAX, ES:[046CH];get clock value MOV[BX], AX;save clock value in CARRAY INCBX;increment BX to next element LOOPAGAIN;loop 50 times.EXIT;exit to DOS END;end file

8 CDP ECE 291 -- Spring 2000 Data Structures Label OpcodeOperandComments CLIENT STRUC CLIENT ENDS NAMES DB32 DUP (?); 32 bytes of name STREET DB32 DUP (?); 32 bytes of street CITY DB16 DUP (?); 16 bytes of city STATE DB2 DUP (?); 2 bytes of state ZIP DB5 DUP (?); 5 bytes of zip code NAME1 CLIENT Declaring & initializing new structures:

9 CDP ECE 291 -- Spring 2000 Operations on Structures - Use to address fields in structures Example: Clear street in array NAME1 MOV CX, 32 MOVAL, 0 MOVSI, OFFSET NAME1.STREET REPSTOSB 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

10 CDP 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 : 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 : 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.

11 CDP 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 PUSHAX;copies AX into the stack POPBX;loads BX with the top of the stack PUSH DS;copies DX onto the stack PUSH 1234H;pushes constant 1234H onto the stack POPCS;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 POPEAX;removes dword from stack into EAX PUSHEDI;copies EDI onto the stack


Download ppt "CDP ECE 291 -- Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE."

Similar presentations


Ads by Google