1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

Post on 20-Dec-2015

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

1

Lecture 5: Procedures

Assembly Language for

Intel-Based Computers,4th edition

Kip R. Irvine

Outline

Stack Operations

Defining and Using Procedures

Stack Operations Why Stacks?

Recall data structures class.Recall how procedure (functions) used.Procedure calling is a stack operation.We use stack to keep track of return

addresses. Parameters and local variables are also put

on the stack when calling some subroutines.

Stack Operations Concepts

A stack is a LIFO (last-in, first-out) structure

The runtime stack is a memory that is managed directly by the CPU, using two registers: SS and ESP SS holds a segment descriptor and is not

modified by user program ESP holds a 32-bit offset into some location

on the stackAn Intel stack grows downward from

high memory to low memory

Format: PUSH r/m16/m32/imm16/imm32

Stack Operations PUSH Operation

Immediate values are 32-bit in protected mode and 16-bit in real mode

A 16-bit/32-bit operand causes ESP to be decremented by 2 and 4, respectively.

Push AX

Push BX

Offset Stack0024

01AB

AX

BX

0024

000001AB

ESP

000010000000100100001002

00001005

00001004

00001003XX

YY

ESP24

00

ESPAB

01

Format: POP r/m16/m32

Stack Operations POP Operation

POP BX

POP AX

Offset Stack0024

01AB

AX

BX

?

0000?000010000000100100001002

00001005

00001004

00001003XX

YY

24

00

ESPAB

01

ESP

01AB

0024

ESP

The area of the stack above ESP is logically empty, and will be overwritten

Stack Operations Additional Stack Operations

PUSHF and POPFPush and pops the Flag register. There are no operands

PUSHAD and POPAD Pushes registers on the stack in this order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI and pops them in reverse order

PUSHA and POPA The same except they work with 16-bit registers

Outline

Stack Operations

Defining and Using Procedures

Procedures Concepts

Procedure: blocks of code that are called and must be returned from A procedure begins with itsname proc

and terminate with itsname endp To end a procedure other that the program

startup procedure (main), use ret instruction Use call itsname to call the procedure.

It is a highly desirable to preserve registers when writing a procedure. Save at beginning and restore before returning

Procedures CALL and RET InstructionsCALL: directs the processor to begin

execution at a new memory location Pushes the return address on the stack Copies the called procedure address into the

EIP.RET: brings the processor back to the point in

the program where the procedure was called Popes the return address from the stack into the

EIP.

???Procedures Examples

main PROC00000020 Call MyProc00000025 MOV eax, ebx …

MyProc PROC00000040 MOV eax, edx …

retMyProc Endp

00000025 ESP

00000040 EIP

???00000025 ESP

00000025 EIP

Procedures Nested Procedures

ESP(return to sub2)

(return to sub1)

(return to main)

Low

high

Procedures Local and Global Labels

Global labels are followed by two colons, making them visible to the whole program

By default, a code label (followed by a single colon) has local scope, making it visible only to statements inside its enclosing procedure

main PROC Sub PROC

JMP L2 L2:

L1:: JMP L1

main endp ret …?

Procedures Passing Parameter

In registers - Fastest In global variables - Hard to reuse, poor

programming practiceOn the stack - Used by high level

languages

Procedures Examples

Calculating the sum of an integer array It is better to pass the offset of an array to the

procedure than to include references to specific variable names inside the procedure

Save and restore registers that are modified by a procedure

The return register should not be pushed and popped

Procedures Design using Procedures

Divide the specification into tasks

Understand the specification of the program

Design each task as a sub-procedure

Design the main procedure (the program start up procedure) to call all sub-procedures

Linking to External Library Link LibraryA file containing procedures that have

been assembled into machine code

Linker command

In your program, these procedure could be included and called

The assembler would leave the target address of the Call instruction blank, which will be filled by the linker

link32 %1.obj libraryName.lib

Outline

Stack Operations

Defining and Using Procedures

top related