Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 793
Nathan Campos Avatar asked Jun 27 '10 16:06

Nathan Campos


3 Answers

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

like image 170
Joe Avatar answered Nov 07 '22 02:11

Joe


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.

like image 3
artless noise Avatar answered Nov 07 '22 02:11

artless noise


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.

like image 1
Carl Norum Avatar answered Nov 07 '22 00:11

Carl Norum