Question

I am having a lot of trouble getting this tiny macro to work.

.macro int_kernel
    subl $4, %esp
    int $0x80
    addl $4, %esp
.endm

Running the assembler, I get

as -arch i386 upper.s -o ./upper.o
upper.s:51:expecting operand before ','; got nothing
upper.s:51:suffix or operands invalid for `int'
upper.s:51:expecting operand before ','; got nothing

The errors come from osx macro syntax using $0,$1,... to refer to the macro's parameters.
What is the correct osx syntax for the constants in that macro?

Solution: Two dollars signs must be used as in subl $$4, %esp

Was it helpful?

Solution

This is the correct syntax for a gas macro. It uses no parameters. The 3 instructions are correct as written.

The is that your as is not GNU. It's a distant relative, with a lot of modifications by Apple, so you can't rely on the GNU documentation to explain anything. For example, try to look up the -arch option in the GNU documentation... it doesn't exist.

You're going to have to refer to Apple's documentation. They keep a copy of it here: https://developer.apple.com/library/archive/documentation/DeveloperTools/Reference/Assembler/

One simple difference difference is that they have changed the end-of-macro directive from .endm to .endmacro. Besides that, yes it looks like they replace $0, $1, and so on with macro arguments instead of using named arguments like GNU as does. And they don't tell you how to include a literal $ in the macro output! (How did the documentation author overlook this question? It can't be a rare thing to want to do)

In summary, since Apple has modified the assembler with a completely different macro syntax and failed to document it properly we're left guessing. I'd try putting a space after each dollar sign, hoping that will get them through the macro process unharmed while not damaging their ability to be recognized as immediate operand signifiers. If that didn't work, my next attempt would be putting a backslash before the dollar sign, then doubling the dollar sign, then finding out whether the real GNU assembler can be installed on MacOS instead of the hacked-and-underdocumented Apple version...

OTHER TIPS

gas does not use any special syntax to identify constants -- they're just numbers. So you probably want something like:

.macro int_kernel
    subl 4, %esp
    int 0x80
    addl 4, %esp
.endm
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top