平均2个寄存器,避免汇编中的溢出? [英] Average of 2 registers, avoiding overflow in assembly?

查看:58
本文介绍了平均2个寄存器,避免汇编中的溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,当加法超出8位范围时,我不知道如何处理.

My problem is that I don't know how to handle the addition when it gets out of the 8-Bit range.

我的程序以#16h +#05h之类的小十六进制数字运行,但没有以#0A9H +#0B5H之类的较高十六进制数运行.

My program runs with small Hex numbers like #02h + #05h but not with higher numbers like #0A9H + #0B5H.

推荐答案

对于2个数字,有一个asm技巧:进位标志保存ADD结果的第9位,我们可以通过旋转右移该9位值-直通携带.8051的 RRC指令可以做到这一点.

For 2 numbers, there's an asm trick: the carry flag holds the 9th bit of the ADD result, and we can right shift that 9-bit value with rotate-right through carry. 8051's RRC instruction does that.

   ADD A, R0
   RRC


如果没有,请参阅类似的问题如何安全地对C ++中的两个未签名整数求平均值?解释两个数字的安全平均值

对于2个以上的数字,请进行扩展精度(使用加法运算)以保持更大的和.

For more than 2 numbers, do extended-precision (using add-with-carry) to keep a wider sum.

这篇关于平均2个寄存器,避免汇编中的溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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