Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catコマンドのコード書きました。 #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cat/nem_cat/makefile
Original file line number Diff line number Diff line change
@@ -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
Binary file added cat/nem_cat/n_cat
Binary file not shown.
91 changes: 91 additions & 0 deletions cat/nem_cat/nem_cat.asm
Original file line number Diff line number Diff line change
@@ -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