assembly - 在 MIPS 宏中传递值

标签 assembly macros mips

我一直在研究 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/

相关文章:

c++ - 是否可以从宏定义宏

c - 如何将类型分配给宏定义的数字?

C 宏一次性定义多个变量

c++ - 无法在 MIPS 中获得正确的输出

c - MIPS - 帮助从 C 转换代码

linux - 我不明白 "and ax, 1111111100000000b"指令在做什么

c - 我的 (AT&T) 程序集 (x86-x64) 代码应该增加但不增加

assembly - Objdump 在编译的程序集上将 fsubrp 交换为 fsubp?

assembly - MIPS 和 ARM 数据路径之间的区别

c++ - 当我使用 void 函数的返回值(通过转换函数指针)时究竟会发生什么?