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

Slides:



Advertisements
Similar presentations
Programming 8086 – Part IV Stacks, Macros
Advertisements

10-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL Subroutine and Interrupt.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
ICS312 Set 6 Operands. Basic Operand Types (1) Register Operands. An operand that refers to a register. MOV AX, BX ; moves contents of register BX to.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
More about procedures and Video Processing. Lesson plan Review existing concepts More about procedures and boolean expression Video processing.
Data Movement Instructions
Set 20 Interrupts. INTERRUPTS The Pentium has a mechanism whereby external devices can interrupt it. Devices such as the keyboard, the monitor, hard disks.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
IP high IP low IP high IP low BP high BP low IP high IP low BP high BP low FL high FL low CS high CS low IP high IP low _TEXTsegment byte public ‘CODE’
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
Semantics of Calls and Returns
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
Factorial of a number data segment x1 db 4 fact dw ? data ends
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
Procedures and the Stack Chapter 10 S. Dandamudi.
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
Implementing function/ procedure calls (in Pascal and C)
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Chapter 2 Instruction Addressing and Execution. Lesson plan Review some concepts in the first week First assembly program with EMU8086 Related concepts.
Strings, Procedures and Macros
Lecture 7 A closer look at procedures Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#9) By Dr. Syed Noman.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Writing and using procedures
ECE291 Lecture 6 Procedures and macros. ECE 291 Lecture 6Page 2 of 36 Lecture outline Procedures Procedure call mechanism Passing parameters Local variable.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
Multi-module programming. Requirements of an assembly language module when it is linked with another module PUBLIC directive - it exports to other modules.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
ECE291 Computer Engineering II Lecture 8 Josh Potts University of Illinois at Urbana- Champaign.
ICS312 Set 12 Subroutines: Passing Arguments Using the Stack.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Assembly Language Lecture 2. Lecture Outline Program Structure Memory models Data Segment Stack Segment Code Segment Input and Output Instructions INT.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Procedures Dr. Hadi Al Saadi Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java.
Chapter 14 Functions.
Assembly language programming
Multi-module programming
Format of Assembly language
COURSE OUTCOMES OF Microprocessor and programming
Microprocessor and Assembly Language
Microprocessor and Assembly Language
(The Stack and Procedures)
Chapter 3 Addressing Modes
CS 301 Fall 2002 Control Structures
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Stack and Subroutines Module M17.1 Section 11.2.
Programming 8086 – Part IV Stacks, Macros
8086 Registers Module M14.2 Sections 9.2, 10.1.
Stack Frames and Advanced Procedures
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Microprocessor and Assembly Language
EECE.3170 Microprocessor Systems Design I
(The Stack and Procedures)
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Morgan Kaufmann Publishers Computer Organization and Assembly Language
EECE.3170 Microprocessor Systems Design I
Miscellaneous Topics.
(The Stack and Procedures)
Procedures and Macros.
Presentation transcript:

Subroutines: Passing Arguments Using the Stack

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).

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

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:

TITLE MAIN3 (main3.asm) EXTRN CALC: NEAR.MODEL SMALL.STACK 100H.DATA X DW 30 Y DW 40.CODE MAIN3 PROC MOV 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

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

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

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 stack CALL CALC Ret Address SP -=2 [BP+2] = RETADDR (IP) PUSH BP BP SP -=2 [BP] = original contents of BP MOV BP, SP BP = SP push regs regs SP -=2/each

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).

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

NOTE C 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.

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

.CODE MAIN3 PROC MOV 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

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

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

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 address PUSH AX X address SP -=2 [BP+4]= X address CALL ADDNOS IP SP -=2 [BP+2] = return addr. offset PUSH BP BP SP -=2 [BP] = orig. contents of BP MOV BP, SP BP = SP PUSH regs regs SP -=2/reg

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.

Example on storing a result in an argument (Cont.1) Title SUB3.Model Small.586 PUBLIC SUB.CODE SUB 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

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 6 SUB ENDP END

FIXING UP THE STACK If 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: RET and, assuming that the subroutine is SUB1, the calling program should contain the code: call sub1 add sp, 6 (i.e. 2*no. of arguments)

ILLUSTRATION OF A RECURSIVE PROCEDURE A procedure to evaluate factorial(n) if n = 1 return 1 else return n*factorial(n-1)

factorialproc near push bp mov bp,sp cmp word ptr [bp+4], 1 jg cont mov ax, 1 jmp endup cont:mov bx, [bp+4] dec bx push bx call factorial

imulword ptr [bp+4] endup: pop bp ret 2 factorial endp end

Textbook Reading (Jones): Chapter 13 Procedures and High-Level Languages