assemblyx86masm32

MASM32 problems with imul when multiply two negative numbers


I mooved to register al number -100, now in al stored 9C in hexademical, the same thing I did with register bl, so in bl stored 9C too. And here is the problem, when I do:

imul ebx

I get 00005F10h in register eax, it's 24336 in decimal instead of 10000 that I was expected to get. What am I doing wrong? Here is the part of code where I do this multiplication:

mov eax, 0
mov ebx, 0
mov ecx, 0
mov al, [sbAval+esi]
mov bl, [sbAval+esi]

power:
imul ebx
cmp ecx, 2
je powerDone
inc ecx
jmp power

sbAval is SBYTE data type

Here I have 9C stored in eax and ebx registers before imul

And here is registers after imul registers after imul


Solution

  • -100 in 32-bit is not 0x9c, it's 0xffffff9c. If you put 0x9c in the registers (rather than the actual integer -100), then you have there 156. It may not surprise you that 156*156=24336. This is a likely source of confusion.

    Following your edit, it seems like you won't be able to do this naturally using an SBYTE, due to the nature of the instructions padding the shorter data type with 0s (and treating it as a positive number). Use movsx to retain the sign while moving the SBYTE to a register.