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?

中古ノート、買ってみた。 その42

Last updated at Posted at 2025-10-23

概要

中古ノート買ってみた。
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!

以上。

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?