上一次的程式裡面使用了簡單的系統呼叫,不過很明顯的,什麼事情都不會做的程式應該沒有任何的魅力吧?那麼,我們何不讓這個程式和我們說幾句話?
就system call的角度來看,要讓程式和外界有所溝通,自然會想到的是read, write等system call。(可以去翻翻asm/unistd_64.h喔~)
.section .text
.global _start
.type _start, @function
_start:
/*
syscall : write
依據asm/unistd_64.h,它的system call number為1
依據man page (在terminal中輸入man 2 write ),
它有三個參數:
參數一(rdi):file descriptor. 1: stdout
參數二(rsi):裝有想要輸出資料的一塊記憶體空間
的指標
參數三(rdx):上面所說的,那個記憶體空間中的資
料大小
*/
mov $1, %rax
mov $1, %rdi
mov $_start_hello_str, %rsi
mov _start_hello_str_size, %rdx
syscall
/*
syscall : exit
syscall number為60
*/
mov $60, %rax
mov $0, %rdi
syscall
.section .rodata /*read-only data*/
_start_hello_str:
.string "Hello!\n"
_start_hello_str_size:
/*
_start_hello_str_size這個標籤所在的位置減去
_start_hello_str,恰好就是Hello字串的長度。
*/
.quad _start_hello_str_size - _start_hello_str
想要編譯它的話,請使用下面的指令:
gcc -nostartfiles hello.s我想該寫的都寫在註解上了吧!那麼,請慢用囉~
沒有留言:
張貼留言