我对MIPS相对较新,并且使用SPIM作为我的编译器。我试图写的程序接受用户输入的十进制整数,并确定它的二进制表示中有多少个零和一个:
.data
ques: .asciiz "Enter a decimal integer:\n"
zer: .asciiz "Number of 0's\n"
one: .asciiz "Number of 1's\n"
buf: .word 16
ans: .word 40
.text
.globl main
#Prompt user for number
main:
la $a0, ques #asks user to enter decimal integer
li $v0, 4
syscall
#Read the input
li $v0, 5 #reads user input
syscall
move $s3, $v0 #stores user input in register
jr count #jumps to count function
count:
addi $s7, $s7, 1 #increments with each run of the loop
beq $s7, 32, end #checks if all parts of the binary integer have been checked
andi $s0, $s3, 1 #ands one to binary number
beq $s0, 0, zeros #goes to incrementing function for 0
beq $s0, 1, ones #goes to incrementing function for 1
zeros:
addi $s5, $s5, 1 #increments 0 counter by one
srlv $s3, $s3, 1 #shifts binary digit right by one
j count #returns to count function
ones:
addi $s6, $s6, 1 #increments 1 counter by one
srlv $s3, $s3, 1 #shifts binary digits right by one
j count #returns to count function
end:
la $a0, zer #Prints zeros header
li $v0, 4
syscall
la $a3, $s5 #prints number of zero's
li $v0, 1
syscall
la $a0, one #prints ones header
li $v0, 4
syscall
la $a3, $s6
li $v0, 1
syscall
jr $31 #end program
我的问题是,当我尝试打印程序的结果时,出现语法错误:
spim: (parser) syntax error on line 43 of file test.mal
la $a3, $s5 #prints number of zero's
^
我已经尝试了很多方法来解决这个问题,但我真的不确定问题是什么。任何的意见都将会有帮助。
答案 0 :(得分:1)
la
是“加载地址”,它希望第二个参数是标签。我想你只想在这里使用一个简单的move $a3, $s5
。另外,如果我没记错的话,对于系统调用1,你想把整数打印在$ a0,而不是$ a3。最后,我发现MARS 很多比SPIM更容易使用:http://courses.missouristate.edu/KenVollmar/mars/