概要
中古ノート買ってみた。
wsl1のubuntu18.04にnasm入れてみた。
アセンブラからcのmain呼んでみた。
手順
cを書く
void print(char *str, int length);
void main(void) {
char *str = "Hello, World!\n";
print(str, 14);
}
asmを書く。
global _start, print
extern main
exit:
mov eax, 60
syscall
print:
mov rdx, rsi
mov esi, edi
mov eax, 1
mov edi, 1
syscall
ret
_start:
call main
call exit
cをコンパイル
/usr/lib/gcc/x86_64-linux-gnu/7/cc1 a.c
main
Analyzing compilation unit
Performing interprocedural optimizations
<*free_lang_data> <visibility> <build_ssa_passes> <opt_local_passes> <targetclone> <free-inline-summary> <whole-program> <inline>Assembling functions:
<materialize-all-clones> <simdclone> main
Execution times (seconds)
phase setup : 0.01 (33%) usr 0.00 ( 0%) sys 0.01 (50%) wall 1179 kB (85%) ggc
phase opt and generate : 0.02 (67%) usr 0.00 ( 0%) sys 0.01 (50%) wall 56 kB ( 4%) ggc
callgraph optimization : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 (50%) wall 0 kB ( 0%) ggc
integrated RA : 0.02 (67%) usr 0.00 ( 0%) sys 0.00 ( 0%) wall 24 kB ( 2%) ggc
TOTAL : 0.03 0.00 0.02 1386 kB
# as a.s
asmをコンパイル
# nasm -f elf64 b.asm
リンクする。
# ld -o a a.out b.o
実行する。
# ./a
Hello, World!
以上。