如何在汇编中使用负数? [英] how to use negative numbers as one in assembly?

查看:1188
本文介绍了如何在汇编中使用负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在汇编中有一个算术程序,但是当我加,减,乘负数时,将不会得到所需的输出.

I have a arithmetic program in assembly, but when i add, subtract, multiply negative numbers, it will not result in desired output.

例如

输入:

-1+2=66675 (should be 1)

-1-1=656745 (should be -2)

-1*-1=66757 (should be 1) 

问题:

  • 我如何将(-)和(1)视为一个?

  • how would i treat (-) and (1) as one?

如何对带符号的数字进行算术运算?

how to do arithmetic operation in signed numbers?

请给我任何建议...

any advice please ...

推荐答案

我建议阅读2的恭维以及有符号和无符号整数之间的区别.您显示的值看起来像一个有符号的int负值,可疑地转换为一个无符号的int值,而无需进行转换.负整数的最高有效位设置为1.如果不先屏蔽就将该值推入无符号整数,则得到的数字会比预期的大得多.

I recommend reading up on 2's compliment and the difference between signed and unsigned ints. The value you are showing looks suspiciously like a signed int negative value being translated into an unsigned int value without doing a conversion. Negative ints have a Most Significant Bit that is set to 1. If you shove that value into an unsigned int without first masking then you get a much larger number then expected.

以8位表示形式的示例:

Example in a 8 bit representation:

signed value = -1 
unsigned value = 255 
binary = 1111 1111

Take the twos compliment: 
       1111 1111
XOR    0000 0000
equals 0000 0000
add1   0000 0001
dec value = 1

您可以在此处了解更多信息(他们有一个例子供您参考,以补充两个): http://academic.evergreen.edu/projects/biophysics/technotes/program/2s_comp.htm

You can learn more here (They have an example for two's compliment addition you can look at): http://academic.evergreen.edu/projects/biophysics/technotes/program/2s_comp.htm

这篇关于如何在汇编中使用负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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