I’m trying to call the system call write with the inline assembler (gcc 4.2.1, Mac OS 10.7.5) the code compiles successfully but there isn’t any output at all. This is the complete code:
int main()
{
char s[] = "Test\n";
// write(1, s, 5);
asm(
"movq $1, %%rax;"
"movq $1, %%rdi;"
"movq %0, %%rsi;"
"movq $5, %%rdx;"
"syscall;"
:
: "r"(s)
: "%rax", "%rdi", "%rsi", "%rdx"
);
}
Compile with gcc:
$ gcc main.c -o test
Run:
$ ./test
By running the code with the Xcode 4.5 debugger I get this error:
Thread 1: EXC_SYSCALL (code=1, subcode=0x1)
What’s wrong with the code? It runs successfully with Ubuntu 12.04 LTS.
Thanks!
As I mentioned in my comment above, the system call number is different. You need to put
0x2000004in%raxto get this program to work on Mac OS X.I’m not really sure how your program works on Ubuntu, either, since you’re using the
exitsyscall number1in your example, rather than4forwrite.