NASM byte is signed?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
greyOne
Member
Member
Posts: 58
Joined: Sun Feb 03, 2013 10:38 pm
Location: Canada

NASM byte is signed?

Post by greyOne »

I've run into a but of a problem with NASM, it seems to complain that:

Code: Select all

push byte 128
Exceeds values storable in a byte.
Since an unsigned 8 bit integer can store values up to 0xFF, I think NASM is treating bytes as signed.
All it's doing is spitting out a bunch of warnings, the code itself runs fine.

However, the warnings are incredibly annoying.
I try to never have any (-Wall, -Wextra and -Werror help).

Is there any concept of unsignedness in NASM? Google hasn't been very helpful,
And the Wiki page mentions nothing on the matter.
All else aside, having a way of getting NASM to shut up about the warning would be nice.
Cheers.
TomT
Member
Member
Posts: 42
Joined: Sat Mar 15, 2008 7:20 am
Location: Wisconsin, USA
Contact:

Re: NASM byte is signed?

Post by TomT »

Your question might best be posted to the NASM forum
http://forum.nasm.us/

In 16 bit assembly I push "words"
In 32 bit assembly I push "dwords"
I have no knowledge of 8 bit assembly.

TomT
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: NASM byte is signed?

Post by Griwes »

@OP: Bytes are just bytes.

*Integer literals*, on the other hand...


@up: your post gave me some amount of WTFs. "8 bit assembly"? Was there any mention of 8 bit assembly?
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: NASM byte is signed?

Post by bluemoon »

According to the x86 manual, push imm8 do sign extend imm8 to current operand size. 128 is indeed over the signed range and it's reasonable for such warning. See volume 2 for detail.
greyOne
Member
Member
Posts: 58
Joined: Sun Feb 03, 2013 10:38 pm
Location: Canada

Re: NASM byte is signed?

Post by greyOne »

bluemoon wrote:According to the x86 manual, push imm8 do sign extend imm8 to current operand size. 128 is indeed over the signed range and it's reasonable for such warning. See volume 2 for detail.
Makes sense.
In any case, I got around the whole thing by using equivalent signed values (bitwise).
Thank for the help.

EDIT:
PS. If any one runs into the issue; 2's compliment.
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: NASM byte is signed?

Post by Nessphoro »

Sooo...why not just push a dword?
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: NASM byte is signed?

Post by Mikemk »

Nessphoro wrote:Sooo...why not just push a dword?
Why waste stack space and possibly trash your stack if the function being called expects a byte?
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: NASM byte is signed?

Post by Nessphoro »

m12 wrote:
Nessphoro wrote:Sooo...why not just push a dword?
Why waste stack space and possibly trash your stack if the function being called expects a byte?
Ah, I was expecting that.

In Protected Mode, the stack is push/pop is extended to 4 bytes (for the most part).
When you push 1 byte, on the stack it counts as four regardless.
If a function expects a byte (pop byte) then it will only take the lowest value (since 32-bit is little endian, the low byte is stored first, therefore it is poped first)

So, in reality, you're just making your life harder by using a push byte.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: NASM byte is signed?

Post by Brendan »

Hi,
Nessphoro wrote:So, in reality, you're just making your life harder by using a push byte.
If you push a byte then pop a byte, it won't matter if the CPU sign extends or zero extends and the opcode will be shorter (2 bytes).

If you push a word or dword then pop a byte, it won't matter if you sign extend or zero extend and the opcode will be longer (5 bytes).

If you push a byte then pop a word or dword, it will matter that the CPU sign extended it (e.g. "push byte 128" then "pop eax" means EAX != 128), which is why NASM gives a warning.

You can disable warnings about number overflows at the command line (e.g. "nasm -w-number-overflow"). This is probably a bad idea, because you might miss problems that you don't know about just to suppress a warning you do know about. You can also disable and enable warnings for any lines of code you want. For example:

Code: Select all

[warning -number-overflow]
	push byte 128                 ;This will not give a warning
[warning +number-overflow]
	push byte 128                 ;This will give a warning
This is probably the best way - disable the warning for that one instruction, keep warnings for everything else in case, and still get a short 2-byte opcode.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: NASM byte is signed?

Post by Nessphoro »

The problem arises when you try to mix asm and C code.

When you push a byte that is over 127 it will be sign extended (NASM treats that number as two's complement, aka negative) and since the number is stored as dword at the stack, when read from C it will be negative even though you might of pushed like 0x80, which clearly is positive.
TomT
Member
Member
Posts: 42
Joined: Sat Mar 15, 2008 7:20 am
Location: Wisconsin, USA
Contact:

Re: NASM byte is signed?

Post by TomT »

Can you pop a byte ?

I dont see that in my Intel manual

You can pop a word, dword or qword depending on the address size attributes of the stack segment.

I program x86 in pmode so

push byte 0x80
pop eax
results in eax=0xffffff80

Thanks for pointing out the sign extend properties of push !

TomT
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: NASM byte is signed?

Post by Owen »

pop al
pop byte ptr [eax]

Both of these will pop a quantity determined by the stack's size attribute and extract the least significant 8 bits
Post Reply