52
Procedures and the Stack Chapter 5 S. Dandamudi

Procedures and the Stack Chapter 5 S. Dandamudi. 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,

Embed Size (px)

Citation preview

Procedures and the Stack

Chapter 5

S. Dandamudi

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 2

Outline

• What is stack?

• Pentium implementation of stack

• Stack instructions

• Uses of stack

• Procedures Pentium instructions

• Parameter passing Register method Stack method

• Examples Call-by-value Call-by-reference Bubble sort

• Procedures with variable number of parameters

• Local variables

• Multiple source program modules

• Performance: Procedure overheads

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 3

What is a Stack?

• Stack is a last-in-first-out (LIFO) data structure• If we view the stack as a linear array of elements,

both insertion and deletion operations are restricted to one end of the array

• Only the element at the top-of-stack (TOS) is directly accessible

• Two basic stack operations: push (insertion) pop (deletion)

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 4

What is a Stack? (cont’d)

• Example Insertion of data items into the stack

» Arrow points to the top-of-stack

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 5

What is a Stack? (cont’d)

• Example Deletion of data items from the stack

» Arrow points to the top-of-stack

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 6

Pentium Implementation of the Stack

• Stack segment is used to implement the stack Registers SS and ESP (32-bit) or SP (16-bit) are used SS:ESP or SS:SP represents the top-of-stack

• Pentium stack implementation characteristics are: Only words (i.e., 16-bit data) or doublewords (i.e., 32-

bit data) are saved on the stack, never a single byte Stack grows toward lower memory addresses (i.e.,

stack grows “downward”) Top-of-stack (TOS) always points to the last data item

placed on the stack

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 7

Pentium Stack Instructions

• Pentium provides two basic instructions:push source

pop destination

• source and destination can be a 16- or 32-bit general register a segment register a word or doubleword in memory

• source of push can also be an immediate operand of size 8, 16, or 32 bits

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 8

Pentium Stack Example - 1

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 9

Pentium Stack Instructions: Examples

• On an empty stack, the following sequence of push instructions

push 21ABH

push 7FBD329AH

results in the stack state shown in (c) in the last figure

• On this stack, executingpop EBX

results in the stack state shown in (b) in the next figure

and the register EBX gets the value 7FBD329AH

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 10

Pentium Stack Example - 2

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 11

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 12

Additional Pentium Stack Instructions

Stack Operations on Flags• push and pop instructions cannot be used with

the Flags register• Two special instructions for this purpose are

pushfd (push 32-bit flags)

popfd (pop 32-bit flags)

• No operands are required• Use pushfw and popfw for 16-bit flags

(FLAGS)

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 13

Additional Pentium Stack Instructions (cont’d)

Stack Operations on All General-Purpose Registers• pushad and popad instructions can be used to save and

restore the eight general-purpose registersEAX, ECX, EDX, EBX, ESP, EBP, ESI, and EDI

• Pushad pushes these eight registers in the above order (EAX first and EDI last)

• popad restores these registers except that ESP value is not loaded into the ESP register

• Use pushaw and popaw for saving and restoring 16-bit registers

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 14

Uses of the Stack

• Three main uses» Temporary storage of data» Transfer of control» Parameter passing

Temporary Storage of DataExample: Exchanging value1 and value2 can be

done by using the stack to temporarily hold datapush value1push value2pop value1pop value2

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 15

Uses of the Stack (cont’d)

• Often used to free a set of registers

;save AX & BX registers on the stack

push AX

push BX

; AX and BX registers can now be usedmov AX,value1mov BX,value2mov value1,BXmov value2,AX

;restore AX & BX registers from the stack

pop BX

pop AX

. . .

Assembly Example1

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 16

; Assembly language that reads two characters; and swap them using stack

.586option segment:use16.model small.stack 200hprint_msg MACRO msg mov dx,offset msg mov ah,9 int 21hENDM

Assembly Example1

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 17

read_char MACRO mov ah,1 int 21h sub ah,ahENDM print_char MACRO char mov dl,char mov ah,2 int 21hENDM

Assembly Example1

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 18

.data msg1 db 'Enter 1st character:$' msg2 db 0ah,0dh,'Enter 2nd character:$' resmsg1 db 0ah,0dh,'1st character after swapping:$' resmsg2 db 0ah,0dh,'2nd character after swapping:$' .code .startup print_msg msg1 read_char push ax print_msg msg2

Assembly Example1

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 19

read_char push ax print_msg resmsg1 pop ax print_char al print_msg resmsg2 pop ax print_char al .exitend

Assembly Example2

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 20

; Assembly language that reads n characters; and print them in reverse order using stack

.586option segment:use16.model small.stack 200hprint_msg MACRO msg mov dx,offset msg mov ah,9 int 21hENDM

Assembly Example2

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 21

read_char MACRO mov ah,1 int 21h sub ah,ah push axENDM print_char MACRO pop dx mov ah,2 int 21hENDM

Assembly Example2

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 22

.data msg1 db 'How many characters do you want to enter?$' msg2 db 0ah,0dh,'Enter characters:',0ah,0dh,'$' resmsg db 0ah,0dh,'Characters you entered in reverse order:',0ah,0dh,'$' errormsg db 0ah,0dh,'Error,You should enter a number$' counter dw ?.code .startup print_msg msg1 read_char

Assembly Example2

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 23

pop counter sub counter,30h cmp counter,1 jl error cmp counter,9 jg error mov cx,counter print_msg msg2read_loop: read_char loop read_loop print_msg resmsg mov cx,counter

Assembly Example2

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 24

print_loop: print_char loop print_loopexit:.exiterror:print_msg errormsg jmp exit

end

Assembly Example3

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 25

; Assembly language that reads n numbers; and then prints all even numbers in reverse ; order using stack

print_msg MACRO msg mov dx,offset msg mov ah,9 int 21hENDM

Assembly Example3

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 26

read_char MACRO mov ah,1 int 21h mov bl,al sub bl,30h sub bh,bhENDMnewline MACRO mov dl,0ah mov ah,2 int 21h mov dl,0dh int 21hENDM

Assembly Example3

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 27

print_char MACRO pop dx add dl,30h mov ah,2 int 21h newlineENDM

.586option segment:use16.model small.stack 200h

Assembly Example3

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 28

.data msg1 db 'How many numbers do you want to enter?$' msg2 db 0ah,0dh,'Enter numbers:',0ah,0dh,'$' resmsg db 0ah,0dh,'Even numbers you entered in reverse order:',0ah,0dh,'$' errormsg db 0ah,0dh,'Error,You should enter a number$' ecounter dw 0.code .startup print_msg msg1 read_char cmp bl,1 jl error

Assembly Example3

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 29

cmp bl,9 jg error mov cl,bl sub ch,ch print_msg msg2read_loop: read_char test bl,01h jnz next push bx inc ecounter

Assembly Example3

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 30

next: newline loop read_loop print_msg resmsg mov cx,ecounterprint_loop: print_char loop print_loopexit:.exiterror:print_msg errormsg jmp exit end

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 31

Uses of the Stack (cont’d)

Transfer of Control• In procedure calls and interrupts, the return

address is stored on the stack• Our discussion on procedure calls clarifies this

particular use of the stack

Parameter Passing• Stack is extensively used for parameter passing• Our discussion later on parameter passing

describes how the stack is used for this purpose

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 32

Procedures

• Two types Call-by-value

» Receives only values

» Similar to mathematical functions

Call-by-reference» Receives pointers

» Directly manipulates parameter storage

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 33

Pentium Instructions for Procedures

• Pentium provides two instructions: call and ret• call instruction is used to invoke a procedure• The format is

call proc-nameproc-name is the procedure name

• Actions taken during a near procedure call:

ESP = ESP - 4 ; push return address SS:ESP = EIP ; onto the stackEIP = EIP + relative displacement ; update EIP to point to the procedure

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 34

Pentium Instructions for Procedures (cont’d)

• ret instruction is used to transfer control back to the calling procedure

• How will the processor know where to return? Uses the return address pushed onto the stack as part of

executing the call instruction Important that TOS points to this return address when ret instruction is executed

• Actions taken during the execution of ret are:

EIP = SS:ESP ; pop return addressESP = ESP + 4 ; from the stack

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 35

Pentium Instructions for Procedures (cont’d)

• We can specify an optional integer in the ret instruction The format is

ret optional-integer

Example: ret 8

• Actions taken on ret with optional-integer are:

EIP = SS:ESP ESP = ESP + 4 + optional-integer

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 36

How Is Program Control Transferred?

Offset(hex) machine code(hex)main:

. . . . 00000002E816000000 call sum0000000789C3 mov EBX,EAX

. . . . ; end of main procedure

sum:0000001D55 push EBP

. . . . ; end of sum procedure

avg: . . . .

00000028E8F0FFFFFF call sum0000002D89D8 mov EAX,EBX

. . . . ; end of avg procedure

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 37

Procedure Declaration in MASM

• The syntax of procedure declaration is the following:

name PROC

      ; here goes the code      ; of the procedure ...

RETname ENDP

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 38

Procedure Declaration in MASM

• Procedure is a part of code that can be called from your program in order to make some specific task.

• Procedures make program more structural and easier to understand.

• Generally procedure returns to the same point from

where it was called.

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 39

Parameter Passing

• Parameter passing is different and complicated than in a high-level language

• In assembly language» You should first place all required parameters in a mutually

accessible storage area» Then call the procedure

• Type of storage area used» Registers (general-purpose registers are used)» Memory (stack is used)

• Two common methods of parameter passing:» Register method» Stack method

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 40

Parameter Passing: Register Method

• Calling procedure places the necessary parameters in the general-purpose registers before invoking the procedure through the call instruction

• Examples:

Example1:» call-by-value using the register method

» a simple sum procedure

Example2:» call-by-reference using the register method» string length procedure

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 41

Example1 : Register Method

; Assembly language that adds two numbers using

;call by value procedure

.586

option segment:use16

.model small

print_msg Macro msg

mov dx,offset msg

mov ah,9

int 21h

ENDM

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 42

Example1 : Register Method

newline Macro

mov dl,0ah

mov ah,2

int 21h

mov dl,0dh

int 21h

ENDM

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 43

Example1 : Register Method

read_num Macro

mov ah,1

int 21h

sub al,30h

ENDM

.DATA

msg1 db 'Please input the first number:$'

msg2 db 'Please input the second number:$'

sum_msg db 'The sum is: $'

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 44

Example1 : Register Method

.code

.startup

print_msg msg1

read_num

mov cl,al

newline

print_msg msg2

read_num

mov ch,al

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 45

Example1 : Register Method

newline

call sum

print_msg sum_msg

call print_res

.exit ; return to operating system.

sum PROC

add ch,cl

RET ; return to caller.

sum ENDP

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 46

Example1 : Register Method

print_res PROC

mov dl,ch

add dl,30h

mov ah,2

int 21h

RET ; return to caller.

print_res ENDP

end

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 47

Example2 : Register Method

; Assembly language that prints the length of text

; using call by reference procedure

.586

option segment:use16

.model small

print_msg Macro msg

mov dx,offset msg

mov ah,9

int 21h

ENDM

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 48

Example2 : Register Method

.DATA

text db 'Assembly$'

txt db 'The length of text <$'

res db '> is $'

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 49

Example2 : Register Method

.code

.startup

mov bx,offset text

print_msg txt

print_msg text

print_msg res

call txt_len

add dl,'0'

mov ah,2

int 21h

.exit

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 50

Example2 : Register Method

txt_len PROC

sub dl,dl

loops:

mov dh,[bx]

cmp dh,'$'

je str_len_done

inc bx

inc dl

jmp loops

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 51

Example2 : Register Method

str_len_done:

RET ; return to caller.

txt_len ENDP

end

2005

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 5: Page 52

Pros and Cons of the Register Method

• Advantages Convenient and easier Faster

• Disadvantages Only a few parameters can be passed using the register

method– Only a small number of registers are available

Often these registers are not free– freeing them by pushing their values onto the stack

negates the second advantage