【问题标题】:qt spim syntax parser error MIPSqt spim 语法解析器错误 MIPS
【发布时间】:2018-07-23 08:11:02
【问题描述】:

我在 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

【问题讨论】:

  • 除了我在回答中指出的事情之外,您还有一堆错别字..就像在word 之前缺少.,在一些标签之后缺少:,应该是.main 之前不在那里。可能还有更多问题,这些只是显而易见的问题。

标签: syntax-error mips qtspim


【解决方案1】:

你不能只说.word而不指定初始值。

有一个 .space 指令可以保留 N 字节空间,而无需指定值,例如.space 4 保留 4 个字节的空间。但这会隐式初始化那些值为 0 的字节,所以你可以直接说 .word 0,这样可以少输入一个字符。


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

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

.text
.globl main

【讨论】:

    猜你喜欢
    • 2015-07-09
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多