Unsigned integers in assembly
Asked Answered
V

1

6

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?

Vadose answered 14/3, 2017 at 20:9 Comment(0)
S
10

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.

There 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", it makes no sense. It can only appear negative if you chose to interpret it that way.

Salty answered 14/3, 2017 at 20:22 Comment(4)
Is there a way for the command (in gdb) info registers to display as unsigned instead?Vadose
A cast should work: print (unsigned)$eax. Or possibly print/uSalty
...info registers seems a bit less flexible. I don't see any obvious equivalent of /u for itSalty
it goes beyond signed and unsigned. Registers hold bits, these bits mean ABSOLUTELY nothing until you use them. An address/pointer for example in a high level language may require an offset that may be an add instruction during the add instruction those bits you think are a pointer are just some operand into an add. they are not a pointer/addres until used to load or store something. The bits are only relevant to the programmer, they mean nothing to the hardware/chip/instruction set.Mientao

© 2022 - 2024 — McMap. All rights reserved.