/
file.s
92 lines (87 loc) · 1.41 KB
/
file.s
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
.section .data
.equ SYS_read, 0
.equ SYS_write, 1
.equ SYS_open, 2
.equ SYS_close, 3
.equ SYS_exit, 60
.equ SYS_creat, 85
.equ STDIN, 0
.equ STDOUT, 1
.equ STDERR, 2
.equ O_CREAT, 0x40
.equ O_TRUNC, 0x200
.equ O_APPEND, 0x400
.equ O_RDONLY, 000000
.equ O_WRONLY, 000001
.equ O_RDWR, 000002
.equ S_IRUSR, 0x100
.equ S_IWUSR, 0x80
.equ S_IXUSR, 0x40
fileDescriptor:
.quad 0
outputSize:
.quad 0
lineCount:
.quad 0
.section .bss
.lcomm buffer, 100000
.section .text
.globl _start
_start:
# readFile:
movq %rsp, %r8
movq $SYS_open, %rax
movq 16(%rsp), %rdi
movq $0644, %rsi
syscall
movq %rax, fileDescriptor
movq $SYS_read, %rax
movq fileDescriptor, %rdi
movq $buffer, %rsi
movq $100000, %rdx
syscall
# work:
movq $0, %rbx
jmp check
loop:
movq $0, %rax
movb buffer(%rbx), %al
cmpq $0xA, %rax
jne add
incq lineCount
add:
incq %rbx
check:
movq $0, %rax
movb buffer(%rbx), %al
testq %rax, %rax
jne loop
# print:
movq $1, outputSize
movq lineCount, %rax
pushq $0xA
loopPrint:
movq $10, %rcx
movq $0, %rdx
idivq %rcx
incq outputSize
addq $48, %rdx
pushq %rdx
testq %rax, %rax
jne loopPrint
writeFile:
movq $SYS_creat, %rax
movq 24(%r8), %rdi
movq $S_IRUSR|S_IWUSR, %rsi
syscall
movq %rax, fileDescriptor
movq $SYS_write, %rax
movq fileDescriptor, %rdi
movq %rsp, %rsi
movq outputSize, %rdx
imulq $8, %rdx
syscall
# exit:
movq $SYS_exit, %rax
movq $0, %rdi
syscall