我的 MIPS 代码中的错误在哪里?
我需要帮助。我已经在 MARS 中编写了这段代码。它应该从用户接收一个整数,并将其转换为十六进制。我已经检查了几个小时,据我所知,它应该可以正常工作。我只包含了程序的循环和输出部分,因为这是唯一不起作用的部分。有人可以指出代码哪里出了问题吗?谢谢。
聚苯乙烯 我认为它弄乱了逐位“与”,我用它来屏蔽较低的位,但由于某种原因,它几乎看起来像是“加”而不是“与”。 :-(
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
DigitOut:
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
i need help. I have written this code in MARS. It is supposed to receive an integer from the user, and convert it to HEX. I've been going over it for hours and as far as i can see, it should work fine. I have only included the loop and output part of the program as this is the only part that is not working. Can someone PLEASE point out where the code is going wrong? Thank you.
P.S.
I think it is messing up on the bit-by-bit AND, i used this to mask off the lower bits but for some reason it almost seems like it's ADDing not ANDing. :-(
li $s1, 8 # Set up a loop counter
Loop:
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
DigitOut:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您至少使用了错误的常量进行掩码:
整数 16 的位模式为
...010000
,即它不以四个 1 结尾。您需要 15,即 16 - 1。一般来说,要创建具有 n 位集的右调整位掩码,您需要计算
2n-1,在 C 表示法中为
(1 << n) - 1
。You're at least masking with the wrong constant:
The integer 16 has the bit pattern
...010000
, i.e. it very much does not end in four ones. You want 15, i.e. 16 - 1.In general, to create a right-adjusted bit-mask with n bits set, you need to compute
2n-1, which in C notation is
(1 << n) - 1
.除了滚动错误的常量之外,您的 ASCII 值设置也不正确。 ASCII 中的“A”由数字 65 表示。在您的代码中,您将 64 添加到滚动掩码的结果中。如果最后四位等于 10(十进制),换句话说 $t0 包含十进制 10,则您的代码会从 $t0 中减去 10。因此,$t0 的十进制结果将为 0。当您添加 64 时,您的代码将获得 @ 字符的 ASCII 值。
http://www.asciitable.com/
In addition to rolling the wrong constant your ASCII values are set incorrectly. 'A' in ASCII is represented by the number 65. In your code you are adding 64 to the result of your rolling mask. If the last four bits were equal to 10 (decimal), in other words $t0 contains the decimal 10, your code subtracts 10 from $t0. $t0 will therefore have the decimal result of 0. When you add 64 to this, your code is going to get the ASCII value for the @ character.
http://www.asciitable.com/