0

I want to be able to compile and dump basic asm into hex using Intel syntax. I saw an answer to another question and want to play around with it. It used GCC and objdump. But I'm getting an error I don't understand.

test.c:1:1: error: expected identifier or '(' before '\xa22293b'

I assume it's something dumb/simple. Thanks for any help.

This is the bat I'm trying to run:

cd C:\MinGW\bin
echo 'asm("mov $400835, %rax\n jmp *%rax\n");' > test.c
gcc -c test.c
objdump -M Intel -d test.o
cmd /k
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 4
    Why don't you write a plain asm file and compile that rather than trying to do inline assembly in C? – Mat Mar 29 '21 at 14:17
  • Same outcome. Unless I'm missing syntax/header info with in the asm file. Still relatively new to ASM. – user4096 Mar 29 '21 at 14:38
  • 1
    `%` has special meaning in Windows batch files. After the `echo`, verify the contents of `test.c`; you'll notice it's not what you were expecting. – Ruud Helderman Mar 29 '21 at 14:57
  • you're right, using both comments I just made test.c with asm("mov $400835, %rax"); Now I get Error: bad register name `%rax' Do I declare rax somehow? – user4096 Mar 29 '21 at 15:46
  • 1
    I assume its because I'm running in 32bit not 64bit. I'll look into how to do so. – user4096 Mar 29 '21 at 15:54
  • `echo -e 'mov $400835, %rax\n jmp *%rax' > test.s` && `gcc -m64 -c test.s` would be easier, and should work in a Bash script. Or just pipe `echo` into `gcc -c -x asm -`, or use a here document. – Peter Cordes Mar 29 '21 at 15:56

1 Answers1

0

Got it working with just using a separate file and not trying to inline. Also had to run in 64-bit to use rax.

Couldn't get below to work but thanks anyway. Thank all for your time.

echo -e 'mov $400835, %rax\n jmp *%rax' > test.s 
gcc -m64 -c test.s
  • 1
    That echo command is for bash, not clunky the DOS-style command shell. MinGW normally comes with bash; I'd recommend using it unless you actually like Window's `cmd.exe`. Those commands work for me on my x86-64 Arch GNU/Linux desktop. – Peter Cordes Mar 30 '21 at 00:49