我正在尝试将存储在 2 个寄存器中的 2 个数字相加。每个数字都是 bcd 格式,有 8 位数字。 我想知道是否有更好的方法,一次只处理每 4 位。
这就是我的开始:
.text
main:
addi $s2,$zero,00010010001101000101011001111000#num1
addi $s3,$zero,00010100011110000101001000110110#num2
addi $t0,$zero,00000000000000000000000000001111#mask
and $t1,$t0,$s2#geting digit#1 of num1
and $t2,$t0,$s3#geting digit#2 of num2
add $t3,$t1,$t2#adding digits
#checking for overflow
#doing the same for the rest of the digits
#add $s4,$s3,$s2
最佳答案
relevant Wikipedia page有一个压缩 BCD 加法的算法:
uint32_t BCDadd(uint32_t a,uint32_t b)
{
uint32_t t1, t2; // unsigned 32-bit intermediate values
t1 = a + 0x06666666;
t2 = t1 ^ b; // sum without carry propagation
t1 = t1 + b; // provisional sum
t2 = t1 ^ t2; // all the binary carry bits
t2 = ~t2 & 0x11111110; // just the BCD carry bits
t2 = (t2 >> 2) | (t2 >> 3); // correction
return t1 - t2; // corrected BCD sum
}
这应该可以直接转换为 MIPS 汇编。
关于assembly - 添加2个bcd数字-mips,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47638491/