-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment4.asm
More file actions
424 lines (348 loc) · 8.68 KB
/
assignment4.asm
File metadata and controls
424 lines (348 loc) · 8.68 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
; Author: Kristy Nguyen
; Section: 1001
; Date Last Modified: 9/20/2020
; Program Description: This program involves working with macros to perform a variety of tasks
; Macro 1 - Returns the length of a given string in rax
; Argument 1: null terminated string
%macro stringLength 1
; YOUR CODE HERE
mov rax, 0
%%readLoop:
mov bl, byte[%1 + rax]
inc rax
cmp bl, NULL ; if current char is null
je %%endReadLoop ; stop reading the string
jmp %%readLoop ; repeat loop
%%endReadLoop:
%endmacro
; Macro 2 - Convert letters in a string to uppercase
; Argument 1: null terminated string
%macro toUppercase 1
; YOUR CODE HERE
mov rax, 0
%%readLoop:
mov bl, byte[%1 + rax]
cmp bl, NULL ; if current char is null
je %%endReadLoop ; stop reading
cmp bl, "a" ; if char is < a
jb %%notLowercase
cmp bl, "z" ; if char is > z
ja %%notLowercase ; go to next char in string
sub bl, 20h ; otherwise, subtract 20h
mov byte[%1 + rax], bl
%%notLowercase: ; if char is not lowercase
inc rax ; go to next char
jmp %%readLoop ; repeat loop
%%endReadLoop:
%endmacro
; Macro 3 - Convert a Decimal String to Integer
; Argument 1: null terminated string (byte array)
; Argument 2: dword integer variable
%macro convertDecimalToInteger 2
; YOUR CODE HERE
mov rbx, %1 ; start rbx at 0th index of Argument 1
; Step 1: 0 or more spaces (1)
%%skipSpaces1:
mov cl, byte[rbx] ; current char
inc rbx ; next char
cmp cl, " "
je %%skipSpaces1
dec rbx ; reset pointer to current char
mov r8, 1 ; positive sign
; Step 2: A '+' or '-' may appear
cmp cl, "+"
je %%signDone
cmp cl, "-" ; if char is not "+" or "-"
jne %%skipSpaces2 ; go to next step
mov r8, -1 ; negative sign
%%signDone:
inc rbx ; next char
; Step 3: 0 or more spaces (2)
%%skipSpaces2:
mov cl, byte[rbx] ; current char
inc rbx ; next char
cmp cl, " "
je %%skipSpaces2
dec rbx ; reset to current char
; Step 4: 1 or more numerals ('0' to '9')
mov eax, 0 ; value = 0
mov r10d, 10
%%digitLoop:
mul r10d ; value = value * 10
movzx ecx, byte[rbx]
sub ecx, 30h ; ascii to decimal
add eax, ecx ; value = value + digit
inc rbx ; next char
mov cl, byte[rbx]
cmp cl, NULL ; end digitLoop if NULL or " "
je %%digitLoopDone
cmp cl, " "
je %%digitLoopDone
jmp %%digitLoop
%%digitLoopDone:
dec rbx ; reset to current char
cmp r8, -1
je %%negativeValue
%%negativeValue:
mul r8
mov dword[%2], eax ; store answer in provided variable
; Step 5: 0 or more spaces (3)
%%skipSpaces3:
mov cl, byte[rbx]
inc rbx ; next char
cmp cl, " "
je %%skipSpaces3
dec rbx ; reset to current char
; Step 6: A NULL character
mov rax, 0
%%readLoop:
mov bl, byte[%1 + rax]
inc rax
cmp bl, NULL ; if current char is null
je %%endReadLoop ; stop reading the string
jmp %%readLoop ; repeat loop
%%endReadLoop:
%endmacro
; Macro 4 - Convert an Integer to a Hexadecimal String
; Argument 1: dword integer variable
; Argument 2: string (11 byte array)
%macro convertIntegerToHexadecimal 2
; YOUR CODE HERE
mov eax, dword[%1] ; move dword integer variable into eax
mov rsi, 9
mov byte[%2 + 10], NULL ; place null char
mov ebx, 16
%%hexLoop:
cmp eax, 0 ; if value = 0, fill in remainder of hex digits
je %%fillHex
cmp rsi, 2 ; if index < 2, hex digits are fully converted
jl %%hexDone
mov edx, 0
div ebx ; divide value by 16
cmp edx, 9
jg %%convert10to15
; if remainder is between 0 and 9, convert to '0' - '9'
add edx, 48
mov cl, dl ; copy remainder in byte to cl
mov byte[%2 + rsi], cl ; copy cl to byte array
dec rsi ; move to previous byte
jmp %%hexLoop
%%convert10to15: ; else convert to 'A' - 'F'
add edx, 55
mov cl, dl
mov byte[%2 + rsi], cl
dec rsi
jmp %%hexLoop
%%fillHex:
cmp rsi, 2 ; if index < 2, hex digits are fully converted
jl %%hexDone
mov byte[%2 + rsi], '0'
dec rsi
jmp %%fillHex
%%hexDone: ; display "0x" before hex digits
mov byte[%2 + 1], 'x'
mov byte[%2], '0'
%endmacro
section .data
; System Service Call Constants
SYSTEM_WRITE equ 1
SYSTEM_EXIT equ 60
SUCCESS equ 0
STANDARD_OUT equ 1
; Special Characters
LINEFEED equ 10
NULL equ 0
; Macro 1 Variable
macro1Message db "This is the string that never ends, it goes on and on my friends.", LINEFEED, NULL
; Macro 1 Test Variables
macro1Label db "Macro 1: "
macro1Pass db "Pass", LINEFEED
macro1Fail db "Fail", LINEFEED
macro1Expected dq 67
; Macro 2 Variables
macro2Message db "Did you read Chapters 8, 9, and 11 yet?", LINEFEED, NULL
; Macro 2 Test Variable
macro2Label db "Macro 2: "
; Macro 3 Variables
macro3Number1 db "12345", NULL
macro3Number2 db " +19", NULL
macro3Number3 db " - 1468 ", NULL
macro3Integer1 dd 0
macro3Integer2 dd 0
macro3Integer3 dd 0
; Macro 3 Test Variables
macro3Label1 db "Macro 3-1: "
macro3Label2 db "Macro 3-2: "
macro3Label3 db "Macro 3-3: "
macro3Pass db "Pass", LINEFEED
macro3Fail db "Fail", LINEFEED
macro3Expected1 dd 12345
macro3Expected2 dd 19
macro3Expected3 dd -1468
; Macro 4 Variables
macro4Integer1 dd 255
macro4Integer2 dd 1988650
macro4Integer3 dd -7
; Macro 4 Test Variables
macro4Label1 db "Macro 4-1: "
macro4Label2 db "Macro 4-2: "
macro4Label3 db "Macro 4-3: "
macro4NewLine db LINEFEED
section .bss
; Macro 4 Strings
macro4String1 resb 11
macro4String2 resb 11
macro4String3 resb 11
section .text
global _start
_start:
; DO NOT ALTER _start in any way.
mov rax, 0
; Macro 1 - Do not alter
; Invokes the macro using macro1Message as the argument
stringLength macro1Message
; Macro 1 Test - Do not alter
push rax
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro1Label
mov rdx, 9
syscall
mov rdi, STANDARD_OUT
mov rsi, macro1Fail
mov rdx, 5
pop rax
cmp rax, qword[macro1Expected]
jne macro1_Fail
mov rsi, macro1Pass
macro1_Fail:
mov rax, SYSTEM_WRITE
syscall
; Macro 2 - Do not alter
; Invokes the macro using macro2message as the argument
toUppercase macro2Message
; Macro 2 Test - Do not alter
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro2Label
mov rdx, 9
syscall
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro2Message
mov rdx, 41
syscall
; Macro 3 - 1 - Do not alter
; Invokes the macro with macro3Number1 and macro3Integer1
convertDecimalToInteger macro3Number1, macro3Integer1
; Macro 3 - 2 - Do not alter
; Invokes the macro with macro3Number2 and macro3Integer2
convertDecimalToInteger macro3Number2, macro3Integer2
; Macro 3 - 3 - Do not alter
; Invokes the macro with macro3Number3 and macro3Integer3
convertDecimalToInteger macro3Number3, macro3Integer3
; Macro 3 Test - Do not alter
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro3Label1
mov rdx, 11
syscall
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro3Fail
mov rdx, 5
mov ebx, dword[macro3Integer1]
cmp ebx, dword[macro3Expected1]
jne macro3_1_Fail
mov rsi, macro3Pass
macro3_1_Fail:
syscall
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro3Label2
mov rdx, 11
syscall
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro3Fail
mov rdx, 5
mov ebx, dword[macro3Integer2]
cmp ebx, dword[macro3Expected2]
jne macro3_2_Fail
mov rsi, macro3Pass
macro3_2_Fail:
syscall
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro3Label3
mov rdx, 11
syscall
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro3Fail
mov rdx, 5
mov ebx, dword[macro3Integer3]
cmp ebx, dword[macro3Expected3]
jne macro3_3_Fail
mov rsi, macro3Pass
macro3_3_Fail:
syscall
; Macro 4 - 1 - Do not alter
convertIntegerToHexadecimal macro4Integer1, macro4String1
; Macro 4 - 2 - Do not alter
convertIntegerToHexadecimal macro4Integer2, macro4String2
; Macro 4 - 3 - Do not alter
convertIntegerToHexadecimal macro4Integer3, macro4String3
; Macro 4 Test - Do not alter
; Test 1
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro4Label1
mov rdx, 11
syscall
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro4String1
mov rdx, 11
syscall
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro4NewLine
mov rdx, 1
syscall
; Test 2
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro4Label2
mov rdx, 11
syscall
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro4String2
mov rdx, 11
syscall
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro4NewLine
mov rdx, 1
syscall
; Test 3
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro4Label3
mov rdx, 11
syscall
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro4String3
mov rdx, 11
syscall
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, macro4NewLine
mov rdx, 1
syscall
endProgram:
mov rax, SYSTEM_EXIT
mov rdi, SUCCESS
syscall