LoginSignup
1
1

More than 3 years have passed since last update.

アセンブリ(NASM)で関数作ってC言語で呼ぶ

Last updated at Posted at 2018-11-03

環境

  • OS : Ubuntu 18.04
  • アセンブラ : NASM 2.13.02
  • リンカ : ld 2.30

ソースコード

function.asm
global _nasm_hello
global _nasm_exit

[section .data]
string1: db "Hello", 0x0a
length1: equ $ - string1

[section .text]

_nasm_hello:
    mov rax, 1
    mov rdi, 1
    mov rsi, string1
    mov rdx, length1
    syscall
    ret

_nasm_exit:
    mov rax, 60
    mov rdi, 0
    syscall
main.c
void _nasm_hello(void);
void _nasm_exit(void);

void main(void){

    _nasm_hello();
    _nasm_exit();

}

コマンド

nasm -felf64 function.asm -o function.obj
gcc -c -o main.obj main.c
ld -e main -o exe main.obj function.obj
./exe

最後に

今回は色々と間違っていることがると思います。
というのも、試行錯誤しているときにSegmentation fault (コアダンプ)と出てきてしまったりしたからです。
最終的に出てこなくなりましたが、ご指摘などありましたらコメントお願いします。
そしてセンター試験まで残り75日くらいなのでやばいです。発狂してます。

1
1
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
1
1