0
votes

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

2
I found out that if we convert 9C as not signed number to decimal it's 156 and if we multiply 156*156 it equals 24336, exactly what I get, but what should I do with that? How to multiply two negative numbers in masm32?kstamax
You need to sign-extend, try movsx eax, byte ptr [sbAval+esi] in place of mov al, ..., and likewise movsx ebx, ...Nate Eldredge
Yeah it did work, thanks a lotkstamax
Why are you using widening one-operand imul in the first place? Are you actually using the high half in EDX? Normally you'd do imul eax, ebx (or use any registers, like EDX for the copy, so you don't have to save/restore a call-preserved register like EBX). Also, why load from memory twice? Just mov edx, eax after doing one movsx load. (If you didn't need to multiply repeatedly, you could have used imul al to square into AX.)Peter Cordes
No, I'm not, I did like it was in our lab example. I didn't know that doing imul eax, ebx won't affect EDX. And with thing about memory I agree too. Thanks I will edit my code.kstamax

2 Answers

3
votes

-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.

0
votes

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 )