使用 PIC16 汇编中的宏创建高级 IF ELSE ENDIF
我尝试在 PIC16F84 的汇编中模拟 IF() .... ELIF .... ENDIF ,但它似乎不适用于多种用途。我尝试在两个地方使用类似的东西,但它给出了一些标签重复的错误。宏中的参数是否也应该在标签中替换? (真实姓名中的姓名:)
_f macro name
btfsc EQUAL,0
goto true_name
goto false_name
true_name:
endm
_lse macro name
goto next_name
false_name:
endm
_ndif macro name
goto next_name
next_name:
endm
;; usage example
_f label1
...
_lse label1
...
_ndif
I tried to simulate IF() .... ELIF .... ENDIF in assembly for PIC16F84, but it doesn't seem to work for more than one usage. I tried to use something like this in two places, but it gives some error that a label is duplicated. Shouldn't the parameter from the macro be replaced in the labels too? (name in true_name:)
_f macro name
btfsc EQUAL,0
goto true_name
goto false_name
true_name:
endm
_lse macro name
goto next_name
false_name:
endm
_ndif macro name
goto next_name
next_name:
endm
;; usage example
_f label1
...
_lse label1
...
_ndif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为一个人可以做得更好一点。下面是一些可以嵌套五层的 if-else-endif 宏。不幸的是,我无法像我希望的那样定义 if1、if2..,因为汇编器不接受“#ifndef if#v(lvl)”,因此宏将嵌套级别限制为 5深的。这些符号计算给定嵌套级别的 If 数量,以便可以附加唯一的标签。包括一个无意义的例子。
I think one can do a bit better. Here's some if-else-endif macros that can be nested five deep. Unfortunately, I was not able to make the definitions of if1, if2.. as nice as I would like since the assembler does not accept "#ifndef if#v(lvl)" so the macros as they stand limit the nesting level to five deep. These symbols count the number of Ifs at a given nesting level so unique labels can be attached. A nonsense example is included.
不要使用从一个宏到另一个宏的跳转,这是危险的。
无需使用独特的标签。
在 MPLAB 下有两种方法可以做到这一点:
1) 使用 LOCAL 伪指令的情况
2) 使用 $ 作为当前存储器地址指针的情况。
Do not use jumps from one macro to another, it is dangerous.
There is no need to use unique labels.
Two ways haw to do this under MPLAB:
1)Case with LOCAL directive
2)Case with $ as current memory address pointer.
我用 MPLAB 变量解决了这个问题,下面是一个测试寄存器和文字之间相等性的示例:
请注意,我没有使用
goto _true#v(name)
和_true#v (name):
标签,您只需确定是否需要btfss
还是btfsc
。您可以使用单个
_lse
和_ndif
宏,并为_f
语句使用多个宏。GJ 的解决方案没有
next
标签,因此 true 分支将执行 false 分支。您需要为每个 if-else-endif 构造定义一个变量。
如果变量名描述了 if-else-endif 的用途,它甚至可能有用。
示例:
变量testing_something=123
I kinda solved this problem with MPLAB variables, here's an example for testing equality between a register and a literal:
Notice that I didn't use
goto _true#v(name)
and_true#v(name):
label, you'll just have to figure if you needbtfss
orbtfsc
.You can have a single
_lse
and_ndif
macro, and have multiple macros for_f
statements.GJ's solution doesn't have a
next
label, so the true branch will execute the false branch.You need to define a variable for each if-else-endif construct.
It might even useful if the variable name describes what the if-else-endif is used for.
Example:
variable testing_something=123