Bob
Bob

Reputation: 101

How to use unsigned ints in Assembly

I'm writing a small program in assembly and i need to calculate an int that is a power of two and uses the last bit in a register. Let's say i need to calculate 2^31 and store the result in ebx register.

How can I treat this int as unsigned for arithmetic operations?

Upvotes: 0

Views: 3401

Answers (2)

Kaz
Kaz

Reputation: 58568

Data in many assembly languages (i.e instruction set architectures) is typeless, except to the extent that it has a size: i.e. 8 bits is a "type" different from 16.

Your EBX register is neither signed nor unsigned; it's just a bank of 32 bits.

The semantics of the bits (what the bits mean) depends on the operation being applied, not on any kind of declared type like in a higher level language.

x86 distinguishes "logical shifts" from "arithmetic shifts".

So to calculate 1 << 31 you want to use a logical shift left (SAL). For shifting signed quantities use the arithmetic shifts.

Upvotes: 1

MiMo
MiMo

Reputation: 11953

To compute a power of two you use a shift left operation - something like

SAL    EAX,31

Addition and subtraction operations are the same for signed and unsigned values - i.e. it is only when you do something with the result (like displaying it) that you have to consider it signed or unsigned

For division and multiplication there are different instructions for signed (IDIV, IMUL) and unsigned (DIV, MUL)

Upvotes: 2

Related Questions