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

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

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

1

Lecture 5: Procedures

Assembly Language for

Intel-Based Computers,4th edition

Kip R. Irvine

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

Outline

Stack Operations

Defining and Using Procedures

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

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.

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

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

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

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

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

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

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

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

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

Outline

Stack Operations

Defining and Using Procedures

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

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

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

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.

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

???Procedures Examples

main PROC00000020 Call MyProc00000025 MOV eax, ebx …

MyProc PROC00000040 MOV eax, edx …

retMyProc Endp

00000025 ESP

00000040 EIP

???00000025 ESP

00000025 EIP

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

Procedures Nested Procedures

ESP(return to sub2)

(return to sub1)

(return to main)

Low

high

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

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 …?

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

Procedures Passing Parameter

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

programming practiceOn the stack - Used by high level

languages

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

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

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

Save and restore registers that are modified by a procedure

The return register should not be pushed and popped

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

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

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

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

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

Outline

Stack Operations

Defining and Using Procedures