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.

Slides:



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

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
10-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL Subroutine and Interrupt.
Computer Architecture CSCE 350
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
8086 Assembly Language Programming I
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
Set 20 Interrupts. INTERRUPTS The Pentium has a mechanism whereby external devices can interrupt it. Devices such as the keyboard, the monitor, hard disks.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
High-Level Language Interface Chapter 13 S. Dandamudi.
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.
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.
Debug and Assembler By, B.R.Chandavarkar Lect. COMP Department NITK, Surathkal.
Objective At the conclusion of this chapter you will be able to:
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
ICS312 Set 14 MACROS. Macros The program structure is similar to that for procedures. As for procedure names, macro names represent a group of instructions.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
Strings, Procedures and Macros
Lecture 7 A closer look at procedures Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
L AB 2. P ROGRAM STRUCTURE The assembly language program consist of code, data and stack. Data segment: contains all the variable definition..Data Code.
ECE291 Lecture 6 Procedures and macros. ECE 291 Lecture 6Page 2 of 36 Lecture outline Procedures Procedure call mechanism Passing parameters Local variable.
by Richard P. Paul, 2nd edition, 2000.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
Assembly Language programming
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of.
Lecture 9 (The Stack and Procedures). 1 Lecture Outline Introduction The Stack The PUSH Instruction The POP Instruction Terminology of Procedures INDEC.
Multi-module programming. Requirements of an assembly language module when it is linked with another module PUBLIC directive - it exports to other modules.
Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1.
ECE 353 Introduction to Microprocessor Systems Michael J. Schulte Week 6.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
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.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
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.
Lecture 3 Translation.
Chapter 14 Functions.
Assembly language programming
Multi-module programming
Instruction set Architecture
Chapter 8: Programming the Microprocessor
Format of Assembly language
MODULAR PROGRAMMING Many programs are too large to be developed by one person. programs are routinely developed by teams of programmers The linker program.
Assembly Language programming
Additional Assembly Programming Concepts
Microprocessor and Assembly Language
Microprocessor and Assembly Language
(The Stack and Procedures)
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
(The Stack and Procedures)
by Richard P. Paul, 2nd edition, 2000.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
(The Stack and Procedures)
Computer Organization and Assembly Language
Procedures & Macros Introduction Syntax Difference.
Procedures and Macros.
Presentation transcript:

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 2. The type of CALL employed can be: a. NEAR (pushing the return address and flags onto the stack) b. FAR (as above, but in addition pushing the code segment reg) 3. Arguments can be passed: a. through registers (as used by INT subroutine calls) b. using EXTRN and PUBLIC (global variables) c. using the stack (as used by all compilers) When using the stack to pass arguments: 4. arguments can be pushed onto the stack: a. from left to right (as occur in the source program “call” stmt.) b. from right to left 5. a. the value of the arguments can be pushed (call by value) b. the offsets of the arguments can be pushed (call by reference) 6. The stack pointer can be restored to its value before the call by: a. the subroutine (as in Visual Basic) b. the calling program (as in C/C++)

Definitions If a high-order language implements a call instruction, similar to e.g. “call sub2(x,y,z)”, by pushing x (i.e. the value of x, or its offset) onto the stack, then y, then z, it is said to push its arguments onto the stack from left to right. If it first pushes z, then y, then x, then it is said to push its arguments onto the stack from right to left.

If the high-order language implements a call statement by pushing onto the stack the value of the call statement’s arguments, then it is said to perform a call by value If, instead, it pushes onto the stack the offsets of these arguments, then it is said to perform a call by reference

Assume that a subroutine has e.g. 5 arguments Pushing these arguments onto the stack will reduce the stack pointer SP by 10. We say that the calling program restores the stack (i.e. sets SP to its original value) if the subroutine ends up with the “ret” instruction, and the calling program subsequently executes the instruction ADD SP,10 We say that the subroutine restores the stack, if it ends up with a “ret 10” instruction

A subroutine which is compiled together with its calling program is called an internal subroutine If it is compiled separately, it is called an external subroutine

There are 2 types of machine-language CALL instructions, the near call and the far call. In the near call, at execution time, the computer pushes onto the stack the offset of the address of the instruction following the call instruction itself, and then jumps to the location specified by the call instruction In the far call, the computer in addition pushes onto the stack the no. in the code segment register.

There are also 2 types of machine return instructions. Subroutines invoked by a near call, need to return using a near return instruction, and those invoked by a far call, need to employ a far return instruction. In Assembler one can specify the near return via “retn”, and the far return via “retf”, or (the usual practice), employ the ambiguous “ret” instruction, and leave it to the Assembler translator to work out which machine-language return instruction to use.

The proc statement of the subroutine indicates whether it is intended to be called by a near call and so requires a near return instruction, or via a far call which requires a far return instrucion. For example: SUB2 PROC FAR (The default if neither “FAR” nor “NEAR” is specified is near.)

The Assembler translator works out which machine-language call instruction to translate an Assembler call instruction, by whether the subroutine involved is declared as NEAR or FAR in an EXTRN statement in the calling program

FAR calls are needed, for example, in calling library subroutines, where we cannot ensure that the code for the subroutine is in the same code segment as the calling program (We will not employ far calls in this class)

Subroutine in C++ For example, C++ uses: External subroutines Call type NEAR Call by value Passes arguments using the stack Pushes arguments from right to left The calling program restores the stack

In contrast, most other commercial compilers (such as Visual Basic, Pascal, etc) pass their arguments from left to right, and expect the restoration of the stack pointer SP, to its value before the call, to be performed by the stack

Examples Using a subroutine to evaluate X - 2*Y Note: this is a function, since it returns a value. For all commercial compilers, the convention is to put the answer (return value) into AL, AX, EAX or DX:AX, or EDX:EAX

Preserving Registers. Subroutines (of whatever type) should save and restore the values stored in registers, except for registers that will be used to return values to the calling procedure. This convention is needed to make sure that subroutines do not cause unexpected errors after control returns to the calling procedure.

Method 1: Passing Arguments in Registers Passing arguments in registers is a convenient and efficient way to pass values between procedures.

Internal Example TITLE INTERNAL EXAMPLE: X – 2*Y.MODEL SMALL.STACK 100H.DATA X DW 20 Y DW 3 Z DW ?.CODE MAIN3 PROC MOV MOV DS, AX MOV BX, X MOV CX, Y CALL SUB1 MOV Z, AX MOV AX, 4C00H INT 21H MAIN3 ENDP

Internal Example (Cont.) SUB1 PROC ; X is BX, Y is in CX SUB BX, CX MOV AX, BX RET SUB1 ENDP END MAIN3 Note: in the internal form, the entire program is all in one file.

External Example TITLE EXTERNAL EXAMPLE: X – 2*Y EXTRN SUB1:NEAR.MODEL SMALL.STACK 100H.DATA X DW 20 Y DW 3 Z DW ?.CODE MAIN3 PROC MOV MOV DS, AX MOV BX, X MOV CX, Y CALL SUB1 MOV Z, AX MOV AX, 4C00H INT 21H MAIN3 ENDP END MAIN3 ; this is the end of MAIN3.ASM

External Example (Cont. 1) TITLE SUB1 in a separate file sub1.asm PUBLIC SUB1.MODEL SMALL ; no stack segment ; data segment is optional.CODE SUB1 PROC NEAR SUB BX,CX MOV AX,BX RET SUB1 ENDP END ; NOTE this is NOT “END SUB1”

External Example (Cont. 2) To assemble and link the above: ml main3.asm sub1.asm will produce: main3.exe To debug the above: masm /zi main3; masm /zi sub1; link /co main3 sub1; This produces main3.exe, but also allows you to debug using: cv main3 (From now on, we'll only do external examples.)

Method 2: Using EXTRN and PUBLIC to pass the values of the arguments Variables can be used as global variables in assembler programs by declaring them: as EXTRN (if defined in another module) or as PUBLIC (if defined in the current module - and are then globally available for use by other modules.)

Example of Method2 TITLE MAIN3 USING GLOBAL VARIABLES: X – 2*Y ; comments to describe program should go here EXTRN SUB1:NEAR ; SUB1 is in another file PUBLIC X, Y ; makes X and Y global.MODEL SMALL.STACK 100H.DATA X DW 20 Y DW 3 Z DW ?

Example of Method2 (Cont. 1).CODE MAIN3 PROC MOV MOV DS, AX CALL SUB1 MOV Z, AX MOV AX, 4C00H INT 21H MAIN3 ENDP END MAIN3 ; this is the end of MAIN3.ASM

Example of Method2 (Cont. 2) TITLE SUB1 in a separate file sub1.asm ; comments to describe SUB1 module PUBLIC SUB1 ; SUB1 can be called by other modules EXTRN X:WORD, Y:WORD ; X & Y must be made public elsewhere.MODEL SMALL ; no stack segment ; data segment is optional.CODE SUB1 PROC NEAR MOV AX, X SUB AX, Y RET SUB1 ENDP END ; end of SUB1.ASM

TERMINOLOGY function - a subroutine that returns a result (usually in AX) subroutine - any block of instructions that can be called (with a return) from another place procedure - same as subroutine

Textbook Reading: Jones, Chapter 6 Homework. Write a programs on the computer to evaluate X + Y – Z employing: an external subroutine and passing arguments via PUBLIC and EXTRN declarations