【问题标题】:mips printing numbers with # of bits given in inputmips 使用输入中给定的位数打印数字
【发布时间】:2022-12-03 14:31:34
【问题描述】:

我需要制作一个 mips 程序,当给定一个整数时,将打印所有可能的数字和该位数。最好的做法是什么?

【问题讨论】:

  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。
  • 你需要一个算法。大多数算法都与语言无关。因此,您不需要在汇编中思考以获得算法,也不需要只研究该算法的 MIPS 版本。通过将算法工作与在 MIPS 汇编中执行相同的算法分开来降低问题的复杂性。

标签: mips mars-simulator


【解决方案1】:

这可能会帮助您入门。这是一种计算二进制数中 1 的个数的方法。

popcnt:
;input: $a0 = the 32-bit number you wish to check
;output: $v0 = the number of bits that equal 1.

move $v0,$zero
li $t0,32

loop_popcnt:

move $a1,$a0
andi $a1,$a1,1
beqz $a1,skip    # if zero, the bit we tested was zero, so don't add 1 to the answer.
   nop           # branch delay slot. We don't want the next instruction to execute if we branch

   addiu $v0,$v0,1
skip:

ror $a0,$a0,1    # next bit
addiu $t0,$t0,-1 

bnez $t0,loop_popcnt
nop              #branch delay slot.

jr $ra

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多