diff --git a/cat/nem_cat/makefile b/cat/nem_cat/makefile new file mode 100644 index 0000000..f17cc24 --- /dev/null +++ b/cat/nem_cat/makefile @@ -0,0 +1,4 @@ +n_cat : nem_cat.asm + nasm -f elf64 -o n_cat.o nem_cat.asm + ld -o n_cat n_cat.o + rm n_cat.o diff --git a/cat/nem_cat/n_cat b/cat/nem_cat/n_cat new file mode 100755 index 0000000..d73ee95 Binary files /dev/null and b/cat/nem_cat/n_cat differ diff --git a/cat/nem_cat/nem_cat.asm b/cat/nem_cat/nem_cat.asm new file mode 100644 index 0000000..623b95f --- /dev/null +++ b/cat/nem_cat/nem_cat.asm @@ -0,0 +1,91 @@ +section .data + cant db "open error", 10 + length equ $- cant + errnum equ 0xFFFFFFFE + + sys_open equ 2 + sys_write equ 1 + sys_read equ 0 + sys_close equ 3 + sys_lseek equ 8 + + seek_start equ 0 + seek_end equ 2 + +section .bss + msg resb 1 + +section .text + global _start + +_write: + mov rax, sys_write + mov rdi, 1 ;標準出力 + syscall + ret + +_start: + pop rcx ;コマンドライン引数の数 + pop rbx ;./n_cat + push rcx + +argloop: + pop rcx + pop rbx ;コマンドライン引数 + dec rcx + push rcx + cmp rcx, 0 + je end + + ;open + mov rax, sys_open + mov rdi, rbx + xor rsi, rsi + xor rdx, rdx + syscall + + ;これ以降rdiに変更なし + push rax + cmp rax, errnum + je op_error + + ;lseek + mov rax, sys_lseek + pop rdi + xor rsi, rsi + mov rdx, seek_end + syscall + + push rax + + mov rax, sys_lseek + xor rsi, rsi + mov rdx, seek_start + syscall + + ;read + mov rax, sys_read + mov rsi, msg ;書き込み先 + pop rdx ;読み込む文字数 + syscall + + ;write + mov rsi, msg ;読み込み先 + call _write + + close: + mov rax, sys_close + xor rdi, rdi + syscall +jmp argloop + +op_error: + mov rsi, cant + mov rdx, length + call _write + +end: + ;exit + mov rax, 60 + xor rdi, rdi + syscall