x86 assembly - MASM32 - multiplying 3 numbers
Asked Answered
B

1

1

I have a program that multiplies 3 numbers and i am trying to understand. I have some questions and i am hoping someone can explain whats going on with the program and tell me if i am on the right track. I understand i have more than one question so i am sorry about that.

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

.data
sum dd 0
prod dd 0

.code

start:



main proc


mov EAX, sval(input("Enter a number: "))
mov num1, EAX
mov EAX, sval(input("Enter a number: "))
mov num2, EAX
mov EAX, sval(input("Enter a number: "))
mov num3, EAX





mov EAX, num1
mov EBX, num2
mul BL                 

mov EBX, num3
mul BX              

mov prod, EDX

this has me confused...

mov EBX, num3
mul BX

so, we are storing num3 into BL? but since the result of num1 and num2 is 16 bit and stored into AX we mul BX? instead of BL? but isnt num3 in BL?

im sorry there isnt one specific question. If my logic is incorrect or close can you explain whats going on piece by piece and why?

Thank you

Bonheur answered 15/7, 2012 at 0:10 Comment(4)
This code definitely doesn't "multiply 3 numbers", it does something weird and confusing. Why not use IMUL?Homoousian
It actually does work even with the worst case scenerio multiplying by 255Bonheur
So it's actually "enter a number between 0 and 255"? Otherwise there's just no way it's going to work.Homoousian
If you add any number between 0 - 255 it works.Bonheur
P
3

When you do an 8-bit multiplication like mul bl, it takes al, multiplies it by the specified input, and puts the result in ax.

When you do a 16-bit multiplication like mul bx, it takes ax, multiplies it by the specified input, and puts the result in dx:ax (i.e., the 16 most significant bits of the result in dx, and the 16 least significant bits of the result in ax).

(Just for completeness): if you do a 32-bit multiplication like mul ebx, it multiplies eax by ebx, and puts the result in edx:eax (offhand I don't remember for sure, but I'd guess 64-bit multiplication works about the same way).

As far as BL vs. BX goes (or AL/AH/AX, etc.), what you have aren't really separate registers -- AL is really the 8 least significant bits of AX. AH is the 8 most significant bits of AX. BL is the 8 least significant bits of BX, and BH is the 8 most significant bits of BX (and so on for CL/CH/DL/DH).

For example, if you execute code like:

xor bx, bx
mov bl, 1

BH will then equal 0 (set to zero by the xor bx, bx), BL will equal 1 (set by the mov) and bx will also equal 1 (because it now contains the bit pattern 00000000 00000001).

Patrizius answered 15/7, 2012 at 0:25 Comment(2)
if i was to add one more number would that be a 32-bit multiplication? if so then the result would be in edx:eax? would i then shift eax into edx?Bonheur
Yes, it's the same for 64-bit. rdx:raxJamille

© 2022 - 2024 — McMap. All rights reserved.