From ee168f8a91ea67870c6193876f5a4197d1d08d32 Mon Sep 17 00:00:00 2001 From: NeM-T Date: Mon, 21 Oct 2019 14:07:30 +0900 Subject: [PATCH 1/2] cat --- cat/nem_cat/makefile | 4 ++ cat/nem_cat/n_cat | Bin 0 -> 1432 bytes cat/nem_cat/nem_cat.asm | 79 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 cat/nem_cat/makefile create mode 100755 cat/nem_cat/n_cat create mode 100644 cat/nem_cat/nem_cat.asm 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 0000000000000000000000000000000000000000..c515835a82cbc13381f8b558ec3b912ff073335e GIT binary patch literal 1432 zcmbW1zi-n}5XaAU)ABrRpui0-+UiZ&VZ(B#ozmNVjn%p$TR-1DccN+3b+dP z%F_}*e3$3>(btTQd_akl1&`kPLw(2rJ>}sRMrU_4wpxLBJB(^!lEle5%06{?czgS! z`x=LNc)PmAm;!kC(E})_bIibTuDZO}rsNugb}Y`ANo&arjO{_8aV?<`;{^%lLO zG_+6m3m4vts?4h)dL5PWJ@K`l^?fh*-;Bhm+(hFHel=hF-cV5-v^F`)`4r|(28i91 t{2!w9-$l=^^{z_XSybIy5KBYg^GS%dlpE2DX#FeygTULP$igV+{{gpajgJ5T literal 0 HcmV?d00001 diff --git a/cat/nem_cat/nem_cat.asm b/cat/nem_cat/nem_cat.asm new file mode 100644 index 0000000..1681c5c --- /dev/null +++ b/cat/nem_cat/nem_cat.asm @@ -0,0 +1,79 @@ +section .data + cant db "open error", 10 + length equ $- cant + errnum equ 0xFFFFFFFE + +section .bss + msg resb 1 + +section .text + global _start + +_write: + mov rax, 1 + mov rdi, 1 + syscall + ret + +_start: + pop rcx + pop rbx + push rcx + +argloop: + open: + pop rcx + pop rbx + dec rcx + push rcx + cmp rcx, 0 + je end + + ;open + mov rax, 2 + mov rdi, rbx + mov rsi, 0 + mov rdx, 0 + syscall + + cmp rax, errnum + je op_error + + push rax + + wrloop: + ;read + mov rax, 0 + pop rdi + mov rsi, msg + mov rdx, 1 + syscall + + cmp rax, 0 + je close + + push rdi + ;write + mov rsi, msg + mov rdx, 1 + call _write + jmp wrloop + + close: + mov rax, 3 + mov rdi, 0 + mov rsi, 0 + mov rdx, 0 + syscall +jmp argloop + +op_error: + mov rsi, cant + mov rdx, length + call _write + +end: + ;exit + mov rax, 60 + mov rdi, 0 + syscall From 435049facd80f8602384a066239e3bd49ed0b35e Mon Sep 17 00:00:00 2001 From: NeM-T Date: Mon, 28 Oct 2019 17:40:25 +0900 Subject: [PATCH 2/2] success --- cat/nem_cat/n_cat | Bin 1432 -> 1584 bytes cat/nem_cat/nem_cat.asm | 74 +++++++++++++++++++++++----------------- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/cat/nem_cat/n_cat b/cat/nem_cat/n_cat index c515835a82cbc13381f8b558ec3b912ff073335e..d73ee956af0769fb648ca73c702a24bc283c70ac 100755 GIT binary patch literal 1584 zcmbVMPixdr5T9(C`VZM!5EL&f2qMVh;-v?XJ=Bte$`(9WSIDNjk6kf;mgGshClAGg z(1V}A&(PyiunL~M`USieJmn&KX`Gq7>BdAnIFL8<`_0VDWHRr)e(-RkVOb#3fcrq` z19X`xR)FaeL&L2>1N~=UnPZ)W3$3m}g??Z(SozGkS&8@zhf7x{Reu3VdH;{JUeseoN+|Jjg({uNJp6EyOvq*bL9+DqvCI}-$ zJSXKIJvS9RYdQ;Xyr1X!+@^p|-gLgdbC0CLLn`q&Apbq_1H@AmwN#SC$$2FIEjD)V z0J^mx8@nrSDLZYjL8=s?dc zAudzH->L8e!>?EPC&;hh|CMvZVYtkgx1nzhgkt@M`9_WRn73h2iHAsq-q6==KMi5% zM;ZbZjr9a@_M%rIq_fncS!81;d7%|siSkEm41+i=s32A9CD7uf+D~+0s%QjZItD)( z2XQ>bx%POf!SlA$)I@q}pp(kQ#wX31cVQozOrRpui0-+UiZ&VZ(B#ozmNVjn%p$TR-1DccN+3b+dP z%F_}*e3$3>(btTQd_akl1&`kPLw(2rJ>}sRMrU_4wpxLBJB(^!lEle5%06{?czgS! z`x=LNc)PmAm;!kC(E})_bIibTuDZO}rsNugb}Y`ANo&arjO{_8aV?<`;{^%lLO zG_+6m3m4vts?4h)dL5PWJ@K`l^?fh*-;Bhm+(hFHel=hF-cV5-v^F`)`4r|(28i91 t{2!w9-$l=^^{z_XSybIy5KBYg^GS%dlpE2DX#FeygTULP$igV+{{gpajgJ5T diff --git a/cat/nem_cat/nem_cat.asm b/cat/nem_cat/nem_cat.asm index 1681c5c..623b95f 100644 --- a/cat/nem_cat/nem_cat.asm +++ b/cat/nem_cat/nem_cat.asm @@ -3,6 +3,15 @@ section .data 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 @@ -10,60 +19,63 @@ section .text global _start _write: - mov rax, 1 - mov rdi, 1 + mov rax, sys_write + mov rdi, 1 ;標準出力 syscall ret _start: - pop rcx - pop rbx + pop rcx ;コマンドライン引数の数 + pop rbx ;./n_cat push rcx argloop: - open: pop rcx - pop rbx + pop rbx ;コマンドライン引数 dec rcx push rcx cmp rcx, 0 je end ;open - mov rax, 2 + mov rax, sys_open mov rdi, rbx - mov rsi, 0 - mov rdx, 0 + xor rsi, rsi + xor rdx, rdx syscall + ;これ以降rdiに変更なし + push rax cmp rax, errnum je op_error - push rax + ;lseek + mov rax, sys_lseek + pop rdi + xor rsi, rsi + mov rdx, seek_end + syscall - wrloop: - ;read - mov rax, 0 - pop rdi - mov rsi, msg - mov rdx, 1 - syscall - - cmp rax, 0 - je close + push rax - push rdi - ;write - mov rsi, msg - mov rdx, 1 - call _write - jmp wrloop + 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, 3 - mov rdi, 0 - mov rsi, 0 - mov rdx, 0 + mov rax, sys_close + xor rdi, rdi syscall jmp argloop @@ -75,5 +87,5 @@ op_error: end: ;exit mov rax, 60 - mov rdi, 0 + xor rdi, rdi syscall