Assembly language - masm32 - multiplying
Asked Answered
S

1

0

I am multiplying 3 numbers which works good even with a carry. I want to add a 4th number to multiply just for learning purposes.

After i multiply 3 numbers i shift into EDX and print. Works great. After i add a 4th number i think i am multiplying 32bit x 32bit? So it stores into EDX:EAX?

Would i then need to shift EAX into EDX so they are together to print? Im not sure if i am doing it right for the 4th number?

.data?
  num1 dd ?
  num2 dd ?
  num3 dd ?
  num4 dd ?

.data
sum dd 0
prod dd 0
prod2 dd 0

here are the prompts

mov EAX, sval(input("Enter first number: "))
mov num1, EAX
mov EAX, sval(input("Enter second number: "))
mov num2, EAX
mov EAX, sval(input("Enter third number: "))
mov num3, EAX

mov EAX, sval(input("Enter fourth number: "))
mov num4, EAX

here is the logic

mov EAX, num1
mov EBX, num2
mul BL                 ; 8 bit x 8 bit ----> AX / 16bit

mov EBX, num3
mul BX     ; 16bit x 16bit --->DX:AX

shl EDX, 16  ; shift low to high             ;high / low in EDX
mov DX, AX     ; mov in all reg

mov ECX, num4   ; 32bit x 32bit ---> EDX:EAX
mul CX
mov prod2, EAX  ; for printing

I am not sure if i should move num4 into ECX and multiplying by CX Should i be multiplying by 32bit instead? What am i doing wrong for the 4th number? Thank you

Sheliasheline answered 16/7, 2012 at 22:16 Comment(3)
Is this any different from your previous question?Innate
My previous post was different. My side question is the same.Sheliasheline
Is my num4 multiplication correct? If so, how would i go about handling EDX:EAX so i can print it out correctly? Thank youSheliasheline
J
0

32-bit multiplication uses EAX register. Your code for the third multiplication is 16-bit since your MUL operand uses 16-bit register, so the multiplication is AX x CX. 32-bit multiplication needs 32-bit operand, so in your code, you need to use ECX rather than CX. Also, the preparation for 32-bit multiplication is incomplete, since the value is still placed in EDX register.

So the code should be like this:

mov EAX, num1
mov EBX, num2
mul BL         ; 8 bit x 8 bit ---> AX / 16bit

mov EBX, num3
mul BX         ; 16bit x 16bit ---> DX:AX

shl EDX, 16    ; shift low to high  ;high / low in EDX
mov DX, AX     ; mov in all reg
mov EAX, EDX   ; prepare EAX for 32bit x32bit

mov ECX, num4
mul ECX        ; 32bit x 32bit ---> EAX x ECX ---> EDX:EAX
mov prod2, EAX ; for printing

Be aware that 32-bit multiplication might result a 64-bit value in EDX:EAX, so make sure the EDX register is taken into account by your printing function.

Jerrylee answered 18/7, 2012 at 0:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.