0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

64ビットLinuxシステムコールの呼び出し

Posted at

64ビットLinuxシステムコールの呼び出し時の決まり

引数はrdi → rsi → rdx → r10 → r8 → r9 の順に設定する
raxにはシステムコール番号を入れる。
返り値はraxに入る。

write引数の設定

ssize_t write(int fd, const void *buf, size_t count);
mov rax, 1      ; システムコール番号1 = write
mov rdi, 1      ; 第1引数 fd = 標準出力(1)
mov rsi, msg    ; 第2引数 buf = 文字列アドレス
mov rdx, 10     ; 第3引数 count = 書き込むバイト数
syscall

exit引数の設定

void exit(int status);
mov rax, 60     ; 番号60 = exit
mov rdi, 0      ; 第1引数 status = 終了コード
syscall

アセンブリ言語のHelloWorld

コード

; ファイル名: hello.asm
section .data
    msg db 'Hello, World!', 0x0A   ; 文字列 + 改行コード(LF)
    len equ $ - msg                ; 文字列の長さを計算

section .text
    global _start

_start:
    ; システムコール: sys_write(番号1)
    mov rax, 1          ; システムコール番号: 1(write)
    mov rdi, 1          ; ファイル記述子: 1(標準出力)
    mov rsi, msg        ; 文字列のアドレス
    mov rdx, len        ; 文字列の長さ
    syscall             ; システムコールを呼び出す

    ; システムコール: sys_exit(番号60)
    mov rax, 60         ; システムコール番号: 60(exit)
    xor rdi, rdi        ; 終了コード: 0
    syscall             ; システムコールを呼び出す

動作確認

nasm -f elf64 hello.asm -o hello.o
ld hello.o -o hello
./hello
ubuntu@ubuntu:~/kaihatsu/linuxasm$ ./hello
Hello, World!
ubuntu@ubuntu:~/kaihatsu/linuxasm$
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?