-
Notifications
You must be signed in to change notification settings - Fork 0
/
end.txt
177 lines (167 loc) · 5.75 KB
/
end.txt
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
atoi:
mov rdx, r8
add rdx, 6 ; set the maximum adress for a char
mov r10, 0 ; initialize int value
cmp byte[r8],45
je atoi.neg_beg
jmp atoi.if
.if:
cmp r8,rdx ; compare the address of cur char with end address
jne atoi.if2
mov rax, input
cmp byte[rax],45
je atoi.neg_end
sub rcx,8
mov rdx, stack
mov rax, [rdx+rcx]
jmp rax
.if2:
movzx r9,byte[r8] ; "read" current char into r9
cmp r9,48
jge .if3
mov rax, input
cmp byte[rax],45
je .neg_end
sub rcx,8
mov rdx, stack
mov rax, [rdx+rcx]
jmp rax
.if3:
movzx r9,byte[r8] ; "read" current char into r9
cmp r9,57
jle .if4
mov rax, input
cmp byte[rax],45
sub rcx,8
mov rdx, stack
mov rax, [rdx+rcx]
jmp rax
.if4:
sub r9,48
mov rax,10
mul r10
mov r10,rax
add r10,r9
mov rdx, input
add rdx, 6 ; set the maximum adress for a char
inc r8
jmp .if
.neg_beg:
inc r8
jmp atoi.if
.neg_end:
neg r10
sub rcx,8
mov rdx, stack
mov rax, [rdx+rcx]
jmp rax
itoa: ;int needs to be stored in r10
mov r8, output ;
add r8, 5 ; set the r8 pointer to the end
mov r9, 10 ; load ten for division
cmp r10,0 ; if value is negative, jump
jl itoa.neg
jmp itoa.loop
.loop:
mov rdx, 0 ; set rdx to 0 as division concatenates rdx and rax
mov rax,r10 ; move value into rax to divide it later
div r9 ; divide the value in rax by ten
add rdx, 48 ; make a character from the int-remainder in rdx
mov [r8],dl ; write this character to the address given by the pointer
dec r8 ; decrease pointer, so next char will be written earlier
cmp r10, r9 ; if value <10 jump out of loop
jae itoa.loop_end ;
sub rcx,8
mov rdx, stack
mov rax, [rdx+rcx]
jmp rax
.loop_end:
mov r10, rax ; move the quotient into r10
jmp itoa.loop ; jump to begin of loop
.neg:
mov rax,45
mov rdx,output
mov [rdx],rax ; set first char to "-"
neg r10 ; create abs value
jmp itoa.loop
store: ; addr in r8, val in r9
cmp rdx,r11
je .if_new
mov rax,addrs
mov rsi,r8
cmp dword [rax+rdx], esi
je .write
add rdx,4
jmp store
.write:
mov rax,addrs
mov rsi,r8
mov [rax+rdx], esi
mov rsi,r9
mov rax,vals
mov [rax+rdx], esi
sub rcx,8
mov rdx, stack
mov rax, [rdx+rcx]
jmp rax
.if_new:
mov rax,addrs
mov rsi,r8
mov [rax+r11], esi
mov rsi,r9
mov rax,vals
mov [rax+r11], esi
add r11,4
sub rcx,8
mov rdx, stack
mov rax, [rdx+rcx]
jmp rax
read: ;init rdx before
mov rax,addrs
mov rsi,r8
cmp dword [rax+rdx], esi
je .read_val
add rdx,4
cmp rdx,1000
jl read
sub rcx,8
mov rdx, stack
mov rax, [rdx+rcx]
jmp rax
.read_val:
mov rax,vals
movsx r10, dword [rax+rdx]
sub rcx,8
mov rdx, stack
mov rax, [rdx+rcx]
jmp rax
input_:
mov r8, input ; pointer to the next char to read
movzx r9,byte [r8+rdi]
cmp r9,0
je .init
inc rdi
sub rcx,8
mov rdx, stack
mov rax, [rdx+rcx]
jmp rax
.init:
mov rax, 0 ; initialize input to zero
mov rdx, input
mov [rdx], rax
mov rax, 0x02000003 ; system call for read
mov rdi, 0 ; file handle 0 is stdin
mov rsi, input ; address to write input
mov rdx, 16 ; number of bytes
push qword r11
push rcx
syscall
pop rcx
pop qword r11
jmp input_
section .bss
input: resb 16
output: resb 6
addrs: resb 1000 ; contains virtual heap addresses from .ws
vals: resb 1000 ; contains corresponding (to addresses from addrs) values
stack: resb 1280