EENG 4005 Microprocessors Outline Program Stack PUSH POP































- Slides: 31

EENG 4005 Microprocessors

Outline • • • Program Stack PUSH & POP instructions Procedures Macros vs. procedures EENG 4005

Stack Key Characteristics • Used to store temporary data during program execution • One point of access - the top of the stack • A stack is always operated as Last-In-First-Out (LIFO) storage, i. e. , data are retrieved in the reverse order to which they were stored • Instructions that directly manipulate the stack – PUSH – POP - place element on top of stack remove element from top of stack EENG 4005

Stack Implementation in Memory Original SP In Use Direction of increasing memory addresses In Use Stack grows in direction of decreasing memory addresses In Use SS: SP FREE EENG 4005 SS

Stack Implementation in Memory (cont. ) • SS - Stack Segment • SP (stack pointer) always points to the top of the stack – SP initially points to top of the stack (high memory address). – SP decreases as data is PUSHed PUSH AX ==> SUB SP, 2 ; MOV [SS: SP], AX – SP increases as data is POPed POP AX ==> MOV AX, [SS: SP] ; ADD SP, 2 • BP (base pointer) can point to any element on the stack EENG 4005

PUSH Instruction Example To address 12 FFF Register Array AX BX 6 A 03800 037 FF B 3 037 FE PUSH BX 6 AB 3 B X CX ef or e PU SH DX B 0800 SS 0300 BX U P er SP SP SH SP t Af 03000 STACK segment EENG 4005

POP Instruction Example To address 0 FFFF Register Array AX BX POP BX 392 F 2 F 01006 BX CX 39 01008 01007 SP Af te r. P OP DX SP 1006 SS 0000 O e. P P BX or SP f Be 00000 STACK segment EENG 4005

PUSH & POP (More I) • PUSH and POP always store or retrieve words of data (never bytes) in the 8086 -80286 microprocessor • The 80386/80486 allow words or double words to be transferred to and from the stack • The source of data for PUSH – any internal 16 -bit/32 -bit register, immediate data, any segment register, or any two bytes of memory data • The POP places data into – internal register, segment register (except CS), or a memory location EENG 4005

PUSH & POP (More II) • The 80286 and later microprocessors there is also PUSHA and POPA to store and retrieve, respectively the contents of internal register set (AX, CX, DX, BX, SP, BP, SI, and DI) • Stack initialization, example: – assume that the stack segment resides in memory locations 10000 h-1 FFFFh – the stack segment (SS)is loaded with 1000 h – the SP is loaded with 0000 h - to start the stack at the top of the 64 K…WHY? EENG 4005

The Stack Use • To store – registers – return address information while procedures are executing – local variables that procedures may require • To pass parameters to procedures EENG 4005

Temporary Register Storage • Push and Pop registers to preserve their value Example: PUSH AX PUSH BX. . . < modify contents of Registers AX & BX >. . . POP BX POP AX ; Place AX on the stack ; Place BX on the stack ; Restore original value of BX ; Restore original value of AX EENG 4005

Store Return Address of a Procedure Print. Rec main PROC NEAR. . . <Print value of a record>. . . RET ENDP PROC FAR. . . <Calculate Scores>. . . CALL Print. Rec <Continue Execution HERE>. . . CALL DOSXIT ENDP At execution time 1. processor encounters the CALL to the procedure 2. pushes the return address (instruction pointer of the next instruction after the CALL) onto the stack 3. jumps to Print. Rec 4. executes the code therein 5. pops the return address off the stack 6. returns to the calling code EENG 4005

Passing Parameters on the Stack • Stack can be used to pass parameter(s) to a procedure • The caller pushes the procedure parameter onto the stack and the callee finds the parameter there • When the procedure completes its task, the parameter should be popped from the stack MOV PUSH CALL AX, OFFSET String AX get. Str ; the proc get. Str expects the offset to the String to ; be on the stack EENG 4005

Passing Parameters on the Stack Example ; Use of Procedures when parameters are passed using Stack ………. ; ====== Stack ============================ stkseg segment stack ; *** STACK SEGMENT *** db 64 dup ('STACK ') ; 64*8 = 512 Bytes of Stack stkseg ends ; ====== Begin Code/Data ======================= cseg segment public 'CODE' ; *** CODE SEGMENT *** assume LEN CR LF Prompt 1 Prompt 2 String cs: cseg, ds: cseg, ss: stkseg, es: nothing EQU EQU BYTE 80 0 dh 0 ah "Input a string", 0 "Do another? ", 0 (LEN +1) DUP (? ) EENG 4005

Passing Parameters on the Stack Example (cont. ) Main PROC Begin: mov push call FAR ax, OFFSET Prompt 1 ax put. Str mov push call ax, OFFSET String ax get. Str mov push call ax, OFFSET String ax put. Str mov push call mov cmp je mov int MAIN ENDP CSEG ENDS END ax, OFFSET Prompt 2 ax put. Str EENG 4005 ax, OFFSET String ax get. Str bx, OFFSET String BYTE PTR [bx], 'y' Begin ax, 4 c 00 h 21 h MAIN

Passing Parameters on the Stack Example (cont. ) ; OFFSET of string to be printed must ; be on the stack and the string must ; be null terminated put. Str PROC NEAR push bp mov bp, sp push ax push bx push dx mov bx, [bp + 4] ; expect bx to point to string mov ah, 2 h ; prepare to print a char with 21 h next. Char: cmp BYTE PTR [bx], 0 h ; check for null terminator je found. End ; when found exit mov dl, [bx] int 21 h ; print with 21 h inc bx ; point to next char jmp next. Char found. End: pop dx OFFSET String pop bx RETURN IP pop ax BP pop bp OLD BP ret 2 AX put. Str ENDP BX Removes passed parameters from the stack EENG 4005 SP DX

Passing Parameters on the Stack Example (cont. ) ; OFFSET of large enough buffer must ; have been pushed onto stack ; string will be null terminated get. Str PROC NEAR push bp mov bp, sp push ax push bx mov bx, [bp + 4] ; base address of storing buffer mov ah, 01 h int cmp 21 h al, CR get. Loop: je mov get. End [bx], al ; bx points to storage location inc bx jmp get. Loop get. End: mov BYTE PTR [bx], 0 ; CR is converted in null term pop pop ret get. Str bx ax bp 2 ENDP OFFSET String RETURN IP BP AX SP ; look for CR in al EENG 4005 OLD BP BX

Procedures (Overview) • Group of instructions that usually perform one task • Reusable section of the software that is stored in memory once, but use as often as necessary • The stack stores the return address whenever a procedure is called during the execution of the program – CALL pushes the address of the instruction following it on the stack – RET removes an address from the stack so the program returns to the instruction following the call PROC NEAR My_Subroutine PROC FAR My_Subroutine RET • PUSH • JUMP POP (CS: ) IP IP Offset My_Subroutine Near calls and returns transfer control between procedures in the same code segment CS IP Segment My_Subroutine: Offset My_Subroutine Far calls and returns pass control between different segments EENG 4005

Procedures (Overview cont. ) • Procedures should save and restore registers that are modified in a subroutine. Print. Rec PROC NEAR PUSH AX PUSH BX PUSH CX PUSH DX PUSH SI < Code modifies AX, BX, CX, DX, SI > POP SI POP DX POP CX LIB 291 Routine to save ALL registers POP BX POP AX RSAVE: Save ALL registers RREST: Restore ALL registers RET Print. Rec ENDP EENG 4005

Procedures (Overview) • Parameters to a procedure can be passed in – – – on the stack global memory locations registers in the code stream in a parameter block reference by a pointer EENG 4005

Passing Parameters in Registers • Example: putsi (put short integer) routine outputs the value in AL as a signed integer putsi PROC PUSH AX CBW PUTI POP RET ENDP AX ; saves AH’s values ; sign extend AL --> AX ; do the work; puti expects the value of the ; signed integer in the AX register ; restore AH EENG 4005

Passing Parameters in the Code Stream • Example: Print. Lp: My. Print BYTE “Code stream parameter. ”, 0 Consider the following implementation of My. Print PROC NEAR PUSH BP MOVE BP, SP PUSH BX PUSH AX End. Str: My. Print MOV CMP JZ PUTC INC JMP INC MOV POP POP RET ENDP EENG 4005 BX, 2[BP] AL, CS: [BX] AL, 0 End. Str BX Print. Lp BX 2[BP], BX AX BX BP ; load return address into BX ; get next character ; check for end of the string ; move to the next char ; point at first byte beyond zero ; save as a new return address

Passing Parameters via a Parameter Block Consider simple subroutine that adds J and K together, storing the result in I. Parm. Block WORD I I WORD ? ; I, J, K must appear in this order J WORD ? K WORD ? …… LES bx, Parm. Block CALL Add. Em PROC NEAR PUSH AX MOV AX, ES: 2[BX] ; get J’s value ADD AX, ES: 4[BX] ; add in K’s value MOV ES: [BX], AX ; store result in I RET Add. EM ENDP EENG 4005

Macros • A macro inserts a block of statements at various points in a program during assembly • Text substitutions made at compile time – – NOT a procedure -- Code is literally dumped into program Parameter names are substituted Useful for tedious programming tasks Instantiated within code segment. EENG 4005

Macros (cont. ) • General Format MACRO_NAME MACRO Param 1, Param 2, . . . , Param. N LOCAL My. Label Your Code. . . Param 1. . . Param 2. . . Your Code. . . JMP My. Label Your Code. . . My. Label: . . . Param. N. . . Your Code. . . ENDM EENG 4005

Local Variable(s) in a Macro • A local variable is one that appears in the macro, but is not available outside the macro • We use the LOCAL directive for defining a local variable – If the label My. Label in the previous example is not defined as local, the assembler will flag it with errors on the second and subsequent attempts to use the macro • The LOCAL directive must always immediately follow the MACRO directive without any intervening comments or spaces • Macros can be placed in a separate file – use INCLUDE directive to include the file with external macro definitions into a program – no EXTERN statement is needed to access the macro statements that have been included EENG 4005

Macros (cont. ) Example: DIV 16 MACRO Result, X, Y ; Store into Result the signed result of X / Y ; Calculate Result = X / Y ; (all 16 -bit signed integers) ; Destroys Registers AX, DX MOV AX, X CWD IDIV Y MOV Result, AX ENDM • EENG 4005 ; Load AX with Dividend ; Extend Sign into DX ; Signed Division ; Store Quotient

Macros (cont. ) • Example: Using the macro in a program ; Variable Section var. X 1 DW 20 var. X 2 DW 4 var. R DW ? ; Code Section DIV 16 var. R, var. X 1, var. X 2 Will Actually Generate the following code (You won't actually see this unless you debug the program). MOV AX, var. X 1 CWD IDIV var. X 2 MOV var. R, AX EENG 4005

Macros vs Procedures Proc_1 PROC MOV MOV RET Proc_1 ENDP NEAR AX, 0 BX, AX CX, 5 CALL …. . . Macro_1 …… Macro_1 MACRO MOV AX, 0 MOV BX, AX MOV CX, 5 ENDM EENG 4005 Proc_1

Macros vs Procedures (cont. ) • In the example the macro and procedure produce the same result • The procedure definition generates code when the assembler encounters the PROC directive • The macro does not emit any code when processing the statements between the MACRO and ENDM – Upon encountering Macro_1 in the mnemonic field, MASM assembles every statement between the MACRO and ENDM directives and emits that code to the output file – At run time, the processor executes these instructions without the call/ret overhead EENG 4005

Macros vs Procedures (cont. ) • Advantage of using macros – execution of MACRO expansion is usually faster (no call and ret) than the execution of the same code implemented with procedures • Disadvantage – assembler copies the macro code into the program at each macro invocation – if the number of macro invocations within the program is large then the program will be much larger than when using procedures EENG 4005