简体   繁体   中英

Why is sign bit ON for positive numbers in Intel 8086 assembly language?

I'm working in Dosbox on ubuntu assembly language. My program has only this line:

mov al, 150 ;(not -150)

The x86 and pretty much every CPU currently in use uses 2's complement signed arithmetic.

A byte has 8 bits.
That means it can store 256 different value (2 8 ).
127 of those are positive and 128 are negative and one is zero.
To test whether a byte is negative you test the Most Significant Bit (MSB). If this bit is 1 it is deemed to be negative, if it is zero it is deemed to be positive.

The encoding of a 150 in a byte is 1001 0110 ergo it is deemed to be negative.
In fact you can interpret the value as 150 only if you don't allow negative numbers. If you do then the byte holds -106.

This is called signed overflow and the OF flag will detect for this if you're doing calculations using instructions that affect the flags.

MOV does not affect the flags
There is something amiss in your question though because MOV does not affect the flags, check out the modif_f column in: http://ref.x86asm.net/coder32-abc.html
The flags show the state of the CPU as per the last instruction that changed those particular flags.
Some instructions only change a subset of the flags.
You need to step back in your code and look at the last instruction that affected the flags.

The sign bit is not on for positive numbers.

What's going on is al only holds eight bits of data, and the sign bit is the high bit. Interpretation of values in registers as signed or unsigned is all in your head. If you read the sign bit as a sign bit, you have indicated the register is signed, yielding a range of 127 to -128. 150 is outside that range, so it rolled over.

Because you are overflowing. on al you can have only numbers from 0 to 127, any greater overflows and becomes negative. In fact the assembly allows that because al is not negative nor possitive. it just is. but if you know you will use signed ones you should not overflow it on the load. In other words, you are storing 150 in unsigned form, and after that the system is actually transforming that to the signed value which is not actually 150. in fact is..-106 the value you actually are setting into AL (as signed) Now why you do not notice this or it complains? because it does not know if the value is signed or not (not sure assembly can notice that) and will depend on the instruction you use to make anything to it, on how the al is used. (in fact for addition does not matter it works the same) but is for multipliation or division if maters or not. Or for whatever parser you use to show.

"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM