5

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

Nathan Campos
  • 27,805
  • 59
  • 192
  • 295

3 Answers3

3

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.

artless noise
  • 19,723
  • 5
  • 59
  • 95
  • `.set` is what I needed. Thanks. Docs explicitly state that it is okay to `.set` the same symbol multiple times in an assembly, in case anyone is uneasy with using it in a macro. – doug65536 Oct 25 '16 at 10:06
  • The intent of this macro/code is to address a 16-bit display with a width of 240. This is a common embedded memory mapped display. It is also seems to be what the OP wanted. You could use it to access similar banks of registers; for instance a multi-SPI, EHCI, MII, etc. It is basically a way to handle a two-dimensional array or an array of 'structs', where x is the offset and y would be the count. The display use is why I originally looked at this question and did not find a sufficiently good answer. – artless noise Oct 21 '20 at 17:45
3

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

Joe
  • 40,031
  • 18
  • 107
  • 123
  • 7
    Link is down now, but it's still availabe [at the wayback machine](http://web.archive.org/web/20090215122116/http://www.ibm.com/developerworks/library/l-gas-nasm.html). – Geier Jul 15 '14 at 09:38
1

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.

Carl Norum
  • 210,715
  • 34
  • 410
  • 462