assembly language for intel-based computers

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

Upload: phuc

Post on 21-Jan-2016

55 views

Category:

Documents


0 download

DESCRIPTION

Assembly Language for Intel-Based Computers. Kip R. Irvine. Chapter 8: Advanced Procedures. Chapter Overview. Stack Frames Recursion. Stack Parameters. More convenient than register parameters. Stack Frame. Also known as an activation record - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Assembly Language for Intel-Based Computers

Assembly Language for Intel-Based Assembly Language for Intel-Based ComputersComputers

Chapter 8: Advanced Procedures

Kip R. Irvine

Page 2: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 2Web site Examples

Chapter OverviewChapter Overview

• Stack Frames• Recursion

Page 3: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 3Web site Examples

Stack ParametersStack Parameters

• More convenient than register parameters

Page 4: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 4Web site Examples

Stack FrameStack 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.

Page 5: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 5Web site Examples

Explicit Access to Stack ParametersExplicit Access to Stack Parameters

• A procedure can explicitly access stack parameters using constant offsets from EBP1.

• 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

Page 6: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 6Web site Examples

Stack Frame ExampleStack Frame Example

.datasum DWORD ?.code

push 6 ; second argumentpush 5 ; first argumentcall AddTwo ; EAX = summov sum,eax ; save the sum

AddTwo PROCpush ebpmov ebp,esp..

Page 7: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 7Web site Examples

AddTwo ProcedureAddTwo Procedure

• Recall the AddTwo Procedure

Page 8: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 8Web site Examples

RET InstructionRET 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.

Page 9: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 9Web site Examples

Passing Arguments by ReferencePassing Arguments by Reference (1 of 2) (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:

.datacount = 100array dw count DUP(?).code

push OFFSET arraypush COUNTcall ArrayFill

Page 10: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 10Web site Examples

Passing Arguments by ReferencePassing Arguments by Reference (2 of 2) (2 of 2)

ArrayFill PROCpush ebpmov ebp,esppushadmov esi,[ebp+12]mov ecx,[ebp+8]cmp ecx,0jle L2

L1:mov [esi],0add esi,2loop L1

L2:popadpop ebpret 8

ArrayFill ENDP

ArrayFill can reference an array without knowing the array's name:

Page 11: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 11Web site Examples

Local VariablesLocal 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 PROCpush ebpmov ebp,espsub esp,8mov [ebp-4],123456h ; locAmov [ebp-8],0 ; locB..

Page 12: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 12Web site Examples

LOCAL DirectiveLOCAL 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 PROCLOCAL var1:BYTE, var2:WORD, var3:DWORD

Page 13: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 13Web site Examples

Using LOCALUsing LOCAL

LOCAL flagVals[20]:BYTE ; array of bytes

myProc PROC ; procedureLOCAL t1:BYTE, ; local variables

Examples:

Page 14: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 14Web site Examples

LOCAL ExampleLOCAL Example (1 of 2) (1 of 2)

BubbleSort PROCLOCAL temp:DWORD, SwapFlag:DWORD. . .ret

BubbleSort ENDP

BubbleSort PROCLOCAL temp:DWORD, SwapFlag:DWORD

push ebpmov ebp,espadd esp,0FFFFFFF8h ; add -8 to ESP. . .mov esp,ebppop ebpret

BubbleSort ENDP

Page 15: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 15Web site Examples

LOCAL ExampleLOCAL Example (2 of 2) (2 of 2)

Diagram of the stack frame for the BubbleSort procedure:

Page 16: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 16Web site Examples

Local ExampleLocal 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,44441111h

• mov flags[0],00• mov flags[1],11h• mov flags[2],22h• mov flags[3],33h

• mov sp,bp• pop bp• ret• xyz endp

Page 17: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 17Web site Examples

Local ExampleLocal Example

Page 18: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 18Web site Examples

Local ExampleLocal 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,44441111h

• 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

Page 19: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 19Web site Examples

RecursionRecursion

• What is recursion?• Recursively Calculating a Sum• Calculating a Factorial

Page 20: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 20Web site Examples

What is Recursion?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:

Page 21: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 21Web site Examples

What is Recursion?What is Recursion?

.codeA:

call Endlessmov ah,4CHint 21h

Endless PROCmov edx, ecxinc ecxcall Endlessret

Endless ENDP

End A

Page 22: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 22Web site Examples

Recursively Calculating a SumRecursively Calculating a Sum

CalcSum PROCcmp ecx,0 ; check counter valuejz L2 ; quit if zeroadd eax,ecx ; otherwise, add to sumdec ecx ; decrement countercall CalcSum ; recursive call

L2: retCalcSum ENDP

The CalcSum procedure recursively calculates the sum of an array of integers. Receives: ECX = count. Returns: EAX = sum

.codemov ecx, 5mov eax,0call CalcSum

Ll:; any instructions

Page 23: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 23Web site Examples

Calculating a FactorialCalculating a Factorial (1 of 3) (1 of 3)

int function factorial(int n){

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

}

5! = 5 * 4!

4! = 4 * 3!

3! = 3 * 2!

2! = 2 * 1!

1! = 1 * 0!

0! = 1

(base case)

1 * 1 = 1

2 * 1 = 2

3 * 2 = 6

4 * 6 = 24

5 * 24 = 120

1 = 1

recursive calls backing up

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.

Page 24: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 24Web site Examples

Calculating a FactorialCalculating a Factorial (2 of 3) (2 of 3)

Factorial PROCpush ebpmov ebp,espmov eax,[ebp+8] ; get ncmp eax,0 ; n < 0?ja L1 ; yes: continuemov eax,1 ; no: return 1jmp L2

L1: dec eaxpush eax ; Factorial(n-1)call Factorial

; Instructions from this point on execute when each; recursive call returns.

ReturnFact:mov ebx,[ebp+8] ; get nmul ebx ; eax = eax * ebx

L2: pop ebp ; return EAXret 4 ; clean up stack

Factorial ENDP

Page 25: Assembly Language for Intel-Based Computers

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 25Web site Examples

Calculating a FactorialCalculating a Factorial (3 of 3) (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.