-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstring.asm
521 lines (460 loc) · 12 KB
/
string.asm
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
;===================================================================================================
; Written By: Oded Cnaan (oded.8bit@gmail.com)
; Site: http://odedc.net
; Licence: None
; Package: UtilLib
; Date: 17-03-2018
; File: string.asm
;
; Description: Handles null terminating strings. All strings are BYTE ARRAYS with ASCII chars
;===================================================================================================
LOCALS @@
CODESEG
;------------------------------------------------------------------------
; strlen: Calculates length of string ending with NULL
;
; Input:
; push offset of string
; call strlen
;
; Output:
; AX - string length
;
; Limitations:
; 1. Assumes string are on DS
; 2. Assumes NULL terminating strings
;------------------------------------------------------------------------
PROC Strlen
store_sp_bp
push bx
push cx
; now the stack is
; bp+0 => old base pointer
; bp+2 => return address
; bp+4 => param2 (offset)
; saved registers
xor cx,cx ; Counter
mov bx, [WORD PTR bp+04h] ; String
@@StrLengthInnerLoop: ; Inner loop
mov ax,[WORD PTR bx] ; Read 16 bits from the string
test al,al ; See if AL is zero
je @@StrLengthEP ; Jump to the EP if yes
inc cx ; Increment the count register
test ah,ah ; See if AH is zero
je @@StrLengthEP ; Jump to the EP if yes
add bx,02h ; Navigate to the next two bytes of the string
inc cx ; Increment the count register once again
jmp @@StrLengthInnerLoop ; Repeat
@@StrLengthEP:
mov ax,cx ; Move the length of the string to AX
pop cx
pop bx
restore_sp_bp
ret 2
ENDP Strlen
;------------------------------------------------------------------------
; StrlenDollar: Calculates length of string ending with '$'
;
; Input:
; push offset of string
; call StrlenDollar
;
; Output:
; AX - string length
;
; Limitations:
; 1. Assumes string are on DS
;------------------------------------------------------------------------
PROC StrlenDollar
store_sp_bp
push bx
push cx
; now the stack is
; bp+0 => old base pointer
; bp+2 => return address
; bp+4 => param2 (offset)
; saved registers
xor cx,cx ; Counter
mov bx, [WORD PTR bp+04h] ; String
@@StrLengthInnerLoop: ; Inner loop
mov ax,[WORD PTR bx] ; Read 16 bits from the string
cmp al, '$' ; See if AL is $
je @@StrLengthEP ; Jump to the EP if yes
inc cx ; Increment the count register
cmp ah, '$' ; See if AH is zero
je @@StrLengthEP ; Jump to the EP if yes
add bx,02h ; Navigate to the next two bytes of the string
inc cx ; Increment the count register once again
jmp @@StrLengthInnerLoop ; Repeat
@@StrLengthEP:
mov ax,cx ; Move the length of the string to AX
pop cx
pop bx
restore_sp_bp
ret 2
ENDP StrlenDollar
;------------------------------------------------------------------------
; Strcpy: Copies string s2 into string s1.
;
; Input:
; push offset of target string (s1)
; push offset of source string (s2)
; call strcpy
;
; Output:
; AX - TRUE on success, FALSE on failure
;
; Limitations:
; 1. Assumes string are on DS
; 2. Assumes NULL terminating strings
; 3. Assumes S1 is long enough
;------------------------------------------------------------------------
PROC Strcpy
push bp
mov bp,sp
push cx si di es
; now the stack is
; bp+0 => old base pointer
; bp+2 => return address
; bp+4 => offset of source string (s2)
; bp+6 => offset of target string (s1)
; saved registers
;{
parSrc_ equ [word bp+4]
parTrg_ equ [word bp+6]
;}
push parSrc_
call strlen
cmp ax, 0
je @@emptyString
mov cx, ax
push ds
pop es
mov di, parTrg_
mov si, parSrc_
; Move byte at address DS:SI to address ES:DI
rep movsb
mov [BYTE di],NULL
mov ax, TRUE
jmp @@end
@@emptyString:
mov di, parTrg_
mov [BYTE di],NULL
mov ax, TRUE
@@end:
pop es di si cx
mov sp,bp
pop bp
ret 4
ENDP Strcpy
;------------------------------------------------------------------------
; Strncpy: Copies given number of chars from string s2 into string s1.
;
; Input:
; push offset of target string (s1)
; push offset of source string (s2)
; push number of chars
; call strcpy
;
; Output:
; AX - TRUE on success, FALSE on failure
;
; Limitations:
; 1. Assumes string are on DS
; 2. Assumes NULL terminating strings
; 3. Assumes S1 is long enough
;------------------------------------------------------------------------
PROC Strncpy
push bp
mov bp,sp
push cx si di es
; now the stack is
; bp+0 => old base pointer
; bp+2 => return address
; bp+4 => number of chars to copy
; bp+6 => offset of source string (s2)
; bp+8 => offset of target string (s1)
; saved registers
;{
parNumChars_ equ [word bp+4]
parSrc_ equ [word bp+6]
parTrg_ equ [word bp+8]
;}
mov ax, parNumChars_
cmp ax, 0
je @@emptyString
mov cx, ax
push ds
pop es
mov di, parTrg_
mov si, parSrc_
; Move byte at address DS:SI to address ES:DI
rep movsb
mov [BYTE di],NULL
mov ax, TRUE
jmp @@end
@@emptyString:
mov di, parTrg_
mov [BYTE di],NULL
mov ax, TRUE
@@end:
pop es di si cx
mov sp,bp
pop bp
ret 4
ENDP Strncpy
;------------------------------------------------------------------------
; Strcat: concatenate 2 strings
;
; Input:
; push offset of destination string
; push offset of source string
; call Strcat
;
; Output:
; AX - length of destination string
;
; Limitations:
; 1. Assumes string are on DS
; 2. Assumes NULL terminating strings
; 3. Assumes Destination is large enough to contain the concatenated
; resulting string
;------------------------------------------------------------------------
PROC Strcat
push bp
mov bp,sp
sub sp, 4
push cx di si es
; now the stack is
; bp-4 => original dest length
; bp-2 => src length
; bp+0 => old base pointer
; bp+2 => return address
; bp+4 => source
; bp+6 => destination
; saved registers
;{
varDestLen_ equ [word bp-4]
varSrcLen_ equ [word bp-2]
parSrc_ equ [word bp+4]
parDest_ equ [word bp+6]
;}
push parSrc_
call Strlen
mov varSrcLen_, ax
mov cx, ax
push parDest_
call Strlen
mov varDestLen_, ax
push ds
pop es
mov di, parDest_
add di, ax
mov si, parSrc_
; Move byte at address DS:SI to address ES:DI
rep movsb
mov [BYTE di], NULL
add ax, varSrcLen_
@@end:
pop es si di cx
mov sp,bp
pop bp
ret 4
ENDP Strcat
;------------------------------------------------------------------------
; Strchr: Searches for the first occurrence of the character in the
; given string
;
; Input:
; push string address
; push the char
; call Strchr
;
; Output:
; AX - index of char or -1 if not found
;
; Limitations:
; 1. Assumes string are on DS
; 2. Assumes NULL terminating strings
;------------------------------------------------------------------------
PROC Strchr
push bp
mov bp,sp
push bx cx dx si
; now the stack is
; bp+0 => old base pointer
; bp+2 => return address
; bp+4 => char
; bp+6 => string address
; saved registers
;{
parChar_ equ [word bp+4]
parStr_ equ [word bp+6]
;}
xor cx, cx ; counter
mov si, parStr_
mov bx, parChar_
@@check:
mov dl,[BYTE si]
cmp dl, NULL
je @@notfound ; reached end of string - not found
cmp dl, bl ; if string[counter] == char
je @@found
inc cx ; counter++
inc si
jmp @@check
@@notfound:
mov ax,-1
jmp @@end
@@found:
mov ax, cx
@@end:
pop si dx cx bx
mov sp,bp
pop bp
ret 4
ENDP Strchr
;------------------------------------------------------------------------
; Strcmp: Compares the string pointed to, by str1 to the string pointed
; to by str2.
;
; Input:
; push str1 offset
; push str2 offset
; call Strcmp
;
; Output:
; AX - 0 if the same, -1 if different
;
; Limitations:
; 1. Assumes string are on DS
; 2. Assumes NULL terminating strings
;------------------------------------------------------------------------
PROC Strcmp
push bp
mov bp,sp
push cx
; now the stack is
; bp+0 => old base pointer
; bp+2 => return address
; bp+4 => str2 offset
; bp+6 => str1 offset
; saved registers
;{
parstr2_ equ [word bp+4]
parStr1_ equ [word bp+6]
;}
mov si, parStr1_
mov di, parStr2_
@@check:
cmp [BYTE si], NULL
je @@endof1
; if we got to the end of S2 but not of S1 then
; there is a miss match
cmp [BYTE di], NULL
je @@notmatch
; if (s1[index] == s2[index])
mov cl, [BYTE si]
cmp cl, [BYTE di]
jne @@notmatch
; mov to next char
inc si
inc di
jmp @@check
@@endof1:
; check if this is the end of s2 as well
cmp [BYTE di],NULL
jne @@notmatch
; match
mov ax, 0
jmp @@end
@@notmatch:
mov ax, -1
@@end:
pop cx
mov sp,bp
pop bp
ret 4
ENDP Strcmp
;------------------------------------------------------------------------
; Strstr: finds the first occurrence of the substring needle in the string
; haystack
;
; Input:
; push offset of haystack string
; push offset of needle string (what we are looking for)
; call Strstr
;
; Output:
; AX - index of subsctring or -1 if not found
;
; Limitations:
; 1. Assumes string are on DS
; 2. Assumes NULL terminating strings
;------------------------------------------------------------------------
PROC Strstr
push bp
mov bp,sp
push bx cx dx si di
; now the stack is
; bp-2 =>
; bp+0 => old base pointer
; bp+2 => return address
; bp+4 => needle
; bp+6 => haystack
; saved registers
;{
parHaystack_ equ [word bp+6]
parNeedle_ equ [word bp+4]
;}
xor cx, cx ; counter
mov si, parHaystack_
mov bx, parNeedle_
@@check:
mov dl,[BYTE si]
cmp dl, NULL
je @@notfound ; reached end of string - not found
cmp dl,[BYTE bx] ; if haystack[counter] == needle[0]
je @@foundFirst
@@next:
inc cx ; counter++
inc si
jmp @@check
@@foundFirst:
push cx si bx di dx
@@checknext:
; we already know the first char matches
inc si ; index to haystack string. h++
inc bx ; index to needle string n = 1
mov dl,[BYTE si] ; haystack[h]
mov dh,[BYTE bx] ; needle[n]
; if (haystack[h] == NULL && needle[n] == NULL) then this is a match else break
cmp dl, NULL
jne @@cont
cmp dh, NULL
je @@found
; hay is null but not neddle
pop dx di bx si cx
jmp @@next
@@cont:
; if (needle[n] == NULL) then we found a match
cmp dh, NULL
je @@found
; if (haystack[h] == needle[n])
cmp dl,dh
je @@checknext ; the same, check next char
; else (no match) break
pop dx di bx si cx
jmp @@next
@@notfound:
mov ax,-1
jmp @@end
@@found:
pop dx di bx si cx
mov ax, cx
@@end:
pop di si dx cx bx
mov sp,bp
pop bp
ret 4
ENDP Strstr