Mars MIPS 模拟器 4.5 锁定用户输入
Mars MIPS Simulator 4.5 Locks up on User Inpur
有人使用火星 SPIM 模拟器吗?我在学校用它来编写小型 MIPS 32 程序,我喜欢用 Mars 进行调试。但是,我有一个程序可以在 QtSPIM 模拟器上运行,但会锁定 Mars 模拟器。我已经从下面的代码中删除了不相关的代码。在 Mars 中,当提示用户输入一个字符串时,模拟器接受该字符串的一个字符并停止。 QtSPIM 接受整个字符串。 (注意:如果某些初始化看起来很奇怪,请忽略它,我在删除的代码中将其用于其他调试目的)。在这一点上,我想知道我是否超过了火星内存限制?有没有人有办法解决吗 ?
###############################################################
#
# Runninng on Windows 7 64-bit
#
# Works fine on QtSPIM v9.1.17
#
# Locks up on user input in Mars 4.5
#
# Program Start.
#
###############################################################
.data
banner1: .asciiz " From Rome to Arabia "
banner2: .asciiz "Roman Numeral to Arabic Numeral Converter"
prompt1: .asciiz "Enter the Roman Numeral : "
newline: .asciiz "\n"
output1: .asciiz "\nThe Arabic equivalent is "
remind1: .asciiz "Uppercase characters IVXLCDM are valid."
terminate: .asciiz "Program terminated.\n"
userString:
.space 16 # Def 16 bytes for user input string
stack:
.space 16 # Def 16 bytes for the stack
.globl main
.text
###############################################################
#
# Begin program execution.
#
###############################################################
main:
li $v0, 4 # Load Syscall to print string
la $a0, banner1 # Print the program title banner
syscall # Execute the syscall instruction
li $v0, 4 # Load Syscall to print string
la $a0, newline # Print blank line for aesthetics
syscall # Execute the syscall instruction
li $v0, 4 # Load Syscall to print string
la $a0, newline # Print blank line for aesthetics
syscall # Execute the syscall instruction
li $v0, 4 # Load Syscall to print string
la $a0, remind1 # Remind user of valid int values
syscall # Execute the syscall instruction
li $v0, 4 # Load Syscall to print string
la $a0, newline # Print blank line for aesthetics
syscall # Execute the syscall instruction
###############################################################
#
# Initialize the variables used throughout this program.
#
###############################################################
move $t0, [=10=] # Initialize the newline character
# value to zero (0)
move $t1, [=10=] # Init the initial stack pointer
# address value to zero (0)
move $t2, [=10=] # Init the initial stack pointer
# address value to zero (0)
move $t3, [=10=] # Init the address of the user
# input string value to zero (0)
move $t4, [=10=] # Init the current character
# address byte value to zero (0)
move $t5, [=10=] # Init the previous decimal
# equivalent value to zero (0)
move $t6, [=10=] # Init the sum of the decimal
# equivalent values to zero (0)
move $t7, [=10=] # Init the procedure variable
# for the decimal value to zero (0)
move $t7, [=10=] # Init the procedure variable
# for the character value to zero (0)
move $a0, [=10=] # Init the storage variable
# for the return value to zero (0)
move $v0, [=10=] # Init the storage variable
# for the syscal values to zero (0)
###############################################################
#
# Get Roman Numeral string from the user.
#
###############################################################
li $v0, 4 # Load Syscall to print string
la $a0, newline # Print blank line for aesthetics
syscall # Execute the syscall instruction
li $v0, 4 # Load Syscall to print string
la $a0, prompt1 # Prompt for Roman Numeral string
syscall # Execute the syscall instruction
addi $t0, $zero, 10 # Store the ASCII newline character
la $t1, stack # Load stack pointer address to $t1
move $t2, $t1 # Load init stack pointer address to
# $t2 for reference.
la $t3, userString # Load address of user input string
li $v0, 8 # Load Syscall to read string
move $a0, $t3 # Move input address to syscall var
syscall # Execute the syscall instruction
lb $t4, 0($t3) # Load current character address and
# sign extend to $t4
bne $t3, $t0, PUSH # Check for newline terminator before
# evaluating string. If user entered
# some characters then jump to the
# PUSH label
j DONE # User entered a blank string so end
# the program
###############################################################
#
# NOTE:
# In my opinion, the algorithm to convert from Roman Numerals to
# their Arabic (or, decimal ) equivalents is better accomplished
# once the string value of the Roman Numeral is reversed. This
# is a design decision that was followed below.
#
###############################################################
###############################################################
#
# Push the characters of the user string on to a stack until the
# newline character is encounterd.
#
###############################################################
PUSH:
sb $t4, 0($t1) # Add current character to the stack
addi $t1, $t1, 1 # Increment the stack pointer
addi $t3, $t3, 1 # Get current character address
lb $t4, 0($t3) # Load current character value
bne $t4, $t0, PUSH # If the current character value is
# the newline character then all of
# the Roman Numerals are on the
# stack and ready for evaluation
###############################################################
#
# Initialize the sum of the decimal equivalent values and the previous
# decimal equivalent value to zero (0)
#
###############################################################
move $t6, [=10=] # Initialize the sum of the decimal
# equivalent values to zero (0)
move $t5, [=10=] # Initialize the previous decimal
# equivalent value to zero (0)
###############################################################
#
# Display a message to the user and terminate the program.
#
###############################################################
DONE:
li $v0, 4 # Load Syscall to print string
la $a0, newline # Print a blank line for aesthetics
syscall # Execute the syscall instruction
li $v0, 4 # Load Syscall to print string
la $a0, terminate # Program termination string
syscall # Execute the syscall instruction
li $v0, 10 # Syscall 10 for program exit
syscall # Execute the syscall instruction
您没有正确使用系统调用 8。来自 the documentation:
Arguments
$a0 = address of input buffer
$a1 = maximum number of characters to read
因此,当将字符串读入 userString
时,您需要在 syscall
之前使用 li $a1, 16
。
有人使用火星 SPIM 模拟器吗?我在学校用它来编写小型 MIPS 32 程序,我喜欢用 Mars 进行调试。但是,我有一个程序可以在 QtSPIM 模拟器上运行,但会锁定 Mars 模拟器。我已经从下面的代码中删除了不相关的代码。在 Mars 中,当提示用户输入一个字符串时,模拟器接受该字符串的一个字符并停止。 QtSPIM 接受整个字符串。 (注意:如果某些初始化看起来很奇怪,请忽略它,我在删除的代码中将其用于其他调试目的)。在这一点上,我想知道我是否超过了火星内存限制?有没有人有办法解决吗 ?
###############################################################
#
# Runninng on Windows 7 64-bit
#
# Works fine on QtSPIM v9.1.17
#
# Locks up on user input in Mars 4.5
#
# Program Start.
#
###############################################################
.data
banner1: .asciiz " From Rome to Arabia "
banner2: .asciiz "Roman Numeral to Arabic Numeral Converter"
prompt1: .asciiz "Enter the Roman Numeral : "
newline: .asciiz "\n"
output1: .asciiz "\nThe Arabic equivalent is "
remind1: .asciiz "Uppercase characters IVXLCDM are valid."
terminate: .asciiz "Program terminated.\n"
userString:
.space 16 # Def 16 bytes for user input string
stack:
.space 16 # Def 16 bytes for the stack
.globl main
.text
###############################################################
#
# Begin program execution.
#
###############################################################
main:
li $v0, 4 # Load Syscall to print string
la $a0, banner1 # Print the program title banner
syscall # Execute the syscall instruction
li $v0, 4 # Load Syscall to print string
la $a0, newline # Print blank line for aesthetics
syscall # Execute the syscall instruction
li $v0, 4 # Load Syscall to print string
la $a0, newline # Print blank line for aesthetics
syscall # Execute the syscall instruction
li $v0, 4 # Load Syscall to print string
la $a0, remind1 # Remind user of valid int values
syscall # Execute the syscall instruction
li $v0, 4 # Load Syscall to print string
la $a0, newline # Print blank line for aesthetics
syscall # Execute the syscall instruction
###############################################################
#
# Initialize the variables used throughout this program.
#
###############################################################
move $t0, [=10=] # Initialize the newline character
# value to zero (0)
move $t1, [=10=] # Init the initial stack pointer
# address value to zero (0)
move $t2, [=10=] # Init the initial stack pointer
# address value to zero (0)
move $t3, [=10=] # Init the address of the user
# input string value to zero (0)
move $t4, [=10=] # Init the current character
# address byte value to zero (0)
move $t5, [=10=] # Init the previous decimal
# equivalent value to zero (0)
move $t6, [=10=] # Init the sum of the decimal
# equivalent values to zero (0)
move $t7, [=10=] # Init the procedure variable
# for the decimal value to zero (0)
move $t7, [=10=] # Init the procedure variable
# for the character value to zero (0)
move $a0, [=10=] # Init the storage variable
# for the return value to zero (0)
move $v0, [=10=] # Init the storage variable
# for the syscal values to zero (0)
###############################################################
#
# Get Roman Numeral string from the user.
#
###############################################################
li $v0, 4 # Load Syscall to print string
la $a0, newline # Print blank line for aesthetics
syscall # Execute the syscall instruction
li $v0, 4 # Load Syscall to print string
la $a0, prompt1 # Prompt for Roman Numeral string
syscall # Execute the syscall instruction
addi $t0, $zero, 10 # Store the ASCII newline character
la $t1, stack # Load stack pointer address to $t1
move $t2, $t1 # Load init stack pointer address to
# $t2 for reference.
la $t3, userString # Load address of user input string
li $v0, 8 # Load Syscall to read string
move $a0, $t3 # Move input address to syscall var
syscall # Execute the syscall instruction
lb $t4, 0($t3) # Load current character address and
# sign extend to $t4
bne $t3, $t0, PUSH # Check for newline terminator before
# evaluating string. If user entered
# some characters then jump to the
# PUSH label
j DONE # User entered a blank string so end
# the program
###############################################################
#
# NOTE:
# In my opinion, the algorithm to convert from Roman Numerals to
# their Arabic (or, decimal ) equivalents is better accomplished
# once the string value of the Roman Numeral is reversed. This
# is a design decision that was followed below.
#
###############################################################
###############################################################
#
# Push the characters of the user string on to a stack until the
# newline character is encounterd.
#
###############################################################
PUSH:
sb $t4, 0($t1) # Add current character to the stack
addi $t1, $t1, 1 # Increment the stack pointer
addi $t3, $t3, 1 # Get current character address
lb $t4, 0($t3) # Load current character value
bne $t4, $t0, PUSH # If the current character value is
# the newline character then all of
# the Roman Numerals are on the
# stack and ready for evaluation
###############################################################
#
# Initialize the sum of the decimal equivalent values and the previous
# decimal equivalent value to zero (0)
#
###############################################################
move $t6, [=10=] # Initialize the sum of the decimal
# equivalent values to zero (0)
move $t5, [=10=] # Initialize the previous decimal
# equivalent value to zero (0)
###############################################################
#
# Display a message to the user and terminate the program.
#
###############################################################
DONE:
li $v0, 4 # Load Syscall to print string
la $a0, newline # Print a blank line for aesthetics
syscall # Execute the syscall instruction
li $v0, 4 # Load Syscall to print string
la $a0, terminate # Program termination string
syscall # Execute the syscall instruction
li $v0, 10 # Syscall 10 for program exit
syscall # Execute the syscall instruction
您没有正确使用系统调用 8。来自 the documentation:
Arguments
$a0 = address of input buffer $a1 = maximum number of characters to read
因此,当将字符串读入 userString
时,您需要在 syscall
之前使用 li $a1, 16
。