如何创建一个扩展为“(x+y*240)*2”这样的表达式的 GNU GAS 宏?

发布于 2024-09-07 13:58:20 字数 457 浏览 7 评论 0原文

我正在使用 GAS 为 ARM Linux 构建一个程序,但我想做一些宏以使我的开发更加智能。然后我想知道:

我怎样才能为此做一个宏:(x+y*240)*2,是xyint,将像这样使用:

mov r0, MACRO_SHOULD_BE_CALLED_HERE

我怎样才能做一个应该像这样调用的宏:

JUST_MACRO_CALLED_HERE_TO_DO_SOMETHING

它只会做一些已经在其中定义的事情,例如打印函数。

另外,如果我需要宏或函数调用的一些参数。我怎样才能做到呢?

PS:r0是一个ARM寄存器,就像x86的eax

I'm building a program for ARM Linux using GAS, but I want to do some macros to make my development some more smart. 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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回心转意 2024-09-14 13:58:20

GAS 与NASM 比较 - 宏 显示了执行参数化宏的方法,但它是简单的替换。

GAS vs NASM comparison - Macros shows ways of doing parametrized macros, but it's simple substitutions.

萌梦深 2024-09-14 13:58:20

这是第一种类型的内联 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;
}

或者在汇编程序中,

.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.

人们通常使用“C”预处理器。

Here is an inline gcc sample of the first type.

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.

送君千里 2024-09-14 13:58:20

我从未见过像您第一个示例那样支持宏的汇编器。第二个例子非常简单——即使是最基本的汇编器文档也应该涵盖它。对于 GNU as,您可能想要这样的东西:

.macro JUST_MACRO_CALLED_HERE_TO_DO_SOMETHING
    ...
.endm

用您想要的任何指令代替 ...

请小心汇编器宏,不要破坏用于保存重要数据的一堆寄存器。通常函数调用是解决这些问题的更好方法。

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:

.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文