# ML ASM Multiplication

My boss gave me a "simple" project to do. Problem is I don't know ASM. But... is the boss... I have to calculate the following in MASM:

n * 16^x

It has to be in a function which we pass n and x and get the result back. I have no idea how to do that. Can anyone help?
Thanks!

• : My boss gave me a "simple" project to do. Problem is I don't know ASM. But... is the boss... I have to calculate the following in MASM:
:
: n * 16^x
:
: It has to be in a function which we pass n and x and get the result back. I have no idea how to do that. Can anyone help?
: Thanks!
:

Your boss maybe want you to learn assembling...
[code]
mov eax,n
mov ebx,x
mov ecx,ebc
shr eax,8 ;n=n*16
mulLoopStart:
mul ebx ;n=n*x
sub ecx,1
jnz mulLoopStart

;if not overflow, wich isn't checked the right result will be in eax:edx
[/code]

Happy coding wishes
the one and only
[b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]

• Hi,

[code]
shr eax,8 ;n=n*16
[/code]

The above is [italic]n = n / 16[/italic].

Use:

[code]
shl eax,8 ;n=n*16
[/code]

Cheers
• Hi,

[italic]n * 16^x[/italic] means [italic]n[/italic] * (16 to the power of [italic]x[/italic]), doesn't it?

[code]
mov eax, 16
mov ecx, x ; x >= 1 or this will freeze the program while it does 4294967295 loops. But why would you want 0?
mov ebx, 16
mul_loop:
dec ecx
jz end_of_mul_loop
mul ebx
jmp mul_loop
end_of_mul_loop:
mov ebx, n
mul ebx
[/code]

The result of the calculation is in EDX:EAX.

I haven't tested the above but it looks alright.

Cheers
• : Hi,
:
: [code]
: shr eax,8 ;n=n*16
: [/code]
:
: The above is [italic]n = n / 16[/italic].
:
: Use:
:
: [code]
: shl eax,8 ;n=n*16
: [/code]
:
: Cheers
:
[green]
2^8 power is equal to 256 not 16. Did you mean 2^4?
[/green]

• Hi,

Yep, I did mean 4.

Cheers
• :But why would you want 0?

Anything to the power of 0 is equal to 1, just to make it clear.
• : Hi,
:
: Yep, I did mean 4.
:
: Cheers
:
Sorry.

I sometimes mix them up.
I was in a hurry when I wrote that.
• Green lines are modified code and added ones are red. I'm pretty sure this would remove the exponent of zero condition. There is an x >= 0 condition though since we're working with integers here and representing the result as fixed point or a fraction (e.g. numerator = EAX and denominator = EBX) is a little weird. I also generalized the code for any power so it would be worth it to modify over the shifting scheme which is faster given powers of two.

[code]
[green] mov eax, 1[/green]
mov ecx, x
mov ebx, b ; b = base e.g. returned value = n * b ^ x
[red] jecxz end_of_mul_loop[/red]
mul_loop:
dec ecx
jz end_of_mul_loop
mul ebx
jmp mul_loop
end_of_mul_loop:
mov ebx, n
mul ebx
[/code]
:
: The result of the calculation is in EDX:EAX.
:
: I haven't tested the above but it looks alright.
:
: Cheers
:
• Hi,

Good modifications to the code, but something broke in the process. The code, with your modifications, should read:

[code]
mov eax, 1
mov ecx, x
mov ebx, b ; b = base e.g. returned value = n * b ^ x
jecxz end_of_mul_loop

mul_loop:
mul ebx
dec ecx ; Moved this down here or b ^ 1 will equal 1 rather than b.
jnz mul_loop

end_of_mul_loop:
mov ebx, n
mul ebx
[/code]

Cheers