I have a question that has been asked before but comes with an extra caveat. How do I properly execute a GCC inline assembly call given an x86_64 CPU, ubuntu machine, no starter files, and no standard libs? My code compiles with no warnings but does nothing on execution.
I know how to check for syscall numbers on my system, so I am certain I am calling the right number. I am trying to simply write "Hello World" to stdout using this method.
Source:
#define __NR_exit 60
#define __NR_write 1
int sys_exit(int status) {
signed int ret;
asm volatile
(
"int $0x80"
: "=a" (ret)
: "0"(__NR_exit), "b"(status)
: "memory"
);
return ret;
}
int sys_write(int fd, const void *buf, unsigned int count) {
signed int ret;
asm volatile
(
"int $0x80\n\t"
: "=a" (ret)
: "0"(__NR_write), "b"(fd), "c"(buf), "d"(count)
: "memory"
);
return ret;
}
void _start(void){
sys_write(1, "Hello World\0", 11);
sys_exit(0);
}
Comilation:
gcc -nostartfiles -nodefaultlibs -nostdlib hello.c -o hello