我需要帮助。我已经在 MARS 中编写了这段代码。它应该从用户那里接收一个整数,并将其转换为 HEX。我已经检查了几个小时,据我所知,它应该可以正常工作。我只包含了程序的循环和输出部分,因为这是唯一不起作用的部分。有人可以指出代码哪里出错了吗?谢谢你。
PS我认为它在逐位AND上搞砸了,我用它来掩盖低位,但由于某种原因,它几乎看起来像是在添加而不是ANDing。:-(
li $s1, 8 # Set up a loop counter
环形:
rol $s0, $s0, 4 # Roll the bits left by four bits - wraps highest bits to lowest bits (where we need them!)
and $t0, $s0, 16 # Mask off low bits (logical AND with 000...01111)
slti $t1, $t0, 10 # Determine if the bits represent a number less than 10 (slti)
bne $t1, $zero, MakeLowDigit # If yes (if lower than 9), go make a low digit
MakeHighDigit:
subi $t0, $t0, 10 # Subtract 10 from low bits
addi $t0, $t0, 64 # Add them to the code for 'A' (65), becomes a..f
j DigitOut
MakeLowDigit:
addi $t0, $t0, 47 # Combine it with ASCII code for '0', becomes 0..9
数字输出:
move $a0, $t0 # Output the ASCII character
li $v0, 11
syscall
subi $s1, $s1, 1 # Decrement loop counter
bne $s1, $zero, Loop # Keep looping if loop counter is not zero