[英]How to create a GNU GAS macro that expands to an expression like “(x+y*240)*2”?
I'm building a program for ARM Linux using GAS, but I want to do some macros to make my development some more smart. 我正在使用GAS为ARM Linux构建一个程序,但我想做一些宏来使我的开发更加智能。 Then I want to know:
然后我想知道:
How could I do a macro for this: (x+y*240)*2
, were x
and y
are int
, that will be used like this: 我怎么能为此做一个宏:
(x+y*240)*2
, x
和y
是int
,将使用如下:
mov r0, MACRO_SHOULD_BE_CALLED_HERE
And how could I do a macro that should be called like this: 我怎么能做一个应该像这样调用的宏:
JUST_MACRO_CALLED_HERE_TO_DO_SOMETHING
That will just do something that is already defined inside it, like a print function for example. 这只会做一些已在其中定义的内容,例如打印功能。
Also, if I need some arguments on the macro or a function call. 另外,如果我需要关于宏或函数调用的一些参数。 How I could do it?
我怎么能这样做?
PS: r0
is an ARM register, like eax
of x86 PS:
r0
是ARM寄存器,类似于x86的eax
GAS vs NASM比较 - 宏显示了执行参数化宏的方法,但它是简单的替换。
Here is an inline gcc sample of the first type. 这是第一种类型的内联gcc样本。
int foo(unsigned short *p)
{
int c;
asm(".macro pixw nm, x, y\n"
" .set \\nm, (\\x+\\y*240)*2\n"
".endm\n"
"pixw pixo,1,2\n"
"ldrh %0, [%1, #pixo]\n" : "=r" (c) : "r" (p));
return c;
}
Or in assembler, 或者在汇编程序中,
.macro pixw nm, x, y
.set \nm, (\x+\y*240)*2
.endm
pixw pix10_2,10,2 ; variable pixo is macro as parameters
ldrh r0, [r1, #pix10_2] ; get pixel offset.
Often people use a 'C' pre-processor instead. 通常人们会使用“C”预处理器。
I've never seen an assembler that supported macros like you want for your first example. 我从来没有见过一个支持宏的汇编程序,就像你想要的第一个例子一样。 The second example is pretty straightforward though - even the most basic assembler documentation should cover it.
第二个例子非常简单 - 即使最基本的汇编程序文档也应该涵盖它。 For GNU
as
, you probably want something like: 对于GNU
as
,您可能需要以下内容:
.macro JUST_MACRO_CALLED_HERE_TO_DO_SOMETHING
...
.endm
Put whatever instructions you want in place of the ...
. 把你想要的任何指示代替
...
Be careful with assembler macros that you don't stomp on a bunch of registers that you were using to hold important data. 小心汇编程序宏,你不要踩在一堆用于保存重要数据的寄存器上。 Usually a function call is a better way to solve these problems.
通常函数调用是解决这些问题的更好方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.