开发者

How to create a GNU GAS macro that expands to an expression like "(x+y*240)*2"?

I'm b开发者_如何转开发uilding 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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜