简体   繁体   English

"为什么 Intel 8086 汇编语言中正数的符号位为 ON?"

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

I'm working in Dosbox on ubuntu assembly language.我正在使用 ubuntu 汇编语言的 Dosbox 工作。 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. x86和当前使用的几乎每个CPU都使用2的补码签名算法。

A byte has 8 bits. 一个字节有8位。
That means it can store 256 different value (2 8 ). 这意味着它可以存储256个不同的值(2 8 )。
127 of those are positive and 128 are negative and one is zero. 其中127个为正,128个为负,一个为零。
To test whether a byte is negative you test the Most Significant Bit (MSB). 要测试字节是否为负,请测试最高有效位(MSB)。 If this bit is 1 it is deemed to be negative, if it is zero it is deemed to be positive. 如果该位为1,则视为负;如果为零,则视为正。

The encoding of a 150 in a byte is 1001 0110 ergo it is deemed to be negative. 字节中的150编码为1001 0110 ergo,因此被视为否定。
In fact you can interpret the value as 150 only if you don't allow negative numbers. 实际上,仅当您不允许负数时,您才可以将值解释为150。 If you do then the byte holds -106. 如果这样做,则该字节保留-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. 这称为有符号溢出,如果您使用影响标志的指令进行计算,则OF标志将检测到此情况。

MOV does not affect the flags MOV不影响标志
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 但是,由于MOV不会影响标记,因此您的问题中存在某些问题,请查看以下网站的modif_f列: http : modif_f
The flags show the state of the CPU as per the last instruction that changed those particular flags. 这些标志按照更改这些特定标志的最后一条指令显示CPU的状态。
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. 发生了什么事, al只能保存八位数据,而符号位是高位。 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. 如果将符号位读取为符号位,则表明该寄存器已签名,范围为127至-128。 150 is outside that range, so it rolled over. 150在该范围之外,所以它滚了过来。

Because you are overflowing.因为你满溢。 on al you can have only numbers from 0 to 127, any greater overflows and becomes negative.在 al 上,您只能有 0 到 127 之间的数字,任何更大的数字都会溢出并变为负数。 In fact the assembly allows that because al is not negative nor possitive.事实上,大会允许这样做,因为 al 既不是消极的也不是积极的。 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?换句话说,您以无符号形式存储 150,然后系统实际上将其转换为实际上不是 150 的有符号值。实际上是..-106 您实际设置为 AL 的值(如签名)现在为什么你没有注意到这一点或它抱怨? 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.因为它不知道该值是否已签名(不确定程序集是否能注意到这一点),并且将取决于您用来对其进行任何操作的指令,以及 al 的使用方式。 (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.或者对于您用来显示的任何解析器。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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