【问题标题】:MIPS not running (code dropped off bottom, infinite loop?)MIPS 没有运行(代码从底部掉下来,无限循环?)
【发布时间】:2014-04-10 00:52:28
【问题描述】:

我有一个项目正在努力解决,但我无法找出代码中的错误(顺便说一句,我对 MIPS 还很陌生)。给定一个函数 F = (AB) xor (CD),我必须将完整的真值表计算为 a 并计算 F 的标准真值表中一个值(最小项)的数量. 输出应显示在显示控制台上。这是我的代码:

.data
space: .asciiz " "
str: .asciiz "The number of minterms in F is: "
newline: .asciiz "\n"
.text

li $t0, 0       #A = 0
li $t1, 0       #B = 0
li $t2, 0       #C = 0
li $t3, 0       #D = 0
li $t4, 0       #counter = 0

loop:
and $t5, $t0, $t1   #$t5 = A and B
and $t6, $t2, $t3   #$t6 = B and C
xor $t5, $t5, $t6   #$t5 = (A and B) xor (B and C)
add $t4, $t4, $t5   #Increment the counter by 1 if F = 1

li $v0, 1

move $a0, $t0       #Print out the values of A, B, C, D, and F
syscall 
move $a1, $t1 
syscall
move $a2, $t2 
syscall
move $a3, $t3 
syscall
la $a0, space 
syscall
move $a1, $t5 
syscall
la $a2, newline
syscall

testA:                  #Test to see if A should be inversed
beq $t1, 1, testCforA           #If B = 1, test to see if C = 1
j testB                 #If B = 0, test to see if B should be inversed
testCforA: beq $t2, 1, testDforA    #If C = 1, test to see if D = 1
j testB                 #If C = 0, test to see if B should be inversed
testDforA: beq $t3, 1, inverseA     #If D = 1, A should be inversed
j testB                 #If D = 0, test to see if B should be inversed

inverseA:               #Inverse bit A
beq $t0, 1, end             #If A = 1, then end the loop because the truth table is completed
li $t0, 1               #If A = 0, then change it to make A = 1

testB:                  #Test to see if B should be inversed
beq $t2, 1, testDforB           #If C = 1, test to see if D = 1
j testC                 #If C = 0, test to see if C should be inversed
testDforB: beq $t3, 1, inverseB     #If D = 1, B should be inversed
j testC                 #If D = 0, test to see if C should be inversed

inverseB:               #Inverse bit B
beq $t1, 1, invB            #If B = 1, then change it to make B = 0
li $t1, 1               #If B = 0, then change it to make B = 1
j testC             
invB: li $t1, 0

testC:                  #Test to see if C should be inversed
beq $t3, 1, inverseC            #If D = 1, C should be inversed
j inverseD              #If D = 0, inverse bit D but DON'T inverse bit C

inverseC:               #Inverse bit C
beq $t2, 1, invC            #If C = 1, then change it to make C = 0
li $t2, 1               #If C = 0, then change it to make C = 1
j inverseD
invC: li $t2, 0

inverseD:               #Inverse bit D
beq $t3, 1, invD            #If D = 1, then change it to make D = 0
li $t3, 1               #If D = 0, then change it to make D = 1
j loop                  #jump back to the beginning of the loop
invD: li $t3, 0
j loop                  #jump back to the beginning of the loop

end:                    #termination of the program
la $a0, str             #Print the number of minterms from the truth table
syscall
move $a0, $t4
syscall

我不断收到的错误是:

00002685009922685009922685009920000268500992268500992268500992000026850099226850099226850099200002685009922685009922685009920000268500992268500992268500992000026850099226850099226850099200002685009922685009922685009920000268500992268500992268500992111126850099226850099226850099211112685009922685009922685009921111268500992268500992268500992111126850099226850099226850099211112685009922685009922685009921111268500992268500992268500992111126850099226850099226850099211112685009922685009922685009922685009946 -- 程序运行完毕(掉到底部) --

非常感谢任何见解!谢谢!!

【问题讨论】:

  • “从底部掉下来”是什么意思?这不是标准术语。
  • 这似乎是 MIPS 中的错误消息:-- program is finished running (dropped off bottom) -- 我注意到如果我不运行 `li $v0, 10 syscall` 会得到这个信息

标签: assembly mips mars-simulator


【解决方案1】:

主要的见解是使用非常好的 MARS MIPS 模拟器单步执行代码,以确定您认为它正在做什么与它实际上之间的区别> 做。 This is the home page.

很明显,输出syscalls 不可能是正确的。要打印第一个变量,您已将其加载到a0,这是正确的。然后打印第二个,你加载到a1!不可能是对的。同样,您尝试使用与打印整数相同的 syscall 函数号来打印空格和换行符。字符串为 8。整数为 1。此值必须在系统调用之前的 v0 中。

【讨论】:

  • 应该是$v0 而不是$v1 @Gene
  • @KonradLindenbach 谢谢。打字比思考快……不好。
  • 谢谢!老实说,我认为最令人困惑的部分是系统调用。我读到的很多关于他们的信息都不清楚。所以我需要把合适的值加载到$v0中,然后我把我想输出的东西放到a寄存器中,对吧?
  • 它们的工作方式类似于高级语言中的库调用。这里courses.missouristate.edu/kenvollmar/mars/help/syscallhelp.html 是一个不错的图表。使用所需功能加载v0 寄存器。如图所示,在其他寄存器中设置参数。然后是syscall 指令。返回结果的函数也将它们放在寄存器中,如图所示。请注意,无论您是否使用它们,返回结果都会更改寄存器值。您不能指望寄存器像高级语言中的局部变量一样工作。
  • 很抱歉花了这么长时间才回到这里。我又在做这个程序了。我修复了 $v0 的所有值以打印适当的值。它打印 0000(真值表的第一行)。然而,这就是所有的代码。这意味着实际循环中可能存在问题,我已经多次阅读并且无法弄清楚(顺便说一下,循环应该迭代真值表的每一行,所以 0000, 0001, 0010, 0011 , 0100 等),在到达 1111 后结束。有什么帮助吗?
【解决方案2】:

有一天我看到这个解决方案,不是专门针对您的问题,而是足以抑制我的代码中的“dropped of bottom”错误:

放在函数声明的开头:

.文本

.globl 主要

只是这个,对我有用。

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 2014-12-23
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    相关资源
    最近更新 更多