Unsigned integers in assembly

frillybob picture frillybob · Mar 14, 2017 · Viewed 7.5k times · Source

Brand new to assembly need some help with unsigned arithmetic. Converting from a C program is that means anything.

Using:

Linux

NASM

x86 (32 Bit)

I want to read in a number from the user. I want this number to be unsigned. When I enter a number above the signed integer limit and use info registers, I notice that my register storing that is negative which means an overflow happened. (Obviously number entered is below max unsigned int) How do I treat this register as unsigned so I can do comparisons and jump based on the result?

Answer

user2404501 picture user2404501 · Mar 14, 2017

In assembly, there are no distinct signed and unsigned types. A register holds a value that can be either signed or unsigned, depending on how you look at it.

The are instructions that are consistent with the signed interpretation (jg, jl, etc.) and instructions that are consistent with the unsigned interpretation (ja, jb, etc.) The cmp instruction works for both - it sets flags that can be used by instructions like jl to jump based on the signed interpretation, and flags that can be used by instructions like jb to jump based on the unsigned interpretation. Whichever flags you're not using, you just ignore.

So when you say "my register storing that is negative", that's nonsense. It can only appear negative if you chose to interpret it that way.