如何在 MPLAB X 中使用 mips 汇编为每个宏调用创建唯一标签 [英] How can one create a unique label for each macro call using mips assembly in MPLAB X
问题描述
我有一个执行比较的宏,如果比较不正确,则跳转到宏的末尾.这是一个简化的例子:
I have a macro which performs a comparison, and jumps to the end of the macro if that comparison isn't true. Here is a simplified example:
.macro do_work_if_value_not_zero value
li s0, value
bne s0, zero, exit_label
nop
// Do work
exit_label:
.endm
然而,问题是我多次调用这个宏:
The issue however is that I call this macro several times like so:
do_work_if_value_not_zero 5
do_work_if_value_not_zero 3
do_work_if_value_not_zero 12
结果我收到以下错误:
错误:符号`exit_label'已经定义
Error: symbol `exit_label' is already defined
因为我从不多次使用相同的参数调用宏,所以我尝试使用参数名称与:"连接来创建一个唯一的标签,如下所示:
Because I never call the macro with the same parameter multiple times, I attempted to use the parameter name concatenated with a ':' to create a unique label like so:
.macro do_work_if_value_not_zero value
li s0, value
bne s0, zero, \value
nop
// Do work
\value:
.endm
但是这似乎不起作用,而且我收到了更多错误.
However this did not seem to work, and I received more errors.
因此我的问题是,如何为每个宏调用创建唯一的退出标签以避免此问题?
Thus my question is, how can one create a unique exit label for each macro call to avoid this issue?
推荐答案
大多数汇编器允许这样的局部标签:
Most assemblers allow local labels like this:
.macro do_work_if_value_not_zero
li s0, value
bne s0, zero, 1f # 1f means branch forward to the next label '1:'
nop
// Do work
1:
.endm
来自 MIPS 汇编器手册 此处:
From the MIPS assembler manual here:
生成的标签是单个数值 (1...255).参考一个生成的标签,立即放入 f(向前)或 a b(向后)在数字之后.该引用告诉汇编器查找最近生成的标签,对应于中的数字词汇向前或向后.
A generated label is a single numeric value (1...255). To reference a generated label, put an f (forward) or a b (backward) immediately after the digit. The reference tells the assembler to look for the nearest generated label that corresponds to the number in the lexically forward or backward direction.
这篇关于如何在 MPLAB X 中使用 mips 汇编为每个宏调用创建唯一标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!