initial import
[staff/goc4/2018bti7061.git] / asm-7-procedures / examples / hexdump2 / textlib.asm
1 ; Executable name : textlib
2 ; Version : 1.0
3 ; Created date : 4/10/2009
4 ; Last update : 4/10/2009
5 ; Author : Jeff Duntemann
6 ; Description : A linkable library of text-oriented procedures and tables
7 ;
8 ; Build using these commands:
9 ; nasm -f elf -g -F stabs textlib.asm
10 ;
11 SECTION .bss ; Section containing uninitialized data
12 BUFFLEN EQU 10
13 Buff resb BUFFLEN
14
15 SECTION .data ; Section containing initialised data
16
17 ; Here we have two parts of a single useful data structure, implementing the
18 ; text line of a hex dump utility. The first part displays 16 bytes in hex
19 ; separated by spaces. Immediately following is a 16-character line delimited
20 ; by vertical bar characters. Because they are adjacent, they can be
21 ; referenced separately or as a single contiguous unit. Remember that if
22 ; DumpLin is to be used separately, you must append an EOL before sending it
23 ; to the Linux console.
24 DumpLin: db " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
25 DUMPLEN EQU $-DumpLin
26 ASCLin: db "|................|",10
27 ASCLEN EQU $-ASCLin
28 FULLLEN EQU $-DumpLin
29
30 ; The HexDigits table is used to convert numeric values to their hex
31 ; equivalents. Index by nybble without a scale: [HexDigits+eax]
32 HexDigits: db "0123456789ABCDEF"
33
34 ; This table allows us to generate text equivalents for binary numbers.
35 ; Index into the table by the nybble using a scale of 4:
36 ; [BinDigits + ecx*4]
37 BinDigits: db "0000","0001","0010","0011"
38 db "0100","0101","0110","0111"
39 db "1000","1001","1010","1011"
40 db "1100","1101","1110","1111"
41
42 ; This table is used for ASCII character translation, into the ASCII
43 ; portion of the hex dump line, via XLAT or ordinary memory lookup.
44 ; All printable characters "play through" as themselves. The high 128
45 ; characters are translated to ASCII period (2Eh). The non-printable
46 ; characters in the low 128 are also translated to ASCII period, as is
47 ; char 127.
48 DotXlat:
49 db 2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh
50 db 2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h,2Eh,2Eh,2Eh,2Eh,2Eh
64 db 2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh
65
66 SECTION .text ; Section containing code
67
68 GLOBAL ClearLine, DumpChar, PrintLine, DumpLin, DUMPLEN, HexDigits
69 GLOBAL BinDigits
70
71 ;-------------------------------------------------------------------------
72 ; ClearLine: Clear a hex dump line string to 16 0 values
73 ; IN: Nothing
74 ; RETURNS: Nothing
75 ; TRASHES: Nothing
76 ; The hex dump line string is cleared to binary 0.
77
78 ClearLine:
79 push edx ; Save caller's EDX
80 mov edx,15 ; We're going to go 16 pokes, counting from 0
81 .Poke: mov eax,0 ; Tell DumpChar to poke a '0'
82 call DumpChar ; Insert the '0' into the hex dump string
83 sub edx,1 ; DEC doesn't affect CF!
84 jae .Poke ; Loop back if EDX >= 0
85 pop edx ; Restore caller's EDX
86 ret ; Go home
87
88 ;-------------------------------------------------------------------------
89 ; DumpChar: "Poke" a value into the hex dump line string.
90 ; IN: Pass the 8-bit value to be poked in EAX.
91 ; Pass the value's position in the line (0-15) in EDX
92 ; RETURNS: Nothing
93 ; TRASHES: EAX
94 ; DESCRIPTION: The value passed in EAX will be placed in both the hex dump
95 ; portion and in the ASCII portion, at the position passed
96 ; in ECX, represented by a space where it is not a printable
97 ; character.
98
99 DumpChar:
100 push ebx ; Save EBX on the stack so we don't trash
101 push edi
102 ; First we insert the input char into the ASCII portion of the dump line
103 mov bl,byte [DotXlat+eax] ; Translate nonprintables to '.'
104 mov byte [ASCLin+edx+1],bl ; Write to ASCII portion
105 ; Next we insert the hex equivalent of the input char in the hex portion
106 ; of the hex dump line:
107 mov ebx,eax ; Save a second copy of the input char
108 lea edi,[edx*2+edx] ; Calc offset into line string (ECX X 3)
109 ; Look up low nybble character and insert it into the string:
110 and eax,0000000Fh ; Mask out all but the low nybble
111 mov al,byte [HexDigits+eax] ; Look up the char equivalent of nybble
112 mov byte [DumpLin+edi+2],al ; Write the char equivalent to line string
113 ; Look up high nybble character and insert it into the string:
114 and ebx,000000F0h ; Mask out all the but second-lowest nybble
115 shr ebx,4 ; Shift high 4 bits of char into low 4 bits
116 mov bl,byte [HexDigits+ebx] ; Look up char equivalent of nybble
117 mov byte [DumpLin+edi+1],bl ; Write the char equiv. to line string
118 ;Done! Let's go home:
119 pop ebx ; Restore caller's EBX register value
120 pop edi ; Restore caller's EDI register value
121 ret ; Return to caller
122
123 ;-------------------------------------------------------------------------
124 ; PrintLine: Displays the hex dump line string via INT 80h sys_write
125 ; IN: Nothing
126 ; RETURNS: Nothing
127 ; TRASHES: Nothing
128 ; DESCRIPTION: The hex dump line string is displayed to stdout using
129 ; INT 80h sys_write.
130
131 PrintLine:
132 pushad ; Push all GP registers
133 mov eax,4 ; Specify sys_write call
134 mov ebx,1 ; Specify File Descriptor 1: Standard output
135 mov ecx,DumpLin ; Pass offset of line string
136 mov edx,FULLLEN ; Pass size of the line string
137 int 80h ; Make kernel call to display line string
138 popad ; Pop all GP registers
139 ret