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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
| %TITLE "HexEdit.asm"
; Tenie Remmel
IDEAL
JUMPS
Model Tiny
CODESEG
org 80h
ComlineLen db ?
FileName db ?
org 100h
Start:
jmp Program
LEFT_MASK = 0Fh
RIGHT_MASK = 0F0h
BUFFER_LEN = 4000h ; 16384
KEY_UP = 48h
KEY_DOWN = 50h
KEY_LEFT = 4Bh
KEY_RIGHT = 4Dh
KEY_PGUP = 49h
KEY_PGDN = 51h
KEY_F1 = 3Bh
KEY_F2 = 3Ch
ERR_MEM_LEN = 13
ERR_FIND_LEN = 14
ERR_ACCESS_LEN = 17
ERR_ZERO_LEN = 14
ErrMem db 'Out of memory'
ErrFind db 'File not found'
ErrAccess db 'File access error'
ErrZero db 'Zero-byte file'
UsageStr db 'Syntax: HEXEDIT <file>'
HelpStr db 'Keys: ',24,32,25,32,27,32,26
db ' PgUp PgDn F1=Save/Abort F2=ASCII table'
TitleStr db 'HexEditor 1.10 ю Tenie Remmel'
OffsetStr db 'OFFSET' ; Length 6, column 3
HexDataStr db 'HEX DATA' ; Length 8, column 1Fh
AscDataStr db 'ASCII DATA' ; Length 0Ah, column 41h
QuitMsg db 'Save Changes (Y/N)? ' ; Length 14h
WindowChars db 1, 'ЙН»Н'
db 1, 'є є '
db 1, 'ЗД¶В'
db 18,'є єі'
db 1, 'ЗД¶Б'
db 2, 'є є '
db 1, 'ИНјН'
db 0
FileLen dd 00000000
BufferPos dd 00000000
MaxBufferPos dd 00000000
FitsInBuffer db 00
FitsInGrid db 00
GridPos dw 0000
MaxGridPos dw BUFFER_LEN-0100h
CurRow db 00
CurCol db 00
CurPos db 00
CurMask db 00Fh
db ' :'
TempStr db 'HEXEDIT.TMP',0
TempName dw offset TempStr
SrcHandle dw 0000
TempHandle dw 0000
PartialRow db 00
;
; Entry point
;
proc Program
lea ax, [Buffer] ; Check for memory
cmp sp, ax
jna NoMem ; Extremely tiny?
lea ax, [StackTop] ; New stack top
cli ; No interrupts
xchg sp, ax ; Reset stack
sti ; Now interrupts
sub ax, sp ; Big enough for buffer?
sub ax, 2
cmp ax, BUFFER_LEN
jna MemOK ; If not, Out of Memory error
MemOK:
mov di, 80h ; Make filename ASCIIZ...
cmp [byte di], 1 ; No command line?
jle NoCmdLine
inc di
mov bl, [ComlineLen] ; Check for leading spaces
xor bh, bh
mov cx, bx
mov al, ' ' ; Присвоение al искомого значения 'CR'
repe scasb ; Просмотр на предмет искомого значения
dec di
mov si, di ; Shift back filename
mov di, 81h ; Адресация цепочки посредством es:di
mov cx, bx ; Присвоение cx длины цепочки
rep movsb ; Копирование
mov di, 81h ; Find the CR at the end
mov cx, bx ; Присвоение cx длины цепочки
mov al, 0Dh ; Присвоение al искомого значения 'CR'
repne scasb ; Просмотр на предмет искомого значения
dec di ; Put a 0 there (ASCIIZ)
xor al, al
stosb ; Накапливает цепочку значений в память
mov di, 81h ; Check for trailing spaces
mov cx, bx ; Присвоение cx длины цепочки
mov al, 20h ; Присвоение al искомого значения ' '
repne scasb ; Просмотр на предмет искомого значения
dec di ; Put a 0 there (ASCIIZ)
xor al, al
stosb ; Накапливает цепочку значений в память
lea si, [FileName] ; File on another drive?
lodsb ; Загрузка цепочки значений в аккумулятор
mov [TempStr-2], al ; Add 'd:' to TempName...
lodsb
cmp al, ':'
jnz NoDrive
sub [TempName], 2 ; Point TempName to new name
NoDrive:
mov ax, 3D00h ; Open source file
lea dx, [FileName]
int 21h
jnc FileOK
jmp NotFound
FileOK:
mov [SrcHandle], ax
mov ah, 3Ch ; Open temp file
xor cx, cx
mov dx, [TempName]
int 21h
jc FileError ; Check
mov [TempHandle], ax
lea dx, [Buffer] ; Offset of buffer
CopyLoop:
mov ah, 3Fh ; Read function
mov bx, [SrcHandle] ; Source handle
mov cx, BUFFER_LEN ; Buffer length
int 21h ; Read file
jnc copy1 ; Check for Zero etc.
cmp ax, 0
jnz FileError
copy1:
mov cx, ax ; Get length
mov ah, 40h ; Write function
mov bx, [TempHandle] ; Tempfile handle
int 21h ; Write data
jc FileError ; Check
cmp cx, BUFFER_LEN ; End of file?
jz CopyLoop ; If not, loop back
CopyDone:
mov ah, 3Eh ; Close source file
mov bx, [SrcHandle]
int 21h
mov ax, 4202h ; Get file size
mov bx, [TempHandle]
xor cx, cx
xor dx, dx
int 21h
jc FileError ; Check
mov [word FileLen], ax ; Save it 'dd 00000000'
mov [word FileLen+2], dx
cmp dx, 0 ; Does it fit?
jnz NotFit
cmp ax, 0 ; Zero byte file?
jnz size1
jmp ZeroErr
size1:
cmp ax, BUFFER_LEN ; Fits in buffer?
ja NotFit
mov [FitsInBuffer], 1
mov [word MaxBufferPos], 0 ; If so, max buffer pos = 0 'dd 00000000'
mov [word MaxBufferPos+2], 0
mov cx, ax ; And max grid pos = length - 100h
sub cx, 100h
mov [MaxGridPos], cx
cmp ax, 100h ; Fits in grid?
ja FitIn
mov [FitsInGrid], 1 ; If so, max grid pos = 0
mov [MaxGridPos], 0
jmp FitIn
NotFit:
sub ax, BUFFER_LEN ; Save max buffer pos
sbb dx, 0
mov [word MaxBufferPos], ax ; 'dd 00000000'
mov [word MaxBufferPos+2], dx
FitIn:
mov ax, 4200h ; Return file pointer to start
mov bx, [TempHandle]
xor cx, cx
xor dx, dx
int 21h
jc FileError ; Check
mov ax, 3
int 10h
call DrawBackground
call SetupAscTable ; Set up ASCII table
call FillBuffer ; Initialize
jmp ReDraw
KeyLoop:
mov ah, 0 ; Get key
int 16h
cmp al, 0 ; Not ASCII, command
jz Command
HexDigit:
cmp al, '0' ; Digit?
jl KeyLoop ; No
cmp al, '9'
jle Is_0_to_9 ; Yes
cmp al, 'A' ; "A" to "F"?
jl KeyLoop ; No
cmp al, 'F'
jle Is_A_to_F ; Yes
sub al, ('a' - 'A') ; "a" to "f"?
cmp al, 'A'
jl KeyLoop ; No?
cmp al, 'F'
jg KeyLoop ; No?
Is_A_to_F:
sub al, ('A' - 0ah) ; Yes, make binary
jmp conv1
Is_0_to_9:
sub al, '0' ; Make binary
conv1:
mov ah, [CurMask] ; At the left?
cmp ah, LEFT_MASK
jnz conv2
mov cl, 4 ; If so, shift left
shl al, cl
conv2:
mov bl, [CurPos] ; Cursor position
xor bh, bh
mov si, [GridPos] ; Grid position
mov dl, [byte Buffer+si+bx] ; Byte from buffer
and dl, ah ; Replace digit
or dl, al
mov [byte Buffer+si+bx], dl ; Write back to buffer
jmp GoRight ; Advance cursor
Command:
cmp ah, KEY_UP ; Up?
jz GoUp
cmp ah, KEY_DOWN ; Down?
jz GoDown
cmp ah, KEY_LEFT ; Left?
jz GoLeft
cmp ah, KEY_RIGHT ; Right?
jz GoRight
cmp ah, KEY_PGUP ; PgUp?
jz GoPgUp
cmp ah, KEY_PGDN ; PgDn?
jz GoPgDn
cmp ah, KEY_F1 ; F1?
jz Quit
cmp ah, KEY_F2 ; F2?
jz ShowAscTable
jmp KeyLoop ; Loop back
GoUp:
mov bx, 10h ; Move of 16
cmp [CurRow], 0 ; First row?
jz MoveGridLeft ; Move grid up
sub [CurPos], 10h ; Otherwise,
dec [CurRow] ; move cursor up
jmp ReDraw ; Redraw screen
GoDown:
mov bx, 10h ; Move of 16
cmp [CurRow], 0Fh ; Last row?
jz MoveGridRight ; Move grid down
add [CurPos], 10h ; Otherwise,
inc [CurRow] ; move cursor down
jmp ReDraw ; Redraw screen
GoLeft:
cmp [CurMask], RIGHT_MASK ; Right side?
jnz LeftSide
not [CurMask] ; Other side
jmp ReDraw ; Redraw screen
LeftSide:
not [CurMask] ; Other side
mov bx, 1 ; Move of 1
cmp [CurCol], 0 ; First column?
jz PrevRow ; Prev. row
dec [CurCol] ; Otherwise,
dec [CurPos] ; prev. column
jmp ReDraw
PrevRow:
cmp [CurRow], 0 ; First row?
jz MoveGridLeft ; Move grid left
mov [CurCol], 0Fh ; Otherwise,
dec [CurRow] ; prev. row, last column
dec [CurPos]
jmp ReDraw ; Redraw screen
GoRight:
cmp [CurMask], LEFT_MASK ; Left side?
jnz RightSide
not [CurMask] ; Other side
jmp ReDraw ; Redraw screen
RightSide:
not [CurMask] ; Other side
mov bx, 1 ; Move of 1
cmp [CurCol], 0Fh ; Last column?
jz NextRow ; Next row
inc [CurCol] ; Otherwise,
inc [CurPos] ; next column
jmp ReDraw
NextRow:
cmp [CurRow], 0Fh ; Last row?
jz MoveGridRight ; Move grid right
mov [CurCol], 0 ; Otherwise,
inc [CurRow] ; next row, first column
inc [CurPos]
jmp ReDraw ; Redraw screen
GoPgUp:
mov bx, 100h ; Move of 100h
jmp MoveGridLeft ; Move grid left
GoPgDn:
mov bx, 100h ; Move of 100h
jmp MoveGridRight ; Move grid right
MoveGridLeft:
mov ax, [GridPos] ; Moving off left of buffer?
cmp ax, bx ; If not, just move the grid
jl BufLeft ; left and redraw the screen
sub [GridPos], bx
jmp ReDraw
BufLeft:
call WriteBuffer ; Write buffer
mov ax, [word BufferPos] ; Get buffer position 'dd 00000000'
mov dx, [word BufferPos+2]
mov cx, [GridPos] ; Grid position to center
add cx, (BUFFER_LEN / 2) ; of new buffer
sub ax, (BUFFER_LEN / 2)
sbb dx, 0
jnc NotOffLeft ; Off the start of the file?
mov dx, 0 ; Start at the start
mov ax, dx
mov cx, [GridPos] ; Grid where it goes...
add cx, [word BufferPos] ; 'dd 00000000'
cmp cx, bx ; Totally off the left?
jge NotOffLeft
mov [CurRow], 0
mov [CurCol], 0
mov [CurPos], 0
mov [CurMask], LEFT_MASK
mov cx, bx
NotOffLeft:
sub cx, bx
mov [GridPos], cx
mov [word BufferPos], ax ; New buffer position 'dd 00000000'
mov [word BufferPos+2], dx
call FillBuffer ; Fill buffer
jmp ReDraw ; Redraw screen
MoveGridRight:
mov ax, [GridPos] ; Moving off right of buffer?
add ax, bx ; If not, just move the grid
cmp ax, [MaxGridPos] ; right and redraw the screen
jg BufRight
add [GridPos], bx
jmp ReDraw
BufRight:
cmp [FitsInBuffer], 0 ; Fits in the buffer?
jz NoFitR
mov [CurRow], 0Fh
mov [CurCol], 0Fh
mov [CurPos], 0FFh
mov [CurMask], RIGHT_MASK
push [MaxGridPos]
pop [GridPos]
jmp ReDraw
NoFitR:
call WriteBuffer ; Write buffer
mov ax, [word BufferPos] ; Get buffer position 'dd 00000000'
mov dx, [word BufferPos+2]
mov cx, [GridPos] ; Grid position to center
sub cx, (BUFFER_LEN / 2) ; of new buffer
add ax, (BUFFER_LEN / 2)
adc dx, 0
add cx, bx
cmp dx, [word MaxBufferPos+2] ; Off the end of the file? 'dd 00000000'
jl NotOffRight
cmp ax, [word MaxBufferPos] ; 'dd 00000000'
jbe NotOffRight
mov ax, [word MaxBufferPos] ; Maximum position 'dd 00000000'
mov dx, [word MaxBufferPos+2]
mov cx, [GridPos] ; Grid where it goes...
mov si, ax
mov di, dx
sub si, [word BufferPos] ; 'dd 00000000'
sbb di, [word BufferPos+2]
sub cx, si
add cx, bx
cmp cx, [MaxGridPos] ; Totally off the right?
jbe NotOffRight
mov cx, [MaxGridPos]
mov [CurRow], 0Fh
mov [CurCol], 0Fh
mov [CurPos], 0FFh
mov [CurMask], RIGHT_MASK
NotOffRight:
mov [GridPos], cx
mov [word BufferPos], ax ; New buffer position 'dd 00000000'
mov [word BufferPos+2], dx
call FillBuffer ; Fill buffer
ReDraw:
call DrawGrid ; Redraw screen
mov ah, 2 ; Position Cursor func.
mov bh, 0 ; Page 0
mov dh, [CurRow] ; Row + 5
add dh, 5
mov dl, [CurCol] ; Column * 3
mov bl, dl
shl dl, 1
add dl, bl
add dl, 12 ; + 12
mov al, [CurMask] ; + 1 if right digit
not al
and al, 1
add dl, al
int 10h ; Do it
jmp KeyLoop ; Jump to key loop
ShowAscTable:
mov ax, 501h ; Show page 1
int 10h
mov ah, 0 ; Wait for key
int 16h
mov ax, 500h ; Reset to page 0
int 10h
jmp KeyLoop ; Jump to key loop
Quit:
mov ax, 3 ; Reset video mode
int 10h
call WriteBuffer ; Write buffer
mov ah, 3Eh ; Close temp file
mov bx, [TempHandle]
int 21h
jc FileError ; Check
lea bp, [QuitMsg] ; Display 'Save?' message
push ds
pop es
mov ax, 1301h
mov bx, 7
mov cx, 20
mov dx, 0
int 10h
QuitKeyLoop:
mov ah, 0 ; Get a key
int 16h
cmp al, 'N' ; No
jz NoSave
cmp al, 'n'
jz NoSave
cmp al, 'Y' ; Yes
jz Save
cmp al, 'y'
jz Save
jmp QuitKeyLoop ; Loop back
NoSave:
mov al, 'N' ; Show char
int 29h
mov al, 0Ah ; Two linefeeds
int 29h
int 29h
mov ah, 41h ; Delete temp file
mov dx, [TempName]
int 21h
jc FileError ; Check
jmp Exit
Save:
mov al, 'Y' ; Show char
int 29h
mov al, 0Ah ;Two linefeeds
int 29h
int 29h
mov ah, 41h ; Delete old file
lea dx, [FileName]
int 21h
jc FileError ; Check
mov dx, [TempName] ; Rename new file
lea di, [FileName] ; to old filename
mov ah, 56h
int 21h
jc FileError ; Check
Exit:
mov ax, 4C00h
int 21h
endp Program
;
; DrawBackground
;
proc DrawBackground
lea si, [WindowChars] ; Get offset of table
xor ch, ch ; Clear CH
mov ax, 0B800h ; Point ES to video memory
mov es, ax
xor di, di ; Point DI to UL corner
DrawLoop1:
lodsb ; Get byte
cmp al, 0 ; End of table?
jz DBGDone ;Done
mov cl, al ; Put number of times in CX
DrawLoop2:
push di ; Save DI
mov bx, si ; Get pointer in BX
mov al, [bx] ; Get first char
stosb ; Накапливает цепочку значений в память
inc di
mov al, [bx+1] ; Get line char
push cx ; Save CX
mov cx, 78 ; 78 chars
DrawLoop3:
stosb ; Накапливает цепочку значений в память
inc di
loop DrawLoop3 ; Loop back
pop cx ; Get back CX
mov ax, [bx+2] ; Get end char
stosb ; Накапливает цепочку значений в память
pop di ; Get back DI
add di, 20 ; Char 10
mov ax, [bx+3] ; Get middle char
stosb ; Накапливает цепочку значений в память
add di, 99 ; Char 60
stosb ; Накапливает цепочку значений в память
add di, 39 ; Next line
loop DrawLoop2 ; Loop back
add si, 4 ; Increment SI
jmp DrawLoop1 ; Loop back
DBGDone:
push ds ; Get DS in ES
pop es
mov ax, 1300h ; BIOS Write string
mov bx, 7 ; Attribute 07 (Wh/Bk)
mov cx, 29 ; 29 chars,
mov dx, 0119h ; Row 01h, Column 19h
lea bp, [TitleStr] ; Title string
int 10h
mov cx, 54 ; 54 chars,
mov dx, 170Dh ; Row 17h, Column 0Dh
lea bp, [HelpStr] ; Help string
int 10h
mov cx, 6 ; 6 chars,
mov dx, 0303h ; Row 03h, Column 03h
lea bp, [OffsetStr] ; "OFFSET"
int 10h
mov cx, 8 ; 8 chars,
mov dx, 031Fh ; Row 03h, Column 1Fh
lea bp, [HexDataStr] ; "HEX DATA"
int 10h
mov cx, 10 ; 10 chars,
mov dx, 0341h ; Row 03h, Column 41h
lea bp, [AscDataStr] ; "ASCII DATA"
int 10h
ret
endp DrawBackground
;
; FillBuffer
;
proc FillBuffer
mov ax, 4200h ; Move pointer to BufferPos 'dd 00000000'
mov bx, [TempHandle]
mov cx, [word BufferPos+2]
mov dx, [word BufferPos]
int 21h
jc FileError ; Check
mov ah, 3Fh ; Read file
mov bx, [TempHandle]
mov cx, BUFFER_LEN ; Fill buffer
lea dx, [Buffer]
int 21h
jc FileError ; Check
ret
endp FillBuffer
;
; WriteBuffer
;
proc WriteBuffer
push ax bx cx dx ; Push registers
mov ax, 4200h ; Move pointer to BufferPos
mov bx, [TempHandle]
mov cx, [word BufferPos+2] ; 'dd 00000000'
mov dx, [word BufferPos]
int 21h
jc FileError ; Check
mov ah, 40h ; Write file
mov bx, [TempHandle]
cmp [FitsInBuffer], 0
jz NoFit
mov cx, [word FileLen] ; File length or 'dd 00000000'
jmp DoWrite
NoFit:
mov cx, BUFFER_LEN ; Buffer length
DoWrite:
lea dx, [Buffer]
int 21h
jc FileError ; Check
pop dx cx bx ax ; Pop registers
ret
endp WriteBuffer
;
; SetupAscTable
;
proc SetupAscTable
mov ax, 0B90Ah ; Point to video memory
mov es, ax ; at page 01, shifted-row
xor di, di
mov cx, 2000 ; Clear screen
mov ax, 720h
rep stosw
mov ax, 2000h ; AH = ' ', AL = 0
AscLoop1:
xor ch, ch ; Row 0
push ax
mov al, 14 ; DI = 14 * column + 4
mul cl
add ax, 4
mov di, ax
pop ax
AscLoop2:
call PutHex ; Put byte in hex
xchg ah, al
stosb ; Two spaces (Накапливает значение в память
inc di
stosb ; Накапливает цепочку значений в память
inc di
xchg ah, al
stosb ; Put byte in ASCII
inc di
add di, 150 ; Next row
inc al ; All the chars?
jz AscLines ; Done
inc ch ; Next row
cmp ch, 24 ; Last row?
jb AscLoop2 ; Loop back
inc cl ; Next column
cmp cl, 11 ; Last column?
jb AscLoop1 ; Loop back
AscLines:
mov al, 'і' ; Vertical lines
mov di, 2 ; Start at column 01
ALinesLoop:
push di
mov cx, 24 ; 24 rows
VLineLoop:
stosb ; Draw the line
add di, 159 ; Next row
loop VLineLoop ; Loop back
pop di
add di, 14 ; Next column
cmp di, 160 ; Past the end?
jb ALinesLoop ; Loop back
mov ah, 2 ; Hide cursor
mov bh, 1
mov dx, 1901h
int 10h
ret
endp SetupAscTable
;
; DrawGrid
;
proc DrawGrid
mov ax, 0B800h ; Point to video memory
mov es, ax
cmp [FitsInGrid], 1 ; Fits in grid?
jz DrawSmall
mov ax, [word BufferPos] ; Get offset in file
mov dx, [word BufferPos+2]
add ax, [GridPos] ; Adjust for grid position
adc dx, 0
mov bx, 500h ; Start at row 05
mov cx, 10h ; 10h rows
lea si, [Buffer] ; Get offset of data
add si, [GridPos]
GridLoop:
call DrawRow ; Draw a row
loop GridLoop ; Repeat
jmp GridDone ; Done
DrawSmall:
mov ax, 0 ; Fits, offset = 0
mov dx, ax
mov bx, [word FileLen] ; File length 'dd 00000000'
push bx
mov cl, 4 ; Length / 10h rows
shr bx, cl
mov cx, bx
mov bx, 500h ; Start at row 05
lea si, [Buffer] ; Get offset of data
GridLoopS:
call DrawRow ; Draw a row
loop GridLoopS ; Repeat
pop cx ; File length MOD 10h
and cx, 0Fh ; bytes in partial row
cmp cx, 0 ; Zero? Done
jz GridDone
mov bl, 1
call DrawRow ; Draw partial row
GridDone:
ret
endp DrawGrid
;
; DrawRow
;
proc DrawRow
; Args: DX:AX=Offset, BH=physical row, SI=16 bytes data
; At end: DX:AX inc by 10h, BH inc by 1, SI inc by 10h
push cx di bx ; Push registers
push ax ax
mov [PartialRow], bl
mov al, 160 ; Get offset for (0, BH) in DI
mul bh
mov di, ax
pop ax
add di, 4 ; column 2
call PrintLong
add di, 4 ; Past the line
mov bx, 0 ; Start byte 0
cmp [PartialRow], 1 ; Partial row?
jz part1
mov cx, 10h ; 10h bytes
jmp HexLoop
part1:
push cx ; Save CX and DI
push di
HexLoop:
mov al, [si+bx] ; Current byte
call PutHex ; Put byte in hex
mov al, ' ' ; A space
stosb ; Store it
inc di ; Skip attribute
inc bx ; Next byte
loop HexLoop ; Loop back
cmp [PartialRow], 1 ; Partial row?
jz part2
add di, 4 ; Past the line
mov cx, 10h ; 10h bytes
jmp AscLoop
part2:
pop di ; Get back DI
add di, 100 ; To ASCII window
pop cx ; Get back CX
AscLoop:
movsb ; Move byte
inc di ; Skip attribute
loop AscLoop ; Loop back
pop ax ; Get back AX
add ax, 10h ; DX:AX = DX:AX + 10h
adc dx, 0
pop bx ; Get back BX
inc bh ; Next Row
pop di cx ; Pop registers
ret
endp DrawRow
;
; PrintLong
;
proc PrintLong
; Args: DX:AX=num, DI=offset, assumes ES=video memory
push ax
mov al, dh ; First byte
call PutHex
mov al, dl ; Second
call PutHex
mov al, ah ; Third
call PutHex
pop ax ; Fourth
call PutHex
ret
endp PrintLong
;
; PutHex
;
proc PutHex
; Args: AL=byte, DI=offset; assumes es=video memory
push ax cx ; Push these
push ax ; Save AL
mov cl, 4 ; Do high nibble
shr al, cl ; Put in low nibble
call ConvShow ; Convert and show
pop ax ; Get back AL
and al, 0Fh ; Only low nibble
call ConvShow ; Convert and show
pop cx ax ; Pop saved registers
ret
ConvShow:
add al, 90h ; Allison's algorithm
daa
adc al, 40h
daa
stosb ; Store and show
inc di
ret
endp PutHex
proc ErrorExit
NoCmdLine:
lea bp, [UsageStr] ; No command line,
mov cx, 22 ; print usage message
jmp ErrExit
NotFound:
lea bp, [ErrFind] ; File not found
mov cx, ERR_FIND_LEN
jmp ErrExit
FileError:
lea bp, [ErrAccess] ; File access error
mov cx, ERR_ACCESS_LEN
jmp ErrExit
ZeroErr:
lea bp, [ErrZero] ; Zero-byte file
mov cx, ERR_ZERO_LEN
jmp ErrExit
NoMem:
lea bp, [ErrMem] ; Out of memory
mov cx, ERR_MEM_LEN
ErrExit:
mov ax, 3 ; Show a string,
int 10h ; specified in BP, and
mov ax, 1301h ; exit with an error
mov bx, 7 ; BIOS show string
mov dx, 0
push ds
pop es
int 10h ; BIOS position cursor
mov ah, 2
mov dx, 100h
int 10h
mov ah, 4ch
int 21h
endp ErrorExit
PC = $
PC = PC + 256
StackTop = PC - 2 ; Stack position
Buffer = PC ; Data buffer
END Start
; -------------------------------------------------------
; <<eof>> HexEdit.asm
; ------------------------------------------------------- |