News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Negative values

Started by ookami, February 16, 2011, 08:50:53 PM

Previous topic - Next topic

ookami

I have a weird question, I hope you'll get me... :red

How the computer know if the byte he gets from the memory is a "byte" or a "sbyte" ? After all, there is no difference (from the binary point of view) between signed or unsigned number...
I mean, 1001 1101 could be signed -99 or unsigned 157.

I hope that's clear...

By the way, another question : Is that possible to store signed values into registers ? If not, how can I store negative numbers into them ?

Here a quote from the Art of Assembly (randall hyde) :
QuoteThe registers, by their very nature, are neither signed nor unsigned.

But it's possible to NEG a register... so I don't understand what that means...


Thanks

jj2007

For the register, it does not matter if the byte carries +255 or -1; if the most significant bit (#7 for a byte, #31 for a DWORD) is set, the number is negative.
For the CPU, it matters when conditional jumps are being performed. Check the differences between e.g. jg and ja, and their high level equivalents .if sdword ptr eax resp. .if dword ptr eax - the latter is the default you get with .if eax.

dedndave

the binary byte value is the same
what is different is - how you treat it
in other words, it is signed or unsigned according to the context in which it's used

a good example of this is the conditional branch instructions
if i want to make a signed comparison, i use JG - if i want an unsigned comparison, i use JA

guess i owe Jochen a coke   :P

MichaelW

QuoteHow the computer know if the byte he gets from memory is a "byte" or a "sbyte" ?

It doesn't "know". For integers the signed/unsigned characteristic is all in the instructions that are used to manipulate/interpret them. And in addition to signed and unsigned comparisons, there are also signed and unsigned versions of the instructions that multiply and divide.

QuoteIs that possible to store signed values into registers ?

Yes, for data initializers and immediate operands MASM will accept signed or unsigned values, with the restriction that the values must be within the allowable range.

;====================================================================
    include \masm32\include\masm32rt.inc
;====================================================================
    .data
        ub  db 255
        sb  db -1
        uw  dw 65535
        sw  dw -1
        ud  dd 4294967295
        sd  dd -1
        uq  dq 18446744073709551615
        sq  dq -1
    .code
;====================================================================
start:
;====================================================================
    mov al, 255
    movzx eax, al
    print hex$(eax),13,10
    mov al, -1
    movzx eax, al
    print hex$(eax),13,10
    mov ax, 65535
    movzx eax, ax
    print hex$(eax),13,10
    mov ax, -1
    movzx eax, ax
    print hex$(eax),13,10
    mov eax, 4294967295
    print hex$(eax),13,10
    mov eax, -1
    print hex$(eax),13,10

    movzx eax, ub
    print hex$(eax),13,10
    movzx eax, sb
    print hex$(eax),13,10
    movzx eax, uw
    print hex$(eax),13,10
    movzx eax, sw
    print hex$(eax),13,10
    print hex$(ud),13,10
    print hex$(sd),13,10

    invoke crt_printf, cfm$("%I64X\n"), uq
    invoke crt_printf, cfm$("%I64X\n\n"), sq

    inkey "Press any key to exit..."
    exit
;====================================================================
end start


000000FF
000000FF
0000FFFF
0000FFFF
FFFFFFFF
FFFFFFFF
000000FF
000000FF
0000FFFF
0000FFFF
FFFFFFFF
FFFFFFFF
FFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF



eschew obfuscation

oex

We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

ookami

Thank you guys, i understand now.

But why when I do :

mov eax, -22
print  str$  (eax)


That gives me :
-22

Does the print macro read all the values like signed ?

dedndave

read the file  \masm32\help\hlhelp.chm

str$ is a signed macro
ustr$ and uhex$ are unsigned macros

another example of context

ookami

Thanks, i understand. :bg

But, instead of str$, i found sstr$. I suppose it's the same...

dedndave

i think it's an alias - or an older version

ookami


mineiro

How the computer know if the byte he gets from memory is a "byte" or a "sbyte" ?

Hello Sr ookami;
Some bytes can go and turn into a sound, or can be some colors in the screen, or just text, or signed or unsigned numbers,...., what they means, who say this are we.
regards.