This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask2.asm
640 lines (484 loc) · 11.7 KB
/
task2.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
; Albert Gierlach
; Text zoom + scrolling
data1 segment
text db 40h dup(0) ;63 bytes + 1 for string termination with 0
textLen db 0
zoomvalue db 1
oldVideoMode db ?
drawOffset db 0 ;index of the character from which we start to draw text
str_emptyArguments db "Uzycie: prog.exe zoomvalue ""tekst""",10,13,"$"
str_argumentsError db "Podane argumenty sa niepoprawne!",10,13,"$"
str_exitError db "Program zakonczyl sie bledem :(",10,13,"$"
str_success db "Plik zaszyfrowany pomyslnie!",10,13,"$"
str_zoomWrongValue db "Zoom musi byc z przedzialu [1,9]",10,13,"$"
data1 ends
code1 segment
start1:
;init stack
mov sp, offset topstack
mov ax, seg topstack
mov ss, ax
;init data segment
mov ax, seg data1
mov ds, ax
;store current video mode
;VIDEO - GET CURRENT VIDEO MODE
mov ah, 0fh
int 10h
mov ds:[oldVideoMode], al
call readArgs
;VIDEO - SET VIDEO MODE
mov ah, 0h
mov al, 13h ;13h = G 40x25 8x8 320x200 256/256K . A000 VGA,MCGA,ATI VIP
int 10h
call displayText
loop_zoom:
;KEYBOARD - GET KEYSTROKE
;AH = BIOS scan code
mov ah, 0
int 16h
cmp ah, 04Bh ;left arrow
je right ;scroll right
cmp ah, 04Dh ;right arrow
je left ;scroll left
jmp exitScrollLoop
left:
mov si, offset drawOffset
mov al, ds:[si]
cmp al, 0
jle ignoreLeft
sub al, 1
; sub al, 2 ;speed of scrolling, 2 means two characters for one press
mov ds:[si], al
call clearScreen
call displayText
ignoreLeft:
jmp loop_zoom
right:
mov si, offset drawOffset
mov al, ds:[si]
mov di, offset textLen
mov ah, ds:[di]
sub ah, 1 ;subtracts one so that the text does not disappear completely, so there will always be one visible sign
cmp al, ah
jge ignoreRight
add al, 1
; add al, 2
mov ds:[si], al
call clearScreen
call displayText
ignoreRight:
jmp loop_zoom
exitScrollLoop:
;wait for character
;DOS 1+ - READ CHARACTER FROM STANDARD INPUT, WITH ECHO
; mov ah, 01h
; int 21h
;restore old video mode
;VIDEO - SET VIDEO MODE
mov ah, 0h
mov al, ds:[oldVideoMode]
int 10h
jmp programExit
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;reads parameters, verify them
readArgs proc
push ax
push dx
push si
push di
xor ax, ax
mov al, byte ptr es:[80h] ;number of characters of cmd line - offset 80h
cmp al, 0 ;check if any arguments exists
jne parseArguments
mov dx, offset str_emptyArguments
call putStr
call programExitError
parseArguments:
mov si, 81h ;cmd line starts at 81h
call parseZoom ;read zoom value
mov di, offset text ;point at the start of text bufer
call parseLastArg
mov al, 0 ;terminate string with 0 byte
mov ds:[di], al
;get length of the file, just subtract index registers
mov si, offset text
sub di, si
mov ax, di
xor ah, ah
mov si, offset textLen
mov ds:[si], al
pop di
pop si
pop dx
pop ax
ret
readArgs endp
;parses first argument and converts it to digit
parseZoom proc
push ax
call skipSpaces
mov al, es:[si] ;current char
cmp al, 0dh ;if data ends here then error
je argumentError
;check if value is digit
cmp al, '1'
jl zoomWrongValue
cmp al, '9'
jg zoomWrongValue
sub al, '0' ;convert to number
mov ds:[zoomvalue], al
inc si
mov al, es:[si] ;get next character
cmp al, ' ' ;if the next character after zoom is not a space, it means something longer than 1 character
jne argumentError
call skipSpaces
cmp al, 0dh ;after skipping spaces, only the 0dh character left, which means that there is no second argument
je argumentError
pop ax
ret
parseZoom endp
;parses last arg, similar to parseOneArg but accepts spaces
parseLastArg proc
push ax
push cx
xor ch, ch
loop_copy:
mov al, es:[si] ;get next character from cmd line
cmp al, 0dh ;no more data
je exitLoop
cmp al, '"' ;skip quote
je skipQuote
cmp ch, 40h-1 ;overflow protection :)
jge exitLoop ;-1 because of string termination with 0
mov ds:[di], al
inc ch
inc di
skipQuote: ;if we skip a character we dont increase di
inc si
jmp loop_copy
exitLoop:
pop cx
pop ax
ret
parseLastArg endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
clearScreen proc
push ax
push dx
push bx
push cx
;VIDEO - SCROLL UP WINDOW
;AL = number of lines by which to scroll up (00h = clear entire window)
mov ah, 06h
mov al, 0h
mov bh, 0h ;color, black
mov cx, 0h ;initial coordinates, upper left corner
mov dh, 24 ;row number
mov dl, 79 ;column number, because it draws like a rectangle, so you have to give two points
int 10h
pop cx
pop bx
pop dx
pop ax
clearScreen endp
;draw text char by char
displayText proc
push ax
push bx
push cx
push di
push si
;use mul (TODO)
mov di, offset zoomvalue
mov ch, ds:[di]
xor ax, ax
loop_centering:
cmp ch, 0
je loop_centering_exit
add ax, 7h * 140h
dec ch
jmp loop_centering
loop_centering_exit:
xor di, di
mov di, 320 * 100 ;center text 320 * 100
;since the font is 14 high, I need to subtract 7 before I can draw * zoomvalue rows, so 7 * 320 * zoomvalue pixels
sub di, ax ;ax calculated earlier, how many pixels to subtract to jump above
mov bx, di ;it will be used to detect if the text still fits or not
add bx, 320 ;skips immediately to the new line, if it is bigger than this (bx) then we finish drawing
add di, 10h ;move the text away from the left edge, e.g. by 16 pixels
;scrolling support, we just start with which letters
mov si, offset drawOffset
xor ax, ax
mov al, ds:[si]
mov si, offset text
add si, ax
loop_drawLetters:
;scrolling support - if there is no space then we don't draw further
call checkIfCanDrawChar
cmp al, 0
jne drawingTextFinished
mov al, ds:[si] ;text char
cmp al, 0 ;text ended
je drawingTextFinished
call drawCharacter
inc si
;instead of 8 pixels we have to move 8 * zoomvalue, because the characters are zoomed
call increase_di
; add di, 8h ; (DEBUG)
jmp loop_drawLetters
drawingTextFinished:
pop si
pop di
pop cx
pop bx
pop ax
ret
displayText endp
;check whether we can draw a new character (if it fits in line), returns 0 in al if we can
checkIfCanDrawChar proc
push di
mov al, 1
call increase_di
cmp di, bx
jge notAllowed
mov al, 0
notAllowed:
pop di
ret
checkIfCanDrawChar endp
;adds 8 * zoomvalue to di
increase_di proc
push si
push ax
push bx
push ds
mov bx, seg zoomvalue
mov ds, bx
xor bx, bx
mov si, offset zoomvalue
mov al, 8
mov bl, ds:[si]
mul bl
add di, ax
pop ds
pop bx
pop ax
pop si
ret
increase_di endp
;adds zoomvalue to di
indcrease_di_by_zoomvalue proc
push si
push ax
push ds
push bx
mov bx, seg zoomvalue
mov ds, bx
xor ax, ax
mov si, offset zoomvalue
mov al, ds:[si]
add di, ax
pop bx
pop ds
pop ax
pop si
ret
indcrease_di_by_zoomvalue endp
;single character drawing procedure
;in al requires which character to draw
;in di requires upper left corner (offset ofc) from which to start draw
drawCharacter proc
push es
push ds
push ax
push bx
push cx
push di
push si
xor ah, ah ;only lower 8 bytes (al)
mov bl, 0eh ;14 - number of rows in font character
mul bl ;multiply ax (now al) times 14, because one character takes 14bytes
xor si, si ;si will be an iterator over 14 bytes of one character
add si, ax ;font data offset
;VIDEO - GET FONT INFORMATION (EGA, MCGA, VGA)
;bh - 02h ROM 8x14 character font pointer
;bh - 03h ROM 8x8 double dot font pointer
mov ax, 1130h
mov bh, 02h
int 10h
mov ax, es ;set the datasegment where the fonts are
mov ds, ax ;remember to change the segment when getting zoomvalue
add si, bp ;bp is returned by previous interrupt, points to memory where font data begin
;we add this value to the already calculated offset of a given character
;here the video memory begins
mov ax, 0a000h
mov es, ax
;each character takes 14bytes = 112 bits. Each set bit is a pixel on the screen. Each line is a byte.
;bytes 0-13 = ascii char with 0 index
;14-27 = index 1
;.... etc
mov ch, 0eh ;14 rows
loop_row:
cmp ch, 0
je drawingFinished
mov bh, 10000000b ;mask for bytes
mov cl, 8 ;8 columns
loop_column:
cmp cl, 0
je drawingColumnFinished
mov al, ds:[si] ;get byte of the font
and al, bh ;check if a given bit is set or not
cmp al, 0 ;if not
je skipDrawing ;dont draw anything, we clear the screen with black color, so whatever
;if yes
mov al, 0fh ;white color
; mov es:[di], al ; (DEBUG)
; inc di ; (DEBUG)
call drawSquare
skipDrawing:
call indcrease_di_by_zoomvalue
shr bh, 1 ;shift mask to the right (because it draws from left to right)
dec cl
jmp loop_column
drawingColumnFinished:
call nextRowBig ;we jump to the new 'big' row... but we have to do it zoomvalue times
;...but we are 8 * zoomvalue pixels too far, so we have to subtract
; add di, 320 ; (DEBUG)
; sub di, 8 ; (DEBUG)
inc si
dec ch
jmp loop_row
drawingFinished:
pop si
pop di
pop cx
pop bx
pop ax
pop ds
pop es
ret
drawCharacter endp
;shifts di for zoomvalue lines
nextRowBig proc
push si
push cx
push bx
push ax
push ds
mov ax, seg zoomvalue
mov ds, ax
mov si, offset zoomvalue
mov cl, ds:[si]
loop_nextRowBig:
cmp cl, 0
je exitNextRowBig
add di, 320 ;screen width
dec cl
jmp loop_nextRowBig
exitNextRowBig:
;now correction, we need to go back 8 * zoomvalue back
xor ax, ax
mov al, ds:[si]
mov bl, 8
mul bl
sub di, ax
pop ds
pop ax
pop bx
pop cx
pop si
ret
nextRowBig endp
;draws square zoomvalue by zoomvalue, requires color in al, di has to point on upper left corner of the square
drawSquare proc
push di
push si
push cx
push ax
push bx
push ds
mov bx, seg zoomvalue
mov ds, bx
mov si, offset zoomvalue
mov ch, ds:[si]
loop_drawSquare1:
cmp ch, 0
je exitDrawSquare1
mov cl, ds:[si]
loop_drawSquare2:
cmp cl, 0
je exitDrawSquare2
mov es:[di], al
inc di
dec cl
jmp loop_drawSquare2
exitDrawSquare2:
add di, 320 ;jump to next row...
xor bx, bx
mov bl, ds:[si]
sub di, bx ;...but we are 'zoomvalue' pixels too far so we have to subtract
dec ch
jmp loop_drawSquare1
exitDrawSquare1:
pop ds
pop bx
pop ax
pop cx
pop si
pop di
ret
drawSquare endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;move si until we encountered non-space character
skipSpaces proc
push ax
loop_Chars:
mov al, es:[si]
cmp al, ' '
jne skipSpacesExit
inc si
jmp loop_Chars
skipSpacesExit:
pop ax
ret
skipSpaces endp
;print ds:dx
putStr proc
push ax
;DOS 1+ - WRITE STRING TO STANDARD OUTPUT
xor al, al
mov ah, 09h
int 21h
pop ax
ret
putStr endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
zoomWrongValue:
mov dx, offset str_zoomWrongValue
call putStr
call programExitError
argumentError:
mov dx, offset str_argumentsError
call putStr
mov dx, offset str_emptyArguments
call putStr
call programExitError
programExitError:
mov dx, offset str_exitError
call putStr
;DOS 2+ - EXIT - TERMINATE WITH RETURN CODE
mov al, 1 ;exit code, error
mov ah, 4ch ;terminate program
int 21h
programExit:
;DOS 2+ - EXIT - TERMINATE WITH RETURN CODE
mov al, 0 ;exit code, 0 means success
mov ah, 4ch ;terminate program
int 21h
code1 ends
stack1 segment stack
dw 300 dup(?)
topstack dw ?
stack1 ends
end start1