Top Banner
Subroutines: Passing Arguments Using the Stack
24

Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Dec 17, 2015

Download

Documents

Benedict Pierce
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Subroutines: Passing Arguments Using the Stack

Page 2: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Passing Arguments via the Stack

Arguments to a subroutine are pushed onto the stack.

The subroutine accesses the arguments from the stack using the Base Pointer (BP) and indirect addressing.

Arguments can be passed by value (their values are pushed onto the stack, or by reference (their offsets are pushed onto the stack).

Page 3: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

The calling routine used in MAIN3 can be either:

To use CALL by Value: PUSH X PUSH Y CALL SUB1

To use CALL by Reference: LEA AX, X PUSH AX LEA AX, Y PUSH AX CALL SUB1

Page 4: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Call by Value Using the Stack

Suppose that X and Y are defined in the MAIN file:

    X DW ...    Y DW ...

To call a subroutine CALC to evaluate X - 2Y using call by value:

Page 5: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

TITLE MAIN3 (main3.asm) EXTRN CALC: NEAR .MODEL SMALL .STACK 100H .DATA X DW 30 Y DW 40 .CODE MAIN3 PROC     MOV AX, @DATA     MOV DS, AX PUSH Y ; call by value PUSH X CALL CALC ; the answer should be returned in AX MOV AX,4C00H     INT 21H MAIN3 ENDP     END MAIN3

Page 6: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

The methods described on the following slides are used by all commercial compilers

Page 7: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Call by Value Using the Stack(Cont.)

TITLE CALC (CALC.ASM - a separate file) PUBLIC CALC .MODEL SMALL .CODE CALC PROC NEAR ;evaluates X - 2Y with result in AX PUSH BP          ;save BP (and DEC SP)     MOV BP,SP        ;BP pts to stack top ; push any registers to be used in the subroutine and ; restored before returning from the subroutine here     MOV AX,[BP+4]   ;AX has X     SUB AX,[BP+6]   ;AX = X - Y SUB AX,[BP+6] ;AX = X - 2Y ; pop any registers that were saved in the subroutine here     POP BP          ;restore BP     RET 4           ;pop IP and add 4 bytes to SP CALC ENDP     END

Page 8: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Call by Value Using the Stack(Cont.)

Stack Representation for NEAR Call by Value in this version of the program:

Instruction      Stack Contents     SP          BP (at the end of this code)

PUSH Y (Arg1)   Y SP -=2     [BP+6] = offset of Y on stack PUSH X (Arg2)   X               SP -=2     [BP+4] = offset of X on stackCALL CALC        Ret Address         SP -=2     [BP+2] = RETADDR (IP)PUSH BP          BP                 SP -=2     [BP] = original contents of BPMOV BP, SP BP = SPpush regs regs SP -=2/each

Page 9: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Call by Value Using the Stack(Cont.)

The purpose of using the BP in this way is because it gives a standard way to retrieve arguments from the stack that is not affected by pushing any additional registers or other values within the subroutine.

Note: if BP is used for indirect addressing, it is assumed to be referring to an offset in the stack segment (SS). Any other register used for indirect addressing is assumed to be an offset in the data segment (DS).

Page 10: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

TITLE CALC (CALC.ASM - a separate file) PUBLIC CALC .MODEL SMALL .CODE CALC PROC NEAR ;evaluates X - 2Y with result in AX PUSH BP          ;save BP     MOV BP,SP        ;BP points to stack top ; push any registers to be used in the subroutine     MOV AX, [BP+4]   ;AX has X     SUB AX, [BP+6]   ;AX = X - Y SUB AX, [BP+6] ;AX = X - 2Y ; pop any registers that were saved in the subroutine     POP BP          ;restore BP     RET 4           ;pop IP and add 4 bytes to SP CALC ENDP     END

Page 11: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

NOTEC assumes that (1) the calling program will fix the stack after the return from a subroutine (2) arguments will be passed using call by value (3) arguments are pushed in reverse order, as shown above.

Different compilers use different calling conventions.

Page 12: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Call by Reference Using the Stack

Here is another version to demonstrate passing arguments by reference on the stack.

TITLE MAIN3 (main3.asm) EXTRN SUB2: NEAR .MODEL SMALL .STACK 100H .DATA X DW 30 Y DW 40

Page 13: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

.CODE MAIN3 PROC     MOV AX,@DATA     MOV DS,AX PUSH OFFSET Y ; using call by reference PUSH OFFSET X CALL CALC ; answer should be in AX MOV AX,4C00H     INT 21H MAIN3 ENDP     END MAIN3

Page 14: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Call by Reference Using the Stack

TITLE SUB2 (sub2.asm - a separate file) PUBLIC SUB2 .MODEL SMALL .CODE SUB2 PROC NEAR ;evaluates X - 2Y, with result in AX PUSH BP          ;save BP (and DEC SP)     MOV BP,SP        ;BP pts to stack top ; push any registers to be used in the subroutine and

; restored before returning from the subroutine here

Page 15: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

MOV BX, [BP+4]   ;BX has address of X MOV AX, [BX] ;AX = X     MOV BX, [BP+6]   ;BX has address of Y SUB AX, [BX] ;AX = X - Y SUB AX, [BX] ;AX = X - 2Y

;pop any registers that were saved in the subroutine here     POP BP          ;restore BP     RET 4           ;pop IP and add 4 bytes to SP SUB2 ENDP     END

Page 16: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Stack Representation for call by reference example

Instruction    Stack contents     SP          BP (at end of this code)PUSH AX          Y address     SP -=2      [BP+6]= Y addressPUSH AX          X address     SP -=2      [BP+4]= X addressCALL ADDNOS      IP                SP -=2      [BP+2] = return addr. offsetPUSH BP          BP                SP -=2      [BP] = orig. contents of BP MOV BP, SP BP = SP PUSH regs        regs         SP -=2/reg 

Page 17: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Example on storing a result in an argument

Call SUB(X, Y, Z), Result is to set Z = Y – X

Main Program . . LEA AX, X PUSH AX LEA AX, Y PUSH AX LEA AX, Z PUSH AX CALL SUB . .

Page 18: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Example on storing a result in an argument (Cont.1)

Title SUB3 .Model Small .586 PUBLIC SUB

.CODESUB PROC NEAR PUSH BP ;Save BP MOV BP, SP PUSH DX ;Save DX PUSH BX ;Save BX MOV BX, [BP+6] ;BX = OFFSET of ADDRESS of Y MOV DX, [BX] ;DX = Y MOV BX, [BP+8] ;BX = OFFSET of ADDRESS of X SUB DX, [BX] ;DX = Y - X

Page 19: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Example on storing a result in an argument (Cont.2)

; to store DX in Z MOV BX, [BP+4] ; puts offset of Z into BX MOV [BX], DX

POP BX ; restore original value of BX POP DX POP BP RET 6SUB ENDP END

Page 20: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

FIXING UP THE STACKIf the subroutine is to fix up the stack,

then it should end up with: RET 2*no. of arguments

e.g. if there are 3 arguments, then it should end up with: RET 6

If the calling program is to fix up the stack, then the subroutine should end up with: RETand, assuming that the subroutine is SUB1, the calling program should contain the code: call sub1 add sp, 6 (i.e. 2*no. of arguments)

Page 21: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

ILLUSTRATION OF A RECURSIVE PROCEDURE

A procedure to evaluate factorial(n)

if n = 1 return 1else return n*factorial(n-1)

Page 22: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

factorial proc nearpush bpmov bp,spcmp word ptr [bp+4], 1jg contmov ax, 1jmp endup

cont: mov bx, [bp+4]dec bxpush bxcall factorial

Page 23: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

imulword ptr [bp+4]endup:

pop bpret 2

factorial endpend

Page 24: Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.

Textbook Reading (Jones):

Chapter 13  Procedures and High-Level Languages