From c400fb5593765a74d7eabc8142a9d7957fd7c51c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Thu, 11 Dec 2025 02:56:15 +0900 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E5=88=9D=E3=82=B3=E3=83=9F=E3=83=83?= =?UTF-8?q?=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +++ Makefile | 10 +++++++ main.s | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.s diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3cbf949 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.ppm +*.o +gen diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d3034e3 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +all: build run + +build: + nasm -f elf64 main.s -o main.o + ld main.o -o gen + +run: + ./gen + +.PHONY: all build run diff --git a/main.s b/main.s new file mode 100644 index 0000000..d393451 --- /dev/null +++ b/main.s @@ -0,0 +1,76 @@ +; FreeBSDで、死すコール番号を調べるには:/usr/include/sys/syscall.h +; Linuxで、死すコール番号を調べるには:/usr/include/asm-generic/unistd.h +; 下記のコードはFreeBSD向けの物です。 + +bits 64 +default rel + +section .data + hdr2 db "960 540", 10 + hdr3 db "255", 10 + +section .rodata + outfile db "output.ppm", 0 + hdr1 db "P6", 10 + err db "開くに失敗", 10 + errlen equ $-err + + w equ 960 + h equ 540 + +section .text + global _start + +_start: + mov rax, 5 ; sys_openat + lea rdi, [outfile] + mov rsi, 0x100601 ; 0x100601 = 0001 0000 0000 0110 0000 0001 = 1048576 + 1024 + 512 + 1 = 1050113 + ; O_WRONLY = 0x0001 + ; O_CREAT = 0x0200 + ; O_TRUNC = 0x0400 + ; O_CLOEXEC = 0x010000 + ; /usr/include/sys/fcntl.h + mov rdx, 0o666 ; chmod 666 + syscall + + test rax, rax + js error ; エラーがあれば + mov rbx, rax ; ファイルディスクリプターをrbxに保存 + + mov rax, 4 ; sys_write + mov rdi, rbx ; fd + lea rsi, [hdr1] + mov rdx, 3 + syscall + + mov rax, 4 + mov rdi, rbx + lea rsi, [hdr2] + mov rdx, 8 + syscall + + mov rax, 4 + mov rdi, rbx + lea rsi, [hdr3] + mov rdx, 4 + syscall + + mov rax, 6 ; sys_close + mov rdi, rbx + syscall + +end: + mov rax, 1 ; sys_exit + xor rdi, rdi + syscall + +error: + mov rax, 4 + mov rdi, 2 ; stderr + lea rsi, [err] + mov rdx, errlen + syscall + + mov rax, 1 + mov rdi, 1 + syscall