关于汇编CF(进位)和OF(溢出)标志

发布于 2024-07-18 05:26:34 字数 108 浏览 7 评论 0原文

众所周知,CF表示无符号进位,OF表示有符号溢出。 那么,汇编程序如何区分无符号和有符号数据,因为它只是一个位序列? (通过类型信息的附加内存存储,或通过位置信息或其他?)这两个标志可以互换使用吗?

It's known that CF indicates unsigned carry out and OF indicates signed overflow. So how does an assembly program differentiate between unsigned and signed data since it's only a sequence of bits? (Through additional memory storage for type information, or through positional information or else?) And could these two flags be used interchangeably?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦过后 2024-07-25 05:26:34

区别在于使用哪些指令来操作数据,而不是数据本身。 现代计算机(大约自 1970 年起)使用称为补码的整数数据表示形式,其中加法和减法对有符号数和无符号数的作用完全相同。

  • 表示形式的差异在于对最高有效位(也称为符号位)的解释。 对于无符号数,当该数位于整个正数范围的上半部分时,最高有效位被设置。 对于有符号数,当数字位于整个范围的下半部分和负半部分时,将设置最高有效位。

  • 不同的指令可能对同一位使用不同的解释。 例如,大多数大型机器都具有有符号和无符号乘法指令。 具有“设置小于”指令的机器可能同时具有有符号和无符号风格。

  • OF(溢出标志)表明进位是否翻转了结果中最高有效位的符号,以便它与参数的最高有效位不同。 如果数字被解释为无符号,则溢出标志无关紧要,但如果它们被解释为有符号,则 OF 表示,例如,两个大正数相加,结果为负。

  • CF(进位标志)表明某位是否完全从字中进位(例如进入位 33 或位 65)。 如果数字被解释为无符号,进位标志意味着加法溢出,并且结果太大而无法容纳机器字。 溢出标志是无关紧要的。

您的问题的答案是,汇编代码有多种方法来区分有符号和无符号数据:

  • 它可以选择 CF 或 OF 来进行有符号或无符号比较。
  • 它可以选择有符号或无符号乘法和除法指令。
  • 它可以选择有符号或无符号右移(有符号复制高位;无符号移动为零)。

The distinction is in what instructions are used to manipulate the data, not the data itself. Modern computers (since circa 1970) use a representation of integer data called two's-complement in which addition and subtraction work exactly the same on both signed and unsigned numbers.

  • The difference in representation is the interpretation given to the most significant bit (also called the sign bit). For unsigned numbers the most significant bit is set when the number is in the upper half of the wholly positive range. For signed numbers the most significant bit is set when the number is in the lower and negative half of the whole range.

  • Different instructions may use different interpretations of the same bit. For example most big machines have both signed and unsigned multiply instructions. Machines with a 'set less than' instruction may have both signed and unsigned flavors.

  • The OF (overflow flag) tells whether a carry flipped the sign of the most significant bit in the result so that it is different from the most significant bits of the arguments. If numbers are interpreted as unsigned, the overflow flag is irrelevant, but if they are interpreted as signed, OF means, e.g., two large positive numbers were added and the result was negative.

  • The CF (carry flag) tells whether a bit was carried out of the word entirely (e.g. into bit 33 or bit 65). If numbers are interpreted as unsigned, carry flag means that addition overflowed, and the result is too large to fit in a machine word. The overflow flag is irrelevant.

The answer to your question is that assembly code has several ways of distinguishing signed from unsigned data:

  • It may choose either CF or OF to do signed or unsigned comparisons.
  • It may choose either signed or unsigned multiply and divide instructions.
  • It may choose a signed or unsigned right shift (signed copies the high bit; unsigned shifts in zeroes).
抽个烟儿 2024-07-25 05:26:34

不要尝试对符号进行操作。 那是不可能的。 相反,只要尝试了解真相:没有迹象。 然后你就会发现,区别的不是星座类型,而是你自己。

Do not try to opcode the sign. That is impossible. Instead, only try to realize the truth: there is no sign. Then you'll see it is not the sign-type that differentiates, it is only yourself.

|煩躁 2024-07-25 05:26:34

有不同的操作码用于处理带符号和不带符号的数据。 如果程序想要比较两个有符号整数,则使用操作码 jljlejgjge ,其中 l 和 g 分别代表 less 和 greeater。 如果程序想要比较两个无符号整数,则使用操作码 jbjbejajae ,其中 a 和 b 分别代表 above 和 below。 在所有情况下,e 都代表“或等于”。 这些操作码用于基于比较的分支。

类似地,还有 setCC 指令,根据比较将字节设置为 0 或 1。 这些功能相同 - 有 setlsetlesetgsetgesetb >、setbesetasetae 等。

签名的操作码测试标志 ZF、OF 和 SF。 无符号操作码测试标志 ZF、CF 和 SF。 请参阅 80386 程序员参考手册中有关 JCC 说明的部分以及针对测试的确切条件的 setCC 说明。

There are different opcodes for dealing with signed and unsigned data. If a program wants to compare two signed integers, it uses the opcodes jl, jle, jg, and jge, where the l and g stand for less and greater respectively. If a program wants to compare two unsigned integers, it uses the opcodes jb, jbe, ja, and jae, where the a and b stand for above and below respectively. The e stands for 'or equal to' in all cases. These opcodes are used for branching based on a comparison.

Similarly, there are also the setCC instructions, which set a byte to 0 or 1 depending on a comparison. These function identically -- there are setl, setle, setg, setge, setb, setbe, seta, setae, and others.

The signed opcodes test the flags ZF, OF, and SF. The unsigned opcodes test the flags ZF, CF, and SF. See the 80386 Programmer's Reference Manual sections on the JCC instructions and the setCC instructions for the exact conditions tested.

如痴如狂 2024-07-25 05:26:34

事实并非如此。 只要条件发生,标志就会被设置。 程序员应该知道他正在使用什么类型的整数,并从中知道如果他关心的话要检查哪个标志。

It doesn't. The flags just become set whenever the condition occurs. The programmer is supposed to know what types of ints he's working with and from that know which flag to examine if he cares.

韶华倾负 2024-07-25 05:26:34

没有办法要求CPU测试并返回byte/word/long的类型。

0xFF 可能包含“255”或“-1”,这完全取决于程序所说的字节类型。

“类型”、“符号”等结构仅存在于 Java 等高级语言中,而不存在于 CPU 级别。 最后,一切对于 CPU 来说都是一个字节,这取决于我们的程序来组织并知道如何解释和操作这些值...

状态中找到的 CPU 标志不会强制执行任何范例,而是由您的代码来测试并做出相应反应。

在 Intel CPU 上,MMX 和 FPU 寄存器实际上占用相同的寄存器。 因此不可能同时混合 FPU 和 MMX 类型指令,因为一个操作的值会破坏另一个操作的值。 需要任一模式的程序通常以一种模式完成其操作,例如发出 FPU 指令,然后可能启动 MMX,但绝不会同时启动两者。

There is no way to ask the CPU to test and return the type of a byte/word/long.

0xFF may hold "255" or "-1" it all depends on what type of byte your program says it is.

Constructs such as "type", "signess" etc only exist in higher level languages such as Java and not at the CPU level. In the end everything is a byte to the CPU it is up to our programs to organise and know how to intrepret and manipulate these values...

The CPU flags found in the status do not enforce any paradigm it is up to your code to test and react accordingly.

On Intel CPUs the MMX and FPU registers actually occupy the same registers. It is thus impossible to mix FPU and MMX type instructions at the same time, because values from one operation will trash the other. Programs that need either typically complete their actions in one mode eg issuing FPU instructions and then might start MMX but never both at the same time.

稀香 2024-07-25 05:26:34

通常汇编程序不携带变量的特殊信息来指示它们是有符号还是无符号。 程序员的工作就是知道何时检查哪些标志以及何时使用哪些条件(即使用 JA 而不是 JG)。

因此,您需要知道要使用什么类型的变量,以便知道要使用哪些命令。 这就是为什么当程序员互换使用有符号/无符号类型(即没有显式转换)时,大多数编程语言都会发出警告,因为这可以在硬件中完成,但可能会产生意外的结果。

Usually assembly programs carry no special information around with variables to indicate whether they are signed or unsigned. It's the programmer's job to know when to check which flags and when to use which conditionals (i.e. using JA instead of JG).

So you need to know what type of variable you're about to work with so that you know which commands to use. This is why most programming languages give warnings when programmers use signed/unsigned types interchangeably (i.e. without an explicit cast), since this can be done in the hardware but can yield unexpected results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文