ASM MIPS-计数:获取错误的值

时间:2019-03-19 23:06:20

标签: assembly mips mars-simulator

我正在使用Mars 4.5,ASM,并且想用用户输入创建一个三角计算器。

我想计算用户输入了多少个(给定的)值。该脚本应打印出已输入的边长和角度的数量(尚未简单地通过按Enter键确认)。

如果我输入所有值,则结果正确。我是否遗漏一个,两个计数器的结果均为0。

如果我删除了alpha / beta / gamma代码,当我将某些值排除在外时,计数器便可以工作了。当我调试它时,它看起来是否会导致

之间的无限循环
li $t8, 1

eret

有人可以告诉我为什么吗?

.data
    printGetA: .asciiz "Enter a: "
    printGetB: .asciiz "\nEnter b: "
    printGetC: .asciiz "\nEnter c: "
    printGetAlpha: .asciiz "\nEnter alpha: "
    printGetBeta: .asciiz "\nEnter beta: "
    printGetGamma: .asciiz "\nEnter gamma: "
    ResultStr: .asciiz "\nResult: "
    SidesStr: .asciiz "\nSides Given: "
    AnglesStr: .asciiz "\nAngles Given: "
.text
.globl main
main:
    li $t8, 0 # Flag that tells if the user entered something
    li $t7, 3 # Side Counter
    li $t9, 3 # Angle Counter
    la $a0, printGetA # Print Input Message A
    li $v0, 4 # Call API StdOut
    syscall
    li $v0, 7 # Call API StdIn
    syscall
    mov.d $f4, $f0 # Move a to Register f4
    beqz $t8, AOK # if not trapped (if the user entered a float) -> AOK
    li $t1, 1 # Flag a not given
    addiu $t7, $t7, -1 # Reduce side counter
    l.d $f4, 0 # Set a = 0
AOK:
    li $t8, 0 # Re-toggle input flag
    la $a0, printGetB
    li $v0, 4
    syscall
    li $v0, 7
    syscall
    mov.d $f6, $f0 # Move b to Register f6
    beqz  $t8, BOK
    li $t2, 1
    addiu $t7, $t7, -1  
    l.d $f6, 0
BOK:
    li $t8, 0
    la $a0, printGetC
    li $v0, 4
    syscall
    li $v0, 7
    syscall
    mov.d $f8, $f0 # Move c to Register f8
    beqz $t8, COK
    li $t3, 1
    addiu $t7, $t7, -1
    l.d $f8, 0
COK:    
    li $t8, 0
    la $a0, printGetAlpha
    li $v0, 4
    syscall
    li $v0, 7
    syscall
    mov.d $f10, $f0 # Move alpha to Register f10
    beqz $t8, AlphaOK
    li $t4, 1 # Flag alpha not given
    addiu $t9, $t9, -1 # Decrease angle counter
    l.d $f10, 0
AlphaOK:    
    li $t8, 0
    la $a0, printGetBeta
    li $v0, 4
    syscall
    li $v0, 7
    syscall
    mov.d $f16, $f0 # Move beta to Register f16
    beqz $t8, BetaOK
    li $t5, 1 # Flag beta not given
    addiu $t9, $t9, -1
    l.d $f16, 0
BetaOK: 
    li $t8, 0
    la $a0, printGetGamma
    li $v0, 4
    syscall
    li $v0, 7
    syscall
    mov.d $f20, $f0 # Move gamma to Register f20
    beqz $t8, GammaOK
    li $t5, 1 # Flag gamma not given
    addiu $t9, $t9, -1
    l.d $f20, 0
GammaOK:    
    li $t8, 0
####### check if input is sufficient######
    la $a0, SidesStr
    li $v0, 4 # Call API StdOut
    syscall
    move $a0, $t7 # Print sides count
    li $v0, 1 # Call API StdOut Int
    syscall
    la $a0, AnglesStr
    li $v0, 4 # Call API StdOut
    syscall
    move $a0, $t9 # Print angles count
    li $v0, 1  # Call API StdOut Int
    syscall
    li $v0, 10
    syscall # End of program
.ktext 0x80000180
   move $k0,$v0   # Save $v0 value
   move $k1,$a0   # Save $a0 value
  #la   $a0, msg  # address of string to print
  #li   $v0, 4    # Print String service
  #syscall
   move $v0,$k0   # Restore $v0
   move $a0,$k1   # Restore $a0
   mfc0 $k0,$14   # Coprocessor 0 register $14 has address of trapping instruction
   addi $k0,$k0,8 # Add 4 to point to next instruction
   mtc0 $k0,$14   # Store new address back into $14
   li $t8, 1
   eret           # Error return; set PC to value in $14
   .kdata   
msg:   
   .asciiz "An error occured."

1 个答案:

答案 0 :(得分:0)

addi $k0,$k0,8 # Add 4 to point to next instruction

实际上是剩下的调试代码

addi $k0,$k0,4 # Add 4 to point to next instruction

是解决方案。