samuelbrody1249
samuelbrody1249

Reputation: 4767

Defining a debugging macro in assembly (with gas?)

For debugging outside a debugger, I'll often add in basic print statements, for example something like:

    str: .string "Hello!\n"

    mov $1,     %rdi
    mov $str,   %rsi
    mov $7,     %rdx
    mov $1,     %eax
    syscall

Is there a way to create a macro where I can instead write it like:

print ($str, $7)

And it would automatically expand to:

mov $1,     %rdi
mov $1,     %eax
mov $str,   %rsi
mov $7,     %rdx
syscall

I've looked at the macro docs for gas but it seems like it's using a syntax that I'm not sure where it's defined (it seems like they are using for loops?). Would it be possible to do the above in asm? I suppose if not I could create a snippet in vim that would 'expand' to the above.


Thanks for the suggestion. The tested macro I used to print the above is:

.macro DEBUG str_addr, str_len
    mov $1,         %edi
    mov $\str_addr, %rsi
    mov $\str_len,  %edx
    mov $1,         %eax
    syscall
.endm

DEBUG str, 7

While the syntax is a bit odd, for 'normal instruction' (and not a loop or conditional, for example), the macro syntax is called as:

macro arg1, arg2

And defined as:

.macro <macro_name> <arg1>, <arg2>, ...

.endm

Variable names need to be escaped with \. So for example to reference arg1 you have to do \arg1.

Upvotes: 0

Views: 409

Answers (1)

paulsm4
paulsm4

Reputation: 121699

Sure. You should be able to do something like this (I don't have gas handy, so it's untested):

.macro myprint s, len
    mov $1,     %rdi
    mov $\s,    %rsi
    mov $\len,  %rdx
    mov $1,     %eax
    syscall
.endm

Then you'd call it like this:

 myprint str, 7

Upvotes: 3

Related Questions