News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Multiplication Procedure Wrong

Started by jwill22, March 25, 2011, 04:34:35 PM

Previous topic - Next topic

jwill22

The addition and subtraction procedure is ok, but when I do multiplication, it works the first time..then when I try to multiply two new numbers, I get the same answer as the first two numbers that were multiplied. I have to do multiplication and division using only the stack and addition and subtraction. So I can't use Mul or Div instructions. Here's what it looks like


TITLE BooleandCalulator(#1)(Pg.227 # 8)
;By: Justin Williams
;Instructor: Dr. Jessie Walker
;Class : 10:00 - 10:50



INCLUDE Irvine32.inc









.data

displayMenu BYTE " Calculator...choose which operation you want to do",0dh,0ah ;The menu for user to view and choose
BYTE 0dh,0ah
BYTE "1. Addition"     ,0dh,0ah
BYTE "2. Subtraction"      ,0dh,0ah
BYTE "3. Multiplication"       ,0dh,0ah
BYTE "4. Division"     ,0dh,0ah
BYTE "5. Exit program",0
;Messages
;**********************************************************************
addMessage BYTE "Enter two Numbers to add",0
subMessage  BYTE " Enter two numbers to subtract",0
mulMessage BYTE " Enter two numbers to multiply",0
divMessage BYTE " Enter the numbers for division",0
tryAgain BYTE " Do you want to start over (y/n)? "

counter DWORD 0
sum DWORD 0
temp DWORD 0

;********************************************************************************
.code
main PROC
.repeat
L0: mov edx, OFFSET displayMenu
call WriteString ;Write message to screen
jmp L1

L1: call Crlf
call ReadInt
.if(eax == 1) ;if eax == 1 call the and procedure

mov edx,OFFSET addMessage
call WriteString
call Crlf
call ReadDec
push eax
call ReadDec
mov ebx,eax
push ebx
call addProc
call WriteDec

.elseif(eax == 2)
mov edx,OFFSET subMessage
call WriteString
call Crlf
call ReadDec
push eax
call ReadDec
mov ebx,eax
push ebx
call subProc
call WriteInt


.elseif(eax == 3)
mov edx,OFFSET mulMessage
call WriteString
call Crlf
call ReadDec
push eax
call ReadDec
mov ebx,eax
push ebx
call mulProc
call WriteDec


.elseif(eax == 4)
mov edx,OFFSET divMessage
call WriteString
call Crlf
call ReadDec
push eax
call ReadDec
mov ebx,eax
push ebx
call divProc
call WriteInt



.endif
.UNTIL(eax==5)
exit

main ENDP

addProc PROC

push ebp
mov ebp,esp
mov eax,[ebp + 12]
add eax,[ebp + 8]

;call WriteInt
;add edx,[ebp +20 ]
;mov eax,edx
;call WriteDec
pop ebp
ret 8
addProc endp

subProc PROC

push ebp
mov ebp,esp
mov eax,[ebp + 12]
sub eax,[ebp+8]
pop ebp
ret 8

subProc endp

mulProc PROC
push ebp
mov ebp,esp
mov eax,[ebp + 12]
mov ebx,[ebp + 8]

L1: CMP counter,ebx
jl L2
jmp L3

L2: add sum,eax
    inc counter
    jmp L1


inc counter

L3: mov eax,sum
    ;call WriteInt

pop ebp

ret 8

mulProc endp


divProc PROC

push ebp
mov ebp,esp
mov eax,[ebp + 12]
mov ebx,[ebp + 8]

.WHILE(temp < eax)
add temp,ebx
inc counter
.ENDW

mov eax,counter

call WriteDec

pop ebp


ret 8

divProc endp






exit


END main


Thanks and if I didn't explain good enough or if its something anyone doesn't understand I'll be more than happy to answer :cheekygreen:

qWord

hi,
- AFAIKS there is no need to set up the stackframe inside you procedures - this is allready done by masm. However I don't know what happen inside Irvine32.inc
- you do not initialize the (global) variables counter and sum
- keep in mind, that the x86 instruction set also has division and multiplication instructions
FPU in a trice: SmplMath
It's that simple!

jwill22

That's Ok I got it...I just had to initialize my counter and  sum variables...like u said