将十进制转换为二进制并在 MIPS 中打印

发布于 2024-09-27 04:38:35 字数 1673 浏览 7 评论 0原文

我正在为作业编写一个简单的代码片段,我需要将十进制数转换为二进制、八进制和十六进制。我让它工作了,但后来我意识到,由于我使用的算法,我向后打印了二进制数。我使用的策略是一次打印一位数字。我仍然是初学者,所以我认为这将是避免更多中间问题的简单方法。不幸的是我并没有想到这一点。

binary:   la        $a0, bType                            #binary function selected, notify user
          li        $v0, 4                                #print notification
          syscall
          la        $a0, in_val                           #ask user for input decimal number
          li        $v0, 4                                #print
          syscall
          li        $v0, 5                                #syscall read int
          syscall
          move      $t0, $v0                              #save input value to $t0
          li        $t1, 2                                #load 2 into $t1 to divide by 2
          li        $v0, 4
          la        $a0, bRes                            
          syscall                                         #print result tag
binLoop:  divu      $t0, $t1                              #LO = $t0/2, HI = $t0 % 2
          mfhi      $t2                                   #$t2 = HI (remainder)
          mflo      $t0                                   #$t0 = $t0/2 (quotient)
          move      $a0, $t2                              #store digit to print
          li        $v0, 1                                #Print digit
          syscall
          bgtz      $t0, binLoop                          #if input != 0, keep dividing
          j         main

有什么方法可以将每个数字存储到带有标签的字符串中,然后将每个数字连接起来,然后向后读取字符串或其他什么?也许你有更好的建议可以给我指点。

请注意,此时代码的工作方式是一次打印一个二进制数字,但顺序与我们想要的相反。该程序应该能够处理大数字(例如 20 个二进制数字长),因此我无法将每个数字存储在自己的寄存器中。谢谢大家!

I'm writing a simple snippet of code for an assignment and I need to convert a decimal number to binary, octal, and hexadecimal. I have it working, but I realized afterwards that because of the algorithm I'm using, I print the binary number backwards. The strategy I was using was to print one digit at a time. I'm still a beginner so I figured this would be an easy way to avoid more intermediate issues. Unfortunately I didn't think it all the way through.

binary:   la        $a0, bType                            #binary function selected, notify user
          li        $v0, 4                                #print notification
          syscall
          la        $a0, in_val                           #ask user for input decimal number
          li        $v0, 4                                #print
          syscall
          li        $v0, 5                                #syscall read int
          syscall
          move      $t0, $v0                              #save input value to $t0
          li        $t1, 2                                #load 2 into $t1 to divide by 2
          li        $v0, 4
          la        $a0, bRes                            
          syscall                                         #print result tag
binLoop:  divu      $t0, $t1                              #LO = $t0/2, HI = $t0 % 2
          mfhi      $t2                                   #$t2 = HI (remainder)
          mflo      $t0                                   #$t0 = $t0/2 (quotient)
          move      $a0, $t2                              #store digit to print
          li        $v0, 1                                #Print digit
          syscall
          bgtz      $t0, binLoop                          #if input != 0, keep dividing
          j         main

Is there any way I can maybe store each digit into a string with a label and concatenate each digit on thereafter, then read the string backwards or something? Maybe there is a better suggestion you might be able to point me to.

Just note that the code works at this point by printing a single binary digit at a time, but in the reverse order which we want it. The program is supposed to be able to handle large numbers(like 20 binary digits long) so I can't store each digit in its own register. Thanks all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

地狱即天堂 2024-10-04 04:38:35

您可能希望将计算值存储在内存中,然后反向读取它们以打印它们。
使用一个备用寄存器来保存缓冲区中数字的偏移量(用于打印),例如 $a1,假设您将存储从内存地址 0x1000 开始的数字。
然后,不要使用系统调用来打印数字,只需将其存储为如下所示:

sw $t2, 0x1000($a1)
addiu $a1, $a1, 4

现在,当您完成处理输入数据时,您可以以相反的顺序遍历您创建的向量并打印每个数字:

addiu $a1, $a1, -4
lw $t2, 0x1000($a1)
# Now your code to print the digit

您必须循环直到$a1达到0

You may want to store the computed values in memory, and then read them on reverse to print them.
Use one spare register to keep the offset of digits in buffer (to print), say $a1, and let's suppose you will be storing the digits starting at memory address 0x1000.
Then, instead of making the syscall to print the digit, just store it with something like this:

sw $t2, 0x1000($a1)
addiu $a1, $a1, 4

Now, when you finish processing the input data, you can traverse in reverse order the vector you made and print each digit:

addiu $a1, $a1, -4
lw $t2, 0x1000($a1)
# Now your code to print the digit

You would have to loop until $a1 reaches 0

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