News:

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

Sign extension?

Started by MickD, January 12, 2006, 09:41:29 PM

Previous topic - Next topic

MickD

Hi All,
I'm having trouble understanding how the machine understands sign extension? <edit> when using sign contraction</edit>

How does the assembler know whether a number should be say F9B when it's FFFF FF9B ???

I understand that when the HO bit is 1 it is negative, I just can't get how the computer knows where to stop?

OR

Given that the number is a signed integer for eg. the machine does its own 2's compliment thing on it to arrive at the positive value and when printed throws a '-' on the front?

Thanks,
Mick.

zooba

The computer has its own limits. There's no such thing as F9B because that would be 1.5 bytes long :wink 0F9B is two bytes and the MSB is zero so the number is positive.

Basically, its a case of FFFF FF9B just isn't F9B, ever. The computer also doesn't know whether the number is signed or not - all it sees are 32 bits - the programmer decides that it will represent a number and that number will be signed.

As for the second part, that is generally how the programmer will code it. Integer to string conversions don't exist in the computer and must be written for it. Generally two different functions (at least) will be provided for conversions.

Hope this helps :U

Cheers,

Zooba

MickD

Thanks Zooba, it definitely does help, I think I got confused as you sometimes see hex values truncated to the bare digits but that would have been in a different context.
Thanks again, now I can move on with my study  :toothy

MickD

Ok, just one more quick question -

So, if I have a signed byte of 9F, which I know is a negative from the MSB, I would have to convert it using NEG to get it back to a positive to get the actual number and print it out as a negative adding the '-' sign myself (if I didn't have a function to do it say).

I think that's right, just want to make sure,

Thanks,
Mick.

MickD

Don't worry, the penny just dropped  ::)
A signed byte is only +127, although you can go higher, all MSB's will be one therefore you couldn't distiguish between +/- hence the limit on the numbers (8 = half of 16 bytes) and this is the same for lager ints also. doh!
:thumbu

zooba

Quote from: MickD on January 13, 2006, 12:53:32 AM
A signed byte is only +127, although you can go higher, all MSB's will be one therefore you couldn't distiguish between +/- hence the limit on the numbers (8 = half of 16 bytes) and this is the same for lager ints also. doh!

Close. The largest value a signed byte can contain is +127 (7Fh). The smallest is -128 (80h). This gives a range of 256 (from -128 to +127) which is the same as for an unsigned byte (0 to 255). Because each binary place has a value of 2, by using the most significant bit for something other than the decimal part of the number (in this case, the sign) it reduces the positive range by a half.

Your statement "8 = half of 16 bytes" is correct, but out of context :wink . In this case, 7 bits = half the range of 8 bits. So by treating the number as signed we are halving the range of the magnitude of the number.

Just for interests sake, a signed byte with a value of -128 needs a special case to convert it to a string. NEG -128 will give a result of -128, since positive 128 won't fit in a byte. :U

MickD

Thanks again zooba, that's sorta what I was trying to say ( I think :D) .

Thanks for the advice on NEG -128 too  :U

Ratch

MickD,

Quote
Hi All,
I'm having trouble understanding how the machine understands sign extension? <edit> when using sign contraction</edit>

     For addition and subtraction, the arithmetic instructions are the same for either signed or unsigned numbers.  It is the programmer/user that interprets whether the MSB is either a sign bit for signed numbers or a number larger than 127 for unsigned numbers.  Ratch