You are on page 1of 5

CALL and RET Instructions CALL-RET Example (1 of 2)

main PROC
• The CALL instruction calls a procedure 0000025 is the offset of the
00000020 call MySub
00000025 mov eax,ebx
• pushes offset of next instruction on the stack instruction immediately
.
following the CALL
• copies the address of the called procedure into EIP instruction
.
main ENDP
• The RET instruction returns from a procedure
• pops top of stack into EIP MySub PROC
00000040 is the offset of 00000040 mov eax,edx
the first instruction inside .
MySub .
ret
MySub ENDP

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 44 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 45

CALL-RET Example (2 of 2) Nested Procedure Calls


main PROC
.
.
00000025 ESP 00000040 call Sub1 By the time Sub3 is called, the
The CALL instruction
pushes 00000025 onto
exit stack contains all three return
main ENDP
the stack, and loads EIP addresses:
00000040 into EIP Sub1 PROC
.
.
call Sub2
(ret to main)
ret
Sub1 ENDP (ret to Sub1)
Sub2 PROC
(ret to Sub2) ESP
.
The RET instruction .
00000025 ESP 00000025 call Sub3
pops 00000025 from the ret
stack into EIP Sub2 ENDP
EIP
Sub3 PROC
.
.
ret
(stack shown before RET executes) Sub3 ENDP

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 46 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 47

Local and Global Labels Procedure Parameters (1 of 3)


A local label is visible only to statements inside the same
procedure. A global label is visible everywhere.
• A good procedure might be usable in many
different programs
main PROC
jmp L2 ; error • but not if it refers to specific variable names
L1:: ; global label
exit • Parameters help to make procedures flexible
main ENDP
because parameter values can change at runtime
sub2 PROC
L2: ; local label
jmp L1 ; ok
ret
sub2 ENDP

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 48 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 49

1
Procedure Parameters (2 of 3) Procedure Parameters (3 of 3)
The ArraySum procedure calculates the sum of an array. It
This version of ArraySum returns the sum of any doubleword
makes two references to specific variable names:
array whose address is in ESI. The sum is returned in EAX:
ArraySum PROC
mov esi,0 ; array index ArraySum PROC
mov eax,0 ; set the sum to zero ; Receives: ESI points to an array of doublewords,
mov ecx,LENGTHOF myarray ; set number of elements ; ECX = number of array elements.
; Returns: EAX = sum
L1: add eax,myArray[esi] ; add each integer to sum ;-----------------------------------------------------
add esi,4 ; point to next integer mov eax,0 ; set the sum to zero
loop L1 ; repeat for array size
L1: add eax,[esi] ; add each integer to sum
mov theSum,eax ; store the sum add esi,4 ; point to next integer
ret loop L1 ; repeat for array size
ArraySum ENDP
ret
ArraySum ENDP
What if you wanted to calculate the sum of two or three arrays
within the same program?

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 50 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 51

Flowchart Symbols
ArraySum Procedure

begin

Flowchart for
• The following symbols are the basic building blocks
of flowcharts: the ArraySum push esi, ecx

begin / end
Procedure eax = 0

manual input add eax,[esi] push esi


push ecx
mov eax,0

process (task) add esi, 4 AS1:


display
add eax,[esi]
add esi,4
loop AS1
ecx = ecx  1
pop ecx
yes pop esi
decision
procedure
yes
call ecx > 0?

no no

pop ecx, esi

end

(Includes two symbols not listed on page 166 of the book.)


Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 52 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 53

Your turn . . . . . . (Solution) begin

Draw a flowchart that expresses the following


pseudocode: input exam grade

input exam grade from the user


no yes
if( grade > 70 ) grade > 70?

display "Pass"
else display "Fail" display "Pass"

display "Fail"
endif

end

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 54 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 55

2
Your turn . . . USES Operator
• Lists the registers that will be preserved
• Modify the flowchart in the previous slide to allow the ArraySum PROC USES esi ecx
mov eax,0 ; set the sum to zero
user to continue to input exam scores until a value of etc.
–1 is entered
MASM generates the code shown in gold:
ArraySum PROC
push esi
push ecx
.
.
pop ecx
pop esi
ret
ArraySum ENDP

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 56 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 57

When not to push a register


The sum of the three registers is stored in EAX on line (3), but
the POP instruction replaces it with the starting value of EAX on
line (4):

SumOf PROC ; sum of three integers


push eax ; 1
add eax,ebx ; 2
add eax,ecx ; 3
pop eax ; 4
ret
SumOf ENDP

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 58 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 59

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 60 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 61

3
Program Design Using Procedures

• Top-Down Design (functional decomposition)


involves the following:
• design your program before starting to code
• break large tasks into smaller ones
• use a hierarchical structure based on procedure calls
• test individual procedures separately

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 62 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 63

Integer Summation Program (1 of 4) Procedure Design (2 of 4)


Main
Description: Write a program that prompts the user for
Clrscr ; clear screen
multiple 32-bit integers, stores them in an array,
calculates the sum of the array, and displays the sum on PromptForIntegers
the screen. WriteString ; display string
ReadInt ; input integer
Main steps: ArraySum ; sum the integers
• Prompt user for multiple integers DisplaySum
• Calculate the sum of the array WriteString ; display string
• Display the sum WriteInt ; display integer

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 64 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 65

Structure Chart (3 of 4) Sample Output (4 of 4)

Summation
Program (main)

Enter a signed integer: 550


Clrscr PromptForIntegers ArraySum DisplaySum
Enter a signed integer: -23
Enter a signed integer: -96
WriteString ReadInt WriteString WriteInt
The sum of the integers is: +431

gray indicates • View the stub program


library
procedure
• View the final program

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 66 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 67

4
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 68 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 69

The End

70 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 71