MIPS 汇编程序语法错误

问题描述

我正在编写一个 MIPS 程序,用于在 MARS 4.5 上输入一个数字并打印 N,N-1,N-2,...,1

我的代码看起来逻辑正确,但语法很弱。

问题是程序不工作:

  • 输入:3
  • 预期:321
  • 实际:“--程序运行完毕(从底部掉下来)--

帮我解决

.text 

.data
msg: .ascii "Enter a number: "
msg1: .ascii "Final result: "

.globl main
main:
    # Print the msg
    li $v0,4   # $v0 code 4 mean syscall print an string
    la $a0,msg # la = load address of the msg
    syscall     # calling
    
    
    # Get number from keybroad
    li $v0,5   # $v0 code 5 mean read integer
    syscall     # calling
    move $t0,$v0   # store number to $t0
    
    
    # Calling the callee (function for print 1,2,3,..,Number)
    jal function
    

# Function for print N,1
function:
    li $v0,1   # Calling a print
    move $a0,$t0   # Store current number in $a0
    syscall     
    sub $t0,$t0,1 # Number = number - 1 after print (t0 = t0 - 1)
    
    beq $t0,1,exit:   # If number == 1 -> exit
    j function      # Calling

exit:
    # Exit program
    li $v0,10
    syscall

谢谢大家。

解决方法

最终答案:

.data
msg: .asciiz "Enter a number: "
msg1: .asciiz "Final result: "
char: .asciiz ","

.text 
.globl main
main:
    # Print the msg
    li $v0,4   # $v0 code 4 mean syscall print an string
    la $a0,msg # la = load address of the msg
    syscall     # calling
    
    # Get number from keybroad
    li $v0,5   # $v0 code 5 mean read integer
    syscall     # calling
    move $t0,$v0   # store number to $t0
    
    # Print the msg
    li $v0,msg1    # la = load address of the msg
    syscall 
    
    # Calling the callee (function for print 1,2,3,..,Number)
    jal function
    

# Function for print N,N-1,...,1
function:   
    li $v0,1   # Calling a print
    move $a0,$t0   # Store current number in $a0
    syscall
        
    sub $t0,$t0,1 # Number = number - 1 after print (t0 = t0 - 1)
    
    li $t3,0
    beq $t0,$t3,exit  # If number == 1 -> exit
    
    li $v0,char    # la = load address of the msg
    syscall
    
    j function      # Calling

exit:
    # Exit program
    li $v0,10
    syscall

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...