Professional Documents
Culture Documents
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 46 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 47
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
no no
end
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
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 62 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 63
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 64 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 65
Summation
Program (main)
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