我一直在研究 MIPS 作为汇编的入门,最近开始使用宏。我想要做的是将另一个宏生成的值或任何特定寄存器中的任何值传递到另一个宏中。即,
.data
.macro print_int(%x) #this macro prints a given integer %x
li $v0, 1
li $a0, %x
syscall
.end_macro
.macro terminate #this macro terminates a program
li $v0, 10
syscall
.end_macro
.text
main:
li $t0, 7 #first, load the value 7 into register t0
print_int($t0) #then, attempt to pass the value in $t0 through print_int
terminate #end program
该程序(特别是 print_int($t0)
行)未通过汇编程序并显示为错误。是否有适当的语法方法可以做到这一点,或者我的方法从根本上是错误的?
最佳答案
纠正错误永远不会太晚!你有很多错误。首先,您不能使用寄存器值作为第二个参数来调用li
。那么,您应该使用 $x
而不是 %x
。最后,宏位于 .text
部分内(请参阅 MIPS )。 (另请注意,QtSpim 不支持宏。但是 MARS 支持它们。)
.text
.macro print_int($x)
#lw $a0, $x #alternative call
la $a0, ($x)
li $v0, 1
syscall
.end_macro
.macro terminate
li $v0,10
syscall
.end_macro
main:
li $t0, 7
print_int($t0) #call print macro!
terminate #end program
关于assembly - 在 MIPS 宏中传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24110050/