assembly - 添加2个bcd数字-mips

标签 assembly mips bcd

我正在尝试将存储在 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/

相关文章:

c - 汇编代码和 Switch 语句案例

python - 使用python进行数据编码和解码

Java将int数组中的2位十进制转换为BCD

assembly - 如果许多编程语言前端的编译器后端相同,那么不同语言的编译目标代码是否相同?

c - 可变大小栈帧的汇编(关于局部变量的栈)

c++ - Borland x86 内联汇编程序;获取标签的地址?

assembly - 查找输入字符串的长度

assembly - 为什么我们不能将寄存器(bne-beq)与立即数(Assembly-MIPS)进行比较?

assembly - 在 MIPS 汇编中,如果浮点寄存器中有浮点值 X.YZDEF,如何截断到小数点后两位?

c - 在C中使用数组进行BCD转换