Processing and Storing Functions in EAX Register
INCLUDE Irvine32.inc
.DATA
prompt DB "Enter a value: ",0
message DB "The absolute value is: ", 0
result DD 0
.CODE
main PROC
; Print prompt for value
mov edx, OFFSET prompt ; load string address
call WriteString ; print the string
call ReadInt ; Read a number
push eax ; pass value to function
call abs ; abs(value)
add esp, 4 ; remove value from stack
mov [result], eax ; save result in variable
; Print result message
mov edx, OFFSET message ; load string address
call WriteString ; print the string
mov eax, [result] ; load result
call WriteInt ; print the number
call CrLf ; move to a new line
call WaitMsg ; wait until user presses a key
exit ; exit the program
main ENDP
; Function abs
abs PROC
push ebp ; save frame pointer
mov ebp, esp ; create new stack frame
mov eax, DWORD PTR[ebp + 8] ; load argument value from stack
cmp eax, 0 ; compare value with zero
jge return ; if it's positive, return without changes
neg eax ; else convert to positive by inverting the value
return:
pop ebp ; restore frame pointer
ret ; return to caller
abs ENDP
END main
INCLUDE Irvine32.inc
.DATA
prompt DB "Enter a value: ",0
oddMsg DB "The value is odd!", 0
evenMsg DB "The value is even!", 0
.CODE
main PROC
; Print prompt
mov edx, OFFSET prompt ; load string address
call WriteString ; print the string
call ReadDec ; Read a number
push eax ; pass value to function
call isEven ; isEven(value)
add esp, 4 ; remove value from stack
cmp eax, 0 ; if the return value is 0,
je odd ; is odd
; else, print is even
mov edx, OFFSET evenMsg ; load string address
call WriteString ; print the string
jmp done ; terminate program
odd:
; Print is odd
mov edx, OFFSET oddMsg ; load string address
call WriteString ; print the string
done:
call CrLf ; move to a new line
call WaitMsg ; wait until user presses a key
exit ; exit the program
main ENDP
; Function isEven
isEven PROC
push ebp ; save frame pointer
mov ebp, esp ; create new stack frame
mov eax, 0 ; return 0 by default
test DWORD PTR[ebp + 8], 1 ; test stack argument lsb bit to see if the value is odd
jne return ; if it's not zero, it's odd, return
mov eax, 1 ; else, is even, return 1
return:
pop ebp ; restore frame pointer
ret ; return to caller
isEven ENDP
END main
INCLUDE Irvine32.inc
.DATA
promptx DB "Enter a value for x: ",0
prompty DB "Enter a value for y: ",0
message DB "x to the y power is: ", 0
x DD 0
y DD 0
result DD 0
.CODE
main PROC
; Print prompt for x
mov edx, OFFSET promptx ; load string address
call WriteString ; print the string
call ReadDec ; Read a number
mov [x], eax ; save number in variable
; Print prompt for y
mov edx, OFFSET prompty ; load string address
call WriteString ; print the string
call ReadDec ; Read a number
mov [y], eax ; save number in variable
push DWORD PTR[y] ; pass y value to function
push DWORD PTR[x] ; pass x value to function
call power ; power(x, y)
add esp, 8 ; remove x and y values from stack
mov [result], eax ; save result in variable
; Print result message
mov edx, OFFSET message ; load string address
call WriteString ; print the string
mov eax, [result] ; load result
call WriteDec ; print the number
call CrLf ; move to a new line
call WaitMsg ; wait until user presses a key
exit ; exit the program
main ENDP
; Function power
power PROC
push ebp ; save frame pointer
mov ebp, esp ; create new stack frame
push ecx ; save registers
push edx
mov eax, 1 ; load 1 to return 1 if y is zero
mov ecx, DWORD PTR[ebp + 12] ; load value of y
cmp ecx, 0 ; see if y is zero
je return ; if zero return 1
pow_loop:
mul DWORD PTR[ebp + 8] ; multiply x by previous power
loop pow_loop ; decrement y and repeat loop if y is not zero
return:
pop edx ; restore registers
pop ecx
pop ebp ; restore frame pointer
ret ; return to caller
power ENDP
END main