Skip to content

Commit a2bde70

Browse files
committed
Lab 13 solutions (minus bonus labs)
1 parent fbb7c20 commit a2bde70

File tree

1 file changed

+244
-0
lines changed

1 file changed

+244
-0
lines changed

labs/Solutions/Lab13.nasm

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
bits 64
2+
3+
; LABS
4+
global ex_strlen, ex_memcpy, ex_memset, ex_memcmp, ex_memchr, ex_strchr, ex_strcmp, ex_strcpy, ex_atoi,
5+
6+
7+
; BONUS LABS
8+
global ex_strstr, ex_isort, ex_qsort
9+
10+
ex_strlen:
11+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12+
; BEGIN student code
13+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14+
push rbp
15+
mov rbp, rsp
16+
mov rcx, -1
17+
xor rax, rax
18+
repne scasb
19+
not rcx
20+
dec rcx
21+
mov rax, rcx
22+
pop rbp
23+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24+
; END student code
25+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26+
ret
27+
28+
ex_memcpy:
29+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30+
; BEGIN student code
31+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32+
mov rcx, rdx
33+
rep movsb
34+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35+
; END student code
36+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37+
ret
38+
39+
40+
ex_memset:
41+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42+
; BEGIN student code
43+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44+
mov rax, rsi
45+
mov rcx, rdx
46+
rep stosb
47+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48+
; END student code
49+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50+
ret
51+
52+
ex_memchr:
53+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
54+
; BEGIN student code
55+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
56+
mov rax, rsi
57+
mov rcx, rdx
58+
;int3
59+
repne scasb
60+
dec rdi
61+
mov dl, [rdi]
62+
cmp al, dl
63+
jnz .not_found
64+
mov rax, rdi
65+
jmp .done
66+
.not_found:
67+
xor rax, rax
68+
.done:
69+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
70+
; END student code
71+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
72+
ret
73+
74+
ex_memcmp:
75+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
76+
; BEGIN student code
77+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
78+
mov rcx, rdx
79+
repe cmpsb
80+
mov rax, rcx
81+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82+
; END student code
83+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84+
ret
85+
86+
ex_strchr:
87+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
88+
; BEGIN student code
89+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
90+
push rsi
91+
push rdi
92+
call ex_strlen ;rcx is now len
93+
pop rdi
94+
pop rsi
95+
mov rax, rsi
96+
repne scasb
97+
xor rdx, rdx
98+
dec rdi
99+
mov dl, byte [rdi]
100+
cmp dl, al
101+
jz .found
102+
xor rax, rax
103+
jmp .done
104+
.found:
105+
mov rax, rdi
106+
.done:
107+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
108+
; END student code
109+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
110+
ret
111+
112+
ex_strcmp:
113+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
114+
; BEGIN student code
115+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
116+
push rdi
117+
push rsi
118+
call ex_strlen
119+
pop rdi
120+
push rcx
121+
push rdi ; old rsi
122+
call ex_strlen
123+
pop rsi
124+
pop rdx ; rdi -> strlen
125+
pop rdi
126+
cmp rdx, rcx
127+
jne .no_match
128+
repe cmpsb
129+
test rcx, rcx
130+
jz .equal
131+
.no_match:
132+
mov rax, 1
133+
jmp .done
134+
.equal:
135+
xor rax, rax
136+
.done:
137+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138+
; END student code
139+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
140+
ret
141+
142+
ex_strcpy:
143+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144+
; BEGIN student code
145+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
146+
push rsi
147+
push rdi
148+
mov rdi, rsi
149+
call ex_strlen
150+
pop rdi
151+
pop rsi
152+
rep movsb
153+
mov byte [rdi], 0x00
154+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155+
; END student code
156+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157+
ret
158+
159+
160+
ex_atoi:
161+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162+
; BEGIN student code
163+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164+
push rbx
165+
push rdi
166+
call ex_strlen
167+
pop rdi
168+
mov rax, 1 ; step
169+
xor rbx, rbx ;
170+
xor r8, r8 ; accumulator
171+
mov r9, 10 ; static: 10
172+
.continue:
173+
mov bl, [rdi + rcx - 1]
174+
sub bl, 0x30
175+
xchg rbx, rax ; rax now contains the unpacked number
176+
test rax, rax
177+
jz .zero ; skip 0's
178+
mul rbx ; if not, multiply by the step
179+
.zero:
180+
xchg rbx, rax ; rax again contains our step, rdx the number
181+
add r8, rbx ; add the number to accum
182+
mul r9 ; increase the step
183+
xor rbx, rbx ; clear rdx
184+
loop .continue
185+
mov rax, r8
186+
pop rbx
187+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
188+
; END student code
189+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190+
ret
191+
192+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193+
; |-- | |--- | |\ | | | |-------
194+
; | | | | | \ | | | |
195+
; |---- | | | | \ | | | |_______
196+
; | | | | | \ | | | |
197+
; | | | | | \ | | | |
198+
; |____ | |___ | | \| |___| ________|
199+
;
200+
;
201+
; | |------ | |------ | |-------
202+
; | | | | | |_______
203+
; | |------ | |-------- | |
204+
; | | | | | |
205+
; |_______ | | |_________| ________|
206+
;
207+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208+
209+
ex_strstr:
210+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
211+
; -BONUS-
212+
; BEGIN student code
213+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
214+
215+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
216+
; END student code
217+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218+
ret
219+
220+
221+
ex_isort:
222+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223+
; insertion_sort.c is provided
224+
; to give an example implementation.
225+
;
226+
; BEGIN student code
227+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
228+
229+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
230+
; END student code
231+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
232+
ret
233+
234+
235+
ex_qsort:
236+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
237+
; BEGIN student code
238+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
239+
240+
241+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
242+
; END student code
243+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
244+
ret

0 commit comments

Comments
 (0)