汇编语言作业遇到问题

发布于 2024-10-15 21:58:45 字数 1371 浏览 4 评论 0原文

我以为我正确地实现了 while 循环,但为什么我没有得到任何输出?

我的书没有多大帮助,而且我无法在网上找到资源。

##### The Data Segment #########

.data
strFirstNumber:     .asciiz  "Enter the first number (0-63): "
strSecondNumber:    .asciiz  "Enter the second number (0-63): "
strError:           .asciiz  "That number is not in the 0-63 range.\n\n"

#### The Text Segment ##########

.text
.globl main

main:
    li $t2, 0
#First Number
    li $10, 64
    li $v0, 4
    la $a0, strFirstNumber
    syscall
    li $v0, 5
    syscall
    blez $v0, in_error
    bgeu $v0, $10, in_error
    j DoneIf

in_error:
    li $v0, 4
    la $a0, strError
    syscall
    li $v0, 4
    la $a0, strFirstNumber
    syscall
    li $v0, 5
    syscall
    bltz $v0, in_error
    bgeu $v0, $10, in_error

DoneIf:
    move $t0, $v0

#Second Number
    li $v0, 4
    la $a0, strSecondNumber
    syscall
    li $v0, 5
    syscall
    bltz $v0, in_error2
    bgeu $v0, $10, in_error2
    j DoneIf2

in_error2:
    li $v0, 4
    la $a0, strError
    syscall
    li $v0, 4
    la $a0, strSecondNumber
    syscall
    li $v0, 5
    syscall
    blez $v0, in_error2
    bgeu $v0, $10, in_error2

DoneIf2:
    move $t1, $v0

Loop:
    beq    $t2, $t0, Exit
    add    $t3, $t1, $t1
    add    $t2, $t2, 1

    j    Loop        # go to Loop

Exit:
    li $v0, 1
    add    $a0, $0, $t3
    syscall

    jr    $31

I thought I correctly implemented a while loop, but why am I not getting any output?

My book isn't that great of a help, and I haven't been able to find a resource online.

##### The Data Segment #########

.data
strFirstNumber:     .asciiz  "Enter the first number (0-63): "
strSecondNumber:    .asciiz  "Enter the second number (0-63): "
strError:           .asciiz  "That number is not in the 0-63 range.\n\n"

#### The Text Segment ##########

.text
.globl main

main:
    li $t2, 0
#First Number
    li $10, 64
    li $v0, 4
    la $a0, strFirstNumber
    syscall
    li $v0, 5
    syscall
    blez $v0, in_error
    bgeu $v0, $10, in_error
    j DoneIf

in_error:
    li $v0, 4
    la $a0, strError
    syscall
    li $v0, 4
    la $a0, strFirstNumber
    syscall
    li $v0, 5
    syscall
    bltz $v0, in_error
    bgeu $v0, $10, in_error

DoneIf:
    move $t0, $v0

#Second Number
    li $v0, 4
    la $a0, strSecondNumber
    syscall
    li $v0, 5
    syscall
    bltz $v0, in_error2
    bgeu $v0, $10, in_error2
    j DoneIf2

in_error2:
    li $v0, 4
    la $a0, strError
    syscall
    li $v0, 4
    la $a0, strSecondNumber
    syscall
    li $v0, 5
    syscall
    blez $v0, in_error2
    bgeu $v0, $10, in_error2

DoneIf2:
    move $t1, $v0

Loop:
    beq    $t2, $t0, Exit
    add    $t3, $t1, $t1
    add    $t2, $t2, 1

    j    Loop        # go to Loop

Exit:
    li $v0, 1
    add    $a0, $0, $t3
    syscall

    jr    $31

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

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

发布评论

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

评论(2

一袭水袖舞倾城 2024-10-22 21:58:45

几乎不可能说出哪里出了问题:

  1. 我们不知道系统调用中发生了什么。
  2. 您正在使用 $t 寄存器来存储值。这很糟糕,因为 MIPS 将它们定义为暂存寄存器。使用 $s0$s7
  3. 我们不知道您的目标。这是真正的硬件还是模拟器?
  4. 顺便说一句,我们不知道代码是否完整

,如果您正确输入了两个数字,则您的代码必须运行到循环中。同样,考虑到系统调用可能会破坏寄存器,它可能会运行很长一段时间。

首先尝试更改寄存器分配,然后删除不会更改 $t3 计算结果的循环。并可能检查您的打印系统调用是否正常工作。

It's nearly impossible to say what's wrong:

  1. we don't know what's happen in syscalls.
  2. you are using $t registers to store value. This is bad because MIPS define them as scratch regiters. Use $s0 to $s7 instead
  3. we don't know your target. Is this real HW or emulator ?
  4. we don't know if the code complete

BTW, if you correctly entered both numbers, your code must run into the loop. Again, given that syscalls probably trashed registers, it may run for a ... long ... time.

Try to change register allocation first, then remove the loop that does not change the result computed into $t3. And possibly check that your syscall to print work correctly.

羁绊已千年 2024-10-22 21:58:45

这是因为你没有在程序中描述“syscall”中发生了什么过程,似乎没有真正的输出结果。

It is because you haven't described in the program what process happens in 'syscall' There seems to be no real output result.

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