Reputation: 69
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
Upvotes: 0
Views: 357
Reputation: 25980
-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.
Upvotes: 3
Reputation: 7806
Assuming the two's-complement system, 0x9C
is negative when stored in a single byte, and positive when stored using two or more bytes.
This is because in a two's-complement system, the most significant bit (i.e. the left-most one) signifies the sign, and the 1
makes it negatives.
When you extend the space to 32 bits where the MSB is 0
- the number is "processed" as positive.
One possible solution is to store 100
in AL
and BL
(after clearing EAX
and EBX
, like you do in your code), and then negate EAX
and BAX
. E.g. NEG EAX
( How to convert a positive number to negative in assembly )
Upvotes: 0