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.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
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.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Runtime Stack Managed by the CPU, using two registers
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition 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)
Libraries and Procedures
Accessing parameters from the stack and calling functions.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
CS2422 Assembly Language & System Programming October 26, 2006.
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
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.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#8) By Dr. Syed Noman.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
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.
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Stack and Procedures Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa
CSC 221 Computer Organization and Assembly Language
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#9) By Dr. Syed Noman.
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.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
1 Assembly Language: Function Calls Jennifer Rexford.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
CS2422 Assembly Language and System Programming 0 Week 13 & 14 Codes in Assembly Language.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Stack Operations Dr. Hadi AL Saadi.
Computer Architecture and Assembly Language
Format of Assembly language
Data Transfers, Addressing, and Arithmetic
Additional Assembly Programming Concepts
Microprocessor and Assembly Language
Introduction to Compilers Tim Teitelbaum
Assembly Language Programming Part 2
(The Stack and Procedures)
Programming 8086 – Part IV Stacks, Macros
Stack Frames and Advanced Procedures
(The Stack and Procedures)
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Multi-modules programming
Miscellaneous Topics.
X86 Assembly Review.
(The Stack and Procedures)
Computer Organization and Assembly Language
ICS51 Introductory Computer Organization
Procedures & Macros Introduction Syntax Difference.
Procedures and Macros.
Presentation transcript:

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 or C++ function

Procedures  A procedure is a logically self-contained unit of code  Called sometimes a function, subprogram, or subroutine  Receives a list of parameters, also called arguments  Performs computation and returns results  Plays an important role in modular program development  Example of a procedure (called function) in C language int sumof ( int x,int y,int z ) { int temp; temp = x + y + z; return temp; }  The above function sumof can be called as follows: sum = sumof( num1,num2,num3 ); Result type Return function result Formal parameter list

Defining a Procedure in Assembly  Assembler provides two directives to define procedures  PROC to define name of procedure and mark its beginning  ENDP to mark end of procedure  A typical procedure definition is procedure_name PROC... ; procedure body... RET ;to return to caller procedure_name ENDP  procedure_name should match in PROC and ENDP

Example of a Procedure Definition  The sumof procedure receives three integer parameters  Assumed to be in AX, BX, and CX  Computes and returns result in register AX ; ; Sumof: Calculates the sum of three integers ; Receives: AX, BX, CX, the three integers ; Returns: AX = sum, and the flags:CF,OF,ZF,SF ; Requires: nothing ; sumof PROC addAX, BX; AX = AX + second number addAX, CX; AX = AX + third number ret; return to caller sumof ENDP  The ret instruction returns control to the caller

The Call Instruction  To invoke a procedure, the call instruction is used  The call instruction has the following format call procedure_name  Example on calling the procedure sumof  Caller passes actual parameters in AX, BX, and CX  Before calling procedure sumof movAX, num1; pass first parameter in AX movBX, num2; pass second parameter in BX movCX, num3; pass third parameter in CX callsumof; result is in EAX movsum, AX; save result in variable sum  call sumof will call the procedure sumof

How a Procedure Call / Return Works  How does a procedure know where to return?  There can be multiple calls to same procedure in a program  Procedure has to return differently for different calls  It knows by saving the return address (RA) on the stack  This is the address of next instruction after call  The call instruction does the following  Pushes the return address on the stack  Jumps into the first instruction inside procedure  ESP = ESP – 4; [ESP] = RA; EIP = procedure address  The ret (return) instruction does the following  Pops return address from stack  Jumps to return address: EIP = [ESP]; ESP = ESP + 4

CALL-RET Example main PROC call MySub mov ax,bx. main ENDP MySub PROC mov ax,dx. ret MySub ENDP is the offset of the instruction immediately following the CALL instruction is the offset of the first instruction inside MySub

CALL-RET Example The CALL instruction pushes onto the stack, and loads into EIP The RET instruction pops from the stack into EIP

Free Area Allocated Before Call ESP = 0012FFC4 Details of CALL and Return ESP AddressMachine CodeAssembly Language.CODE main PROC A movEAX, num B 1D movEBX, num B8B 0D movECX, num E Bcallsumof A Cmovsum, EAX exit main ENDP sumof PROC C3addEAX, EBX C1addEAX, ECX C3ret sumof ENDP END main RA= IP-relative call EIP = B EIP = After Call ESP = 0012FFC0 After Ret (Return) ESP = 0012FFC4 Runtime Stack

Don’t Mess Up the Stack ! Just before returning from a procedure ›Make sure the stack pointer ESP is pointing at return address Example of a messed-up procedure ›Pushes EAX on the stack before returning ›Stack pointer ESP is NOT pointing at return address! main PROC callmessedup... exit main ENDP messedupPROC pushEAX ret messedupENDP Free Area Used high addr Stack ESP Return Addr ESP EAX Value Where to return? EAX value is NOT the return address!

Nested Procedure Calls By the time Sub3 is called, the stack contains all three return addresses main PROC.. call Sub1 exit main ENDP Sub1 PROC.. call Sub2 ret Sub1 ENDP Sub2 PROC.. call Sub3 ret Sub2 ENDP Sub3 PROC.. ret Sub3 ENDP return address of call Sub3 ESP return address of call Sub2 return address of call Sub1

Example: Find the product of two positive integers A and B, by addition and bit shifting using procedure Pseudo Algorithm: Product=0 REPEAT IF lsb of B is 1 (Recall 1sb= LSB) THEN Product= Product +A END_IF Shift Left A, Shift Right B UNTIL B=0

Assembly Code TITLE PGM8_2: Multiplication by ADD AND SHIFT. MODEL SMALL. STACK 100H.CODE MAIN PROC ; EXECUTE IN DEBUG. Place A in AX and B in BX Mov Ax,A Mov Bx,B CALL MULTIPLY ;DX will contain the product MOV AH,4CH INT 21h MAIN ENDP MULTIPLY PROC ; AX=A, BX=B, in range 0-FFh, OUTPUT=DX=product=DX PUSH AX PUSH BX XOR DX,DX,

REPEAT: ; if B is odd TEST BX,1 ;is B odd? JZ END_IF ;no. is even ADD DX,AX ; product=product+A END_IF: SHL AX,1 ;shift left A SHR BX,1 ; shift right B ; until JNZ REPEAT POP BX POP AX RET MULTIPLY ENDP END MAIN

Ex: Write a procedure REVERSE that will reverse an array of N- word ; that is the N th word becomes the first, and ( N-1) word become the second The procedures is entered with SI pointing to array, Bx has N, the number of the words solution: Exchange the 1 st element with N th element, and the 2 nd with N-1 element. The number of exchanges will be N/2.model small.stack 100h.data Array dw 3456,2AB3, 8234,6789,2030,4080,BC3A,7890,2390,ABC4,7890,2C3D.code Main proc Mov si, OFFSET array Mov Bx, LENGTHOF Array Call REVERSE Mov 4ch,ah int 21h Main endp

REVERSE Proc Push AX Push BX Push si Push CX Mov CX,BX ; Cx=N Mov DX,SI Dec BX ; Bx=N-1 SHL BX,1 ; Bx=2*(N-1) Add DX,BX ; DX=2(N-1)+SI SHR CX,1 ; CX=N/2 ; Swap operation xx: Mov BX,[SI] XCHG BX, [DX] Mov [SI], BX Add SI,2 Sub DX,2 Loop xx Ret REVERSE endp End main