-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtree.asm
More file actions
102 lines (80 loc) · 1.58 KB
/
Copy pathtree.asm
File metadata and controls
102 lines (80 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
;; Given a directory name, display its contents as a tree
extern tree
extern print_strs
extern display_contents
global _start
global puts
global malloc
global open
global close
global dirents
section .data
argc_error: db `Usage: tree <dir>\n`, 18
argc_error_len: equ 18
section .text
_start:
pop r8 ; argc
cmp r8, 2 ; argc > 2
jne usage ; show usage and exit
mov rdi, [rsp+8]
call tree
jmp exit
; rdi -> char*path
open:
mov rax, 2 ; open
mov rsi, 0 ; flags
mov rdx, 0 ; read only
syscall
ret
; rdi -> fd
close:
mov rax, 3 ; close
syscall
ret
; rdi -> fd
; rsi -> struct linux_dirent*buff
; rdx -> buff length
dirents:
mov rdi, rax ; result fd
mov rax, 78
syscall
ret
; rdi -> char*buff
; rsi -> length
puts:
push rax ; save registers
push rdi
mov rax, 1 ; write
mov rdx, rsi ; length
mov rsi, rdi ; char buffer
mov rdi, 1 ; stdin
syscall
pop rdi ; restore registers
pop rax
ret
; rdi -> length
malloc:
push rdi ; save the length
xor rbx, rbx ; determine end of the bss segment
mov rdi, rbx ; push the addr in rdi (arg 0)
mov rax, 12 ; brk
syscall
pop rdi ; restore the length
push rdi
add rax, rdi ; now add the length to the end of the bss segment
mov rbx, rax ; update rbx
mov rdi, rax ; push new offset in rdi
mov rax, 12 ; brk
syscall
pop rdi
sub rax, rdi ; rewind the index by the allocation amount
ret
usage:
mov rdi, argc_error
mov rsi, argc_error_len
call puts
call exit
exit:
mov rax, 60
mov rdi, 0
syscall