Chapt 05
Chapt 05
Chapter Overview
INCLUDE Irvine32.inc
.code
mov eax,1234h ; input argument
call WriteHex ; show hex number
call Crlf ; end of line
Procedure Purpose
Calling & Return Arguments
Example of usage
.code
call Clrscr
mov eax,500
call Delay
call DumpRegs
Sample output:
EAX=00000613 EBX=00000000 ECX=000000FF EDX=00000000
ESI=00000000 EDI=00000100 EBP=0000091E ESP=000000F6
EIP=00401026 EFL=00000286 CF=0 SF=1 ZF=0 OF=0
.data
str1 BYTE "Assembly language is easy!",0
.code
mov edx,OFFSET str1
call WriteString
call Crlf
.data
str1 BYTE "Assembly language is easy!",0Dh,0Ah,0
.code
mov edx,OFFSET str1
call WriteString
IntVal = 35
.code
mov eax,IntVal
call WriteBin ; display binary
call Crlf
call WriteDec ; display decimal
call Crlf
call WriteHex ; display hexadecimal
call Crlf
Sample output:
0000 0000 0000 0000 0000 0000 0010 0011
35
23
.data
fileName BYTE 80 DUP(0)
.code
mov edx,OFFSET fileName
mov ecx,SIZEOF fileName – 1
call ReadString
.code
mov ecx,10 ; loop counter
.data
str1 BYTE "Color output is easy!",0
.code
mov eax,yellow + (blue * 16)
call SetTextColor
mov edx,OFFSET str1
call WriteString
call Crlf
• Runtime Stack
• PUSH Operation
• POP Operation
• PUSH and POP Instructions
• Using PUSH and POP
• Example: Reversing a String
• Related Instructions
* SP in Real-address mode
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
35
PUSH Operation (1 of 2)
• A 32-bit push operation decrements the stack pointer
by 4 and copies a value into the location pointed to
by the stack pointer.
• PUSH syntax:
• PUSH r/m16
• PUSH r/m32
• PUSH imm32
• POP syntax:
• POP r/m16
• POP r/m32
• Creating Procedures
• Documenting Procedures
• Example: SumOf Procedure
• CALL and RET Instructions
• Nested Procedure Calls
• Local and Global Labels
• Procedure Parameters
• Flowchart Symbols
• USES Operator
sample PROC
.
.
ret
sample ENDP
;---------------------------------------------------------
SumOf PROC
;
; Calculates and returns the sum of three 32-bit integers.
; Receives: EAX, EBX, ECX, the three integers. May be
; signed or unsigned.
; Returns: EAX = sum, and the status flags (Carry,
; Overflow, etc.) are changed.
; Requires: nothing
;---------------------------------------------------------
add eax,ebx
add eax,ecx
ret
SumOf ENDP
main PROC
00000020 call MySub
0000025 is the offset of the
00000025 mov eax,ebx
instruction immediately
.
following the CALL
instruction .
main ENDP
MySub PROC
00000040 is the offset of 00000040 mov eax,edx
the first instruction inside .
MySub .
ret
MySub ENDP
main PROC
jmp L2 ; error
L1:: ; global label
exit
main ENDP
sub2 PROC
L2: ; local label
jmp L1 ; ok
ret
sub2 ENDP
ArraySum PROC
mov esi,0 ; array index
mov eax,0 ; set the sum to zero
mov ecx,LENGTHOF myarray ; set number of elements
ArraySum PROC
; Receives: ESI points to an array of doublewords,
; ECX = number of array elements.
; Returns: EAX = sum
;-----------------------------------------------------
mov eax,0 ; set the sum to zero
ret
ArraySum ENDP
Main steps:
• Prompt user for multiple integers
• Calculate the sum of the array
• Display the sum
There is also an IDIV instruction for Signed Divide, but the registers
used are the same as the above.
The user must be aware of the sizes of the variables being used.
For example, if AX contains 4000 and DIV 10 is used, then the
quotient is 400, which is too big to fit into the AL register.