qt spim语法解析器错误MIPS

时间:2018-02-13 02:38:42

标签: syntax-error mips qtspim

我在QT SPIM上运行了以下代码并收到此错误: spim :(解析器)语法错误在文件/Users/emily/Documents/p2.s的第4行       。字 这是MIPS中的代码,它编译并能够在火星上运行它。我不知道为什么它在QT SPIM模拟器中这样做,它必须在QT SPIM模拟器上工作。它应该递归地执行快速模幂运算。我尝试重新初始化并加载它,我很确定这是你如何正确声明数据类型。

.data

x: .word
k: .word
n:  word
result: .word 1
temp: .word
zero: .word 0 #for the if statement
Prompt1 : .asciiz "Enter the first integer x: "
Prompt2: .asciiz "Enter the second integer k: "
Prompt3: .asciiz "Enter the third integer n: "
Prompt4 .asciiz "The result of x^k mod n = %d/n "
# the rest of it will be after this

.main
li, $v0, 4
la, $a0, Prompt1
syscall

# Tell the syscall that you want to read in integer
li $v0, 5
# make the syscall
syscall
sw, $v0, x

li, $v0, 4
la, $a0, Prompt2
syscall
li $v0, 5
syscall
sw $v0, k

li $v0, 4
la $a0, Prompt3
syscall
li $v0, 5
syscall
sw $v0, n

lw $a0, x
lw $a1, k
lw $a2, n

jal fme

move $t0, $v0

#Value is now saved in $t0.
#prints the prompt
li $v0, 4
la $a0, Prompt4
syscall
#prints the int
li $v0,1
la $a0,$t0
syscall

#TODO: print the prompt and then the result (in $v0)

fme:
# Move the stack pointer by 1 word
addi $sp, $sp, -32
# Store the return address
sw $ra, ($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
sw $s3, 16($sp)
sw $s4, 20($sp)
sw $s5, 24($sp)
sw $s6, 28($sp)



move $s0, $a0   # x
move $s1, $a2   # k
move $s2, $a3   # n
lw $s3,  result # result in v0
lw $s4, $zero   # temp
# if! k > 0; skip if block
ble $s1, 0, toReturn
# if block
# set temp = fme(x, k/2, n);
# k = k/2
div $a1, $a1, 2
# call fme
jal fme
# set temp = fme
move $s4, $s3
# if k%2 == 1
# calculating k%2 by anding with 1
addi $s5, $s1, 1
bneq $s5, 1, skip
# result = x%n
div $s0, $s2
# move x%n into result
mfhi $s3

skip:
#result = (result * temp * temp) % n;
# calculate temp*temp
mul $ss6, $ss4, $ss4
# result*temp*temp
mul $s6, $s3, $s6
# (result*temp*temp)%n
div $t6, $s2
# result = (result*temp*temp)%n
mfhi $s3



toReturn:
move $v0, $s3
# Retore the ra from stack
lw $ra, ($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
lw $s3, 16($sp)
lw $s4, 20($sp)
lw $s5, 24($sp)
lw $s6, 28($sp)
addi $sp, $sp, 32
# return result
jr $ra

1 个答案:

答案 0 :(得分:0)

如果不指定初始值,您不能只说.word

有一个.space指令可以保留N个字节的空间而无需指定值,例如: .space 4保留4个字节的空间。但是,这将隐含地初始化那些值为0的字节,所以你可以简单地说.word 0而不是键入一个字符。

此外,您已将代码放在.data部分中,而您不应该这样做,而且您未能将main标签设为全局。

所以,在main标签之前,您应该添加:

.text
.globl main