首页 > 解决方案 > Mips如何计算整数中的唯一数字

问题描述

您好,我正在将此 C 代码转换为 MIPS,但遇到了一个小障碍。该程序应该计算整数中的唯一数字。我遇到的麻烦是while循环中的“IF语句”。我似乎无法让我的 MIPS 代码执行该 C 操作。

C代码:

    while(user > 0) {
       digit = 1 << (user % 10);
       if(!(store & digit)) {
          store += digit;
          count++;
       }
       user /= 10;
     }

MIPS 代码:

    WhileLoop:
       #while(user > 0)
       blez $t0, Exit

       #$t3 = user % 10
       div $t3, $t0, 10

       #get remainder
       mfhi $s0

       #move remainder to $t0
       move $t3, $s0

       #$t6 = 1
       li $t6, 1

       #$t8 = 1 << (user % 10)
       sllv $t8, $t6, $t3

       #$t4 = store& digit
       and $t4, $t7, $t8

       #!(store& digit)
       bne $t4, 0, ExitIf

       #store += digit
       add $t7, $t7, $t8

       #count++
       addi $t9, $t9, 1

       ExitIf:
          div $t0, $t0, 10

          #get the quotient
          mflo $s1

          move $t0, $s1

          j WhileLoop

标签: cc++11assemblymips

解决方案


您的 MIPS 代码正在执行以下操作:

store &= digit

您确定要更新吗?store那不是您的 C 代码正在做的事情。

此外,由于您的 C 代码示例不完整(程序集也是如此),我们无法判断您是否正确反转了程序集版本的条件。

在 C 中,if ( a ) then-part需要if (! a) goto skip; then-part; skip: ;在汇编中。


推荐阅读