練習:低レベルなHelloWorld
Hello Worldを生で書く
linux 64bitを想定
初級
#include "stdio.h"
int main(void){
printf("Hello, world!\n");
return 0;
}
中級
#include "unistd.h"
int main(void){
write(1,"Hello, world!\n",14);
return 0;
}
上級
int main(void)
{
const char mes[] = "hello, world\n";
asm("movq %0, %%rdx" :: "r"(sizeof(mes)) );
asm("movq %0, %%rsi" :: "r"(mes) );
asm("movq $1, %%rdi" ::);
asm("movl $1, %%eax" ::);
asm("syscall");
}
超上級
gcc -O3 -static -fomit-frame-pointer -nostartfiles -fno-stack-protector hello.c
void _start(void){
const char mes[] = "hello, world\n";
asm("movq %0, %%rdx" :: "r"(sizeof(mes)) );
asm("movq %0, %%rsi" :: "r"(mes) );
asm("movq $1, %%rdi" ::); /* stdout */
asm("movl $1, %%eax" ::); /* write */
asm("syscall");
asm("movq $0, %%rdi" ::);
asm("movl $0x3c, %%eax" ::); /* _exit */
asm("syscall");
}