汇编代码如何知道某个值是带符号的还是无符号的? [英] How does assembly code know if a value is signed or unsigned?

查看:634
本文介绍了汇编代码如何知道某个值是带符号的还是无符号的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当看起来我的代码有时将某个值视为带符号的值而有时将其与比较值时视为无符号的值时,我感到非常困惑.代码如何知道一个值是带符号的还是无符号的?

I get very confused when it seems like sometimes my code treats a certain value as signed and sometimes it treats it as unsigned when comparing values. How does the code know whether a value is signed or unsigned?

推荐答案

为什么您认为汇编代码必须知道"值是带符号的还是无符号的?

Why do you think that assembly code has to "know" if a value is signed or unsigned?

对于大多数操作,签名操作和未签名操作的结果相同:

For most operations the results of a signed and an usigned operation are the same:

signed int a = 5;
signed int b = -6; // 0xFFFFFFFA
signed int c;
c = a + b; // results in -1 which is 0xFFFFFFFF

并且:

unsigned int a = 5;
unsigned int b = 0xFFFFFFFA;
unsigned int c;
c = a + b; // results in 0xFFFFFFFF

一些例外是除法和比较.在这种情况下,大多数CPU对于有符号和无符号操作具有不同的汇编程序指令.这里的示例是x86汇编程序,但msp430应该类似:

Some exceptions are division and comparison. Most CPUs have different assembler instructions for signed and unsigned operations in this case. The examples here are x86 assembler but msp430 should be similar:

signed int a, b;
if(a > b) { ... }

结果:

mov eax, [a]
cmp eax, [b]
jle elsePart ; Note the "L" in "jle"

并且:

unsigned int a, b;
if(a > b) { ... }

结果:

mov eax, [a]
cmp eax, [b]
jbe elsePart ; Note the "B" in "jbe"

这篇关于汇编代码如何知道某个值是带符号的还是无符号的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆