我的 MIPS 代码中的错误在哪里?

发布于 2024-08-05 04:10:58 字数 1312 浏览 7 评论 0原文

我需要帮助。我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯到世界奔溃 2024-08-12 04:10:58

您至少使用了错误的常量进行掩码:

and $t0, $s0, 16               # Mask off low bits (logical AND with 000...01111)

整数 16 的位模式为 ...010000,即它以四个 1 结尾。您需要 15,即 16 - 1。

一般来说,要创建具有 n 位集的右调整位掩码,您需要计算
2n-1,在 C 表示法中为 (1 << n) - 1

You're at least masking with the wrong constant:

and $t0, $s0, 16               # Mask off low bits (logical AND with 000...01111)

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.

行至春深 2024-08-12 04:10:58

除了滚动错误的常量之外,您的 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文