Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.

Slides:



Advertisements
Similar presentations
Assembly Language Programming Chapter 8
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 8:Advanced Procedures (c) Pearson Education, All rights reserved. You may modify.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Conditional Loop Instructions LOOPZ and LOOPE LOOPNZ.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 8:Advanced Procedures (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for Intel-Based Computers
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
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.
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
Assembly Language Basic Concepts IA-32 Processor Architecture.
Semantics of Calls and Returns
Assembly Language for Intel-Based Computers Chapter 3: Assembly Language Fundamentals Kip Irvine.
Kip Irvine: Assembly Language for Intel-Based Computers
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.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
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.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
Assembly Language for Intel-Based Computers, 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for Intel-Based Computers, 5 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.
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.
CSC 221 Computer Organization and Assembly Language
Assembly Language for x86 Processors 7th Edition
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Compiler Construction Code Generation Activation Records
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
Chapter 8:Advanced Procedures. 2 Chapter Overview Local Variables Stack Parameters Stack Frames Recursion Creating Multimodule Programs.
Chapter 7 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
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.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Assembly Language for Intel-Based Computers, 4 th Edition Week 12: Advanced Procedures Modified by Dr. Osama Younes.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for x86 Processors 7th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for x86 Processors 6th Edition
Introduction to Compilers Tim Teitelbaum
Assembly IA-32.
Assembly Language for x86 Processors 6th Edition
Stack Frames and Advanced Procedures
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Assembly Language for Intel-Based Computers, 4th Edition
Computer Organization and Assembly Languages Yung-Yu Chuang 2008/12/22
Microprocessor and Assembly Language
Practical Session 4.
Multi-modules programming
Assembly Language for Intel-Based Computers, 5th Edition
Computer Organization and Assembly Languages Yung-Yu Chuang 2005/12/4
Miscellaneous Topics.
X86 Assembly Review.
Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/24
Computer Organization and Assembly Language
Assembly Language for Intel-Based Computers, 4th Edition
Presentation transcript:

Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Chapter Overview Stack Frames Recursion

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Stack Parameters More convenient than register parameters

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Stack Frame Also known as an activation record Area of the stack set aside for a procedure's return address, passed parameters, saved registers, and local variables Created by the following steps: Calling program pushes arguments on the stack and calls the procedure. The called procedure pushes EBP on the stack, and sets EBP to ESP. If local variables are needed, a constant is subtracted from ESP to make room on the stack.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Explicit Access to Stack Parameters A procedure can explicitly access stack parameters using constant offsets from EBP 1. Example: [ebp + 8] EBP is often called the base pointer or frame pointer because it holds the base address of the stack frame. EBP does not change value during the procedure. EBP must be restored to its original value when a procedure returns. 1 BP in Real-address mode

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Stack Frame Example.data sum DWORD ?.code push 6 ; second argument push 5 ; first argument call AddTwo; EAX = sum mov sum,eax; save the sum AddTwo PROC push ebp mov ebp,esp.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, AddTwo Procedure Recall the AddTwo Procedure

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, RET Instruction Return from subroutine Pops stack into the instruction pointer (EIP or IP). Control transfers to the target address. Syntax: RET RET n Optional operand n causes n bytes to be added to the stack pointer after EIP (or IP) is assigned a value.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Passing Arguments by Reference (1 of 2) The ArrayFill procedure fills an array with 0 The calling program passes the address of the array, along with a count of the number of array elements:.data count = 100 array dw count DUP(?).code push OFFSET array push COUNT call ArrayFill

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Passing Arguments by Reference (2 of 2) ArrayFill PROC push ebp mov ebp,esp pushad mov esi,[ebp+12] mov ecx,[ebp+8] cmp ecx,0 jle L2 L1: mov [esi],0 add esi,2 loop L1 L2: popad pop ebp ret 8 ArrayFill ENDP ArrayFill can reference an array without knowing the array's name:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Local Variables To explicitly create local variables, subtract their total size from ESP. The following example creates and initializes two 32- bit local variables (we'll call them locA and locB): MySub PROC push ebp mov ebp,esp sub esp,8 mov [ebp-4],123456h; locA mov [ebp-8],0; locB.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, LOCAL Directive A local variable is created, used, and destroyed within a single procedure The LOCAL directive declares a list of local variables immediately follows the PROC directive each variable is assigned a type Syntax: LOCAL varlist Example: MySub PROC LOCAL var1:BYTE, var2:WORD, var3:DWORD

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Using LOCAL LOCAL flagVals[20]:BYTE; array of bytes myProc PROC; procedure LOCAL t1:BYTE,; local variables Examples:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, LOCAL Example (1 of 2) BubbleSort PROC LOCAL temp:DWORD, SwapFlag:DWORD... ret BubbleSort ENDP BubbleSort PROC LOCAL temp:DWORD, SwapFlag:DWORD push ebp mov ebp,esp add esp,0FFFFFFF8h; add -8 to ESP... mov esp,ebp pop ebp ret BubbleSort ENDP

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, LOCAL Example (2 of 2) Diagram of the stack frame for the BubbleSort procedure:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Local Example xyz proc local v1:byte, v2:byte, v3:dword, flags[4]:BYTE push bp mov bp,sp sub sp,12 mov v1,21h mov v2,43h mov v3, h mov flags[0],00 mov flags[1],11h mov flags[2],22h mov flags[3],33h mov sp,bp pop bp ret xyz endp

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Local Example

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Local Example xyz proc local v1:byte, v2:byte, v3:dword, flags[4]:BYTE push bp mov bp,sp sub sp,12 mov v1,21h mov v2,43h mov v3, h Mov cx,4 Lea si,flags L1: mov byte ptr ss:[si], 22h inc si loop L1 mov sp,bp pop bp ret xyz endp

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Recursion What is recursion? Recursively Calculating a Sum Calculating a Factorial

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, What is Recursion? The process created when... A procedure calls itself Procedure A calls procedure B, which in turn calls procedure A Using a graph in which each node is a procedure and each edge is a procedure call, recursion forms a cycle:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, What is Recursion?.code A: call Endless mov ah,4CH int 21h Endless PROC mov edx, ecx inc ecx call Endless ret Endless ENDP End A

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Recursively Calculating a Sum CalcSum PROC cmp ecx,0; check counter value jz L2; quit if zero add eax,ecx; otherwise, add to sum dec ecx; decrement counter call CalcSum; recursive call L2: ret CalcSum ENDP The CalcSum procedure recursively calculates the sum of an array of integers. Receives: ECX = count. Returns: EAX = sum.code mov ecx, 5 mov eax,0 call CalcSum Ll: ; any instructions

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Calculating a Factorial (1 of 3) int function factorial(int n) { if(n == 0) return 1; else return n * factorial(n-1); } This function calculates the factorial of integer n. A new value of n is saved in each stack frame: As each call instance returns, the product it returns is multiplied by the previous value of n.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Calculating a Factorial (2 of 3) Factorial PROC push ebp mov ebp,esp mov eax,[ebp+8]; get n cmp eax,0; n < 0? ja L1; yes: continue mov eax,1; no: return 1 jmp L2 L1:dec eax push eax; Factorial(n-1) call Factorial ; Instructions from this point on execute when each ; recursive call returns. ReturnFact: mov ebx,[ebp+8] ; get n mul ebx ; eax = eax * ebx L2:pop ebp; return EAX ret 4; clean up stack Factorial ENDP

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Calculating a Factorial (3 of 3) Suppose we want to calculate 12! This diagram shows the first few stack frames created by recursive calls to Factorial Each recursive call uses 12 bytes of stack space.