News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

MASM problen with calling a procedure from A PROC that pulls from another proc

Started by c1nteqc2, July 19, 2016, 12:22:11 PM

Previous topic - Next topic

c1nteqc2

I am writing a program using masm and the Irvine library in VS 2015. I am just trying to learn how to call a procedure from a procedure that pulls from another procedure. I have a AddProc, MultPROC and PowProc. The use enters 2 numbers and first i call the addProc and store the sum then I use the MultProc by using the add procedure to get my answer. Then the PowProc calls on the multiply procedure that calls on the AddProc to get the answer for the power

here is my code and I can get the sum and product right but i get the wrong answer for the power

ExitProcess proto, dwExitCode:dword
.data

prompt BYTE "Enter a pos integer", 0
sumTotal BYTE "The sum is:", 0
prodTotal BYTE "The product is", 0
powTotal  BYTE "The power total is", 0
num1 DWORD 0
num2 DWORD 0
sum  DWORD 0
prod DWORD 0
pow  DWORD 0
tmp  DWORD 0

.code
main PROC

mov edx, OFFSET prompt     ;// get integers
call WriteString
call ReadInt
mov[num1], eax
call WriteString
call ReadInt
mov[num2], eax

mov eax, [num1] ;// calculations
mov ebx, [num2]
call CalcSum
mov[sum], eax
call CalcMul
mov[prod], eax
call CalcPow
mov [pow], eax

mov edx, OFFSET sumTotal ;// display sum total
call WriteString
mov  eax, [sum]
call WriteInt
call Crlf

mov  edx, OFFSET prodTotal ;// display product total
call WriteString
mov  eax, [prod]
call WriteInt
call Crlf

mov edx, OFFSET powTotal ;// display power results
call WriteString
mov  eax, [pow]
call WriteInt
call Crlf


exit
main ENDP



; --------------------------------------------------------
CalcSum PROC
; Calculates the sum of two integers
; Receives: nothing
; Returns: eax = sum
; --------------------------------------------------------

add  eax, ebx

ret 0
CalcSum ENDP

; ----------------------------------------------------------
CalcMul PROC
; Calculates the prod of two integers
; Recieves: ECX = count
; Returns: EAX = prod
; ---------------------------------------------------------- -


xor  eax, eax
mov  ebx, [num1]
mov  ecx, [num2]
LOOP_MUL :
call CalcSum
LOOPNZ LOOP_MUL
mov [prod], eax
ret
CalcMul ENDP

; ------------------------------------------------------------ -
CalcPow PROC
; Calculates the power of two integers
; Recieves: ECX = count
; Returns: EAX = power
; --------------------------------------------------------------
 
mov eax, [num1];// add result x num2 times till end
mov[tmp], eax

mov esi, [num2]

POW_LOOP :
    xor eax, eax
mov ebx, [tmp]
mov ecx, [num1]
call CalcMul;// Call Sum proc
imul eax
        dec esi
     
jnz POW_LOOP
mov[pow], eax
ret 0
CalcPow ENDP 
END Main


qWord

You might first correct the comment blocks (input, output) of the functions and make clear what variables are read and written to.
Actual there are better ways to pass parameters to functions ... should be explained in the book.
MREAL macros - when you need floating point arithmetic while assembling!