ログ

見る価値ありません

アセンブリ言語で書いたサブルーチンをC言語から呼び出す

メモ

str.h

int strlen(const char*);

str.asm

global strlen

section .text

; int strlen(const char*)
strlen:
    xor rax, rax
_L1:
    cmp [rdi], byte 0
    jz _L2
    inc rax
    inc rdi
    jmp _L1
_L2:
    ret

test.c

#include <stdio.h>
#include <str.h>

int main(void) {
    printf("%d\n", strlen("Hello, World!\n"));
    return 0;
}

ビルド

$ nasm -f elf64 str.h -o str.o
$ gcc -c -I . test.c -o test.o
$ gcc str.o test.o -o test
$ ./test