Putting assembly code into Macro creates an unknown token in expression error
Putting assembly code into Macro creates an unknown token in expression error
So, I created a quine in Assembly. And wanted to put parts of the code into a macro. However as soon as I do that, I get a warning saying:
instantiation :9:6: error: unknown token in expression
mov , %rcx
Grace.s:28:1: note: while in macro instantiation
notmain(x)
Here is the code in question with the macro (Gets error)
.macro nomain
_main:
push %rbp
mov %rsp, %rbp
lea name(%rip), %rdi
lea write(%rip), %rsi
call _fopen
mov %rax, %rdi
mov $10, %rdx
mov $9, %rcx
mov $34, %r8
lea quine(%rip), %r9
lea quine(%rip), %rsi
call _fprintf
leave
ret
.endmacro
.data
quine: .string ".data%1$cquine: .string %3$c%4$s%3$c%1$cname: .string %3$cGrace_kid.s%3$c%1$cwrite: .string %3$cw%3$c%1$c%1$c.text%1$c.globl _main%1$c#main function%1$c_main:%1$c%2$cpush %%rbp%1$c%2$cmov %%rsp, %%rbp%1$c%2$clea name(%%rip), %%rdi%1$c%2$clea write(%%rip), %%rsi%1$c%2$ccall _fopen%1$c%2$cmov %%rax, %%rdi%1$c%2$cmov $10, %%rdx%1$c%2$cmov $9, %%rcx%1$c%2$cmov $34, %%r8%1$c%2$clea quine(%%rip), %%r9%1$c%2$clea quine(%%rip), %%rsi%1$c%2$ccall _fprintf%1$c%2$cleave%1$c%2$cret%1$c"
name: .string "Grace_kid.s"
write: .string "w"
.text
.globl _main
#main function
nomain
and without macro (Works)
.data
quine: .string ".data%1$cquine: .string %3$c%4$s%3$c%1$cname: .string %3$cGrace_kid.s%3$c%1$cwrite: .string %3$cw%3$c%1$c%1$c.text%1$c.globl _main%1$c#main function%1$c_main:%1$c%2$cpush %%rbp%1$c%2$cmov %%rsp, %%rbp%1$c%2$clea name(%%rip), %%rdi%1$c%2$clea write(%%rip), %%rsi%1$c%2$ccall _fopen%1$c%2$cmov %%rax, %%rdi%1$c%2$cmov $10, %%rdx%1$c%2$cmov $9, %%rcx%1$c%2$cmov $34, %%r8%1$c%2$clea quine(%%rip), %%r9%1$c%2$clea quine(%%rip), %%rsi%1$c%2$ccall _fprintf%1$c%2$cleave%1$c%2$cret%1$c"
name: .string "Grace_kid.s"
write: .string "w"
.text
.globl _main
#main function
_main:
push %rbp
mov %rsp, %rbp
lea name(%rip), %rdi
lea write(%rip), %rsi
call _fopen
mov %rax, %rdi
mov $10, %rdx
mov $9, %rcx
mov $34, %r8
lea quine(%rip), %r9
lea quine(%rip), %rsi
call _fprintf
leave
ret
.macro
#define
Assembles fine in my version of
gas
(v2.22) after I fix the .endmacro
to .endm
.– Jester
Sep 14 '18 at 0:15
gas
.endmacro
.endm
@Jester That didn't fix it for me, ill check if I have the same gas version as you
– Stanislas Juery
Sep 14 '18 at 0:20
@PeterCordes Added the full code with and without the macro. It seems the compiler dosen't see the $9 portion of "mov $9, %rcx" when I put it inside the macro.
– Stanislas Juery
Sep 14 '18 at 0:21
Since you seem to be using XCODE and possibly an older clang you may find yourself having to use
$$
instead of $
inside macros when dealing with immediate values.– Michael Petch
Sep 14 '18 at 0:26
$$
$
0
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Include the code directly into your question, so we know it's a GAS
.macro
as opposed to a CPP#define
macro (which would also work in a .S file). SO requires questions to be self-contained. (You might want to make a Minimal, Complete, and Verifiable example of just the part of the macro that gives the error, without the full quine.)– Peter Cordes
Sep 14 '18 at 0:11