-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinthex.asm
142 lines (124 loc) · 4.7 KB
/
printhex.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
; SPDX-License-Identifier: CC-BY-SA-3.0
;
; Author: Dirk Wolfgang Glomp
; Source: https://stackoverflow.com/a/22897576
;
; DOS Print 32 bit value stored in EAX with hexadecimal output (for 80386+)
; (on 64 bit OS use DOSBOX)
;
; Entry: EAX=32 bit value (0 - FFFFFFFF) for example
;
.DOSSEG
.MODEL small, c, os_dos
.386
.CODE
print32BitHexValue proc public
push ds
push ax ;
mov ax,@DATA ; get the address of the data segment
mov ds,ax ; store the address in the data segment register
pop ax ; EAX should now contain the entry argument again
pusha ; Push all 16-bit general purpose registers
;-----------------------
; convert the value in EAX to hexadecimal ASCIIs
;-----------------------
mov di,OFFSET ASCII32 ; get the offset address
mov cl,8 ; number of ASCII
P32_1: rol eax,4 ; 1 Nibble (start with highest byte)
mov bl,al
and bl,0Fh ; only low-Nibble
add bl,30h ; convert to ASCII
cmp bl,39h ; above 9?
jna short P32_2
add bl,7 ; "A" to "F"
P32_2: mov [di],bl ; store ASCII in buffer
inc di ; increase target address
dec cl ; decrease loop counter
jnz P32_1 ; jump if cl is not equal 0 (zeroflag is not set)
;-----------------------
; Print string
;-----------------------
mov dx,OFFSET ASCII32 ; DOS 1+ WRITE STRING TO STANDARD OUTPUT
mov ah,9 ; DS:DX->'$'-terminated string
int 21h ; maybe redirected under DOS 2+ for output to file
; (using pipe character">") or output to printer
popa
pop ds
; return to caller...
ret
print32BitHexValue endp
print16BitHexValue proc public
push ds
push ax ;
mov ax,@DATA ; get the address of the data segment
mov ds,ax ; store the address in the data segment register
pop ax ; AX should now contain the entry argument again
pusha ; Push all 16-bit general purpose registers
;-----------------------
; convert the value in AX to hexadecimal ASCIIs
;-----------------------
mov di,OFFSET ASCII16 ; get the offset address
mov cl,4 ; number of ASCII
P16_1: rol ax,4 ; 1 Nibble (start with highest byte)
mov bl,al
and bl,0Fh ; only low-Nibble
add bl,30h ; convert to ASCII
cmp bl,39h ; above 9?
jna short P16_2
add bl,7 ; "A" to "F"
P16_2: mov [di],bl ; store ASCII in buffer
inc di ; increase target address
dec cl ; decrease loop counter
jnz P16_1 ; jump if cl is not equal 0 (zeroflag is not set)
;-----------------------
; Print string
;-----------------------
mov dx,OFFSET ASCII16 ; DOS 1+ WRITE STRING TO STANDARD OUTPUT
mov ah,9 ; DS:DX->'$'-terminated string
int 21h ; maybe redirected under DOS 2+ for output to file
; (using pipe character">") or output to printer
popa
pop ds
; return to caller...
ret
print16BitHexValue endp
print8BitHexValue proc public
push ds
push ax ;
mov ax,@DATA ; get the address of the data segment
mov ds,ax ; store the address in the data segment register
pop ax ; AL should now contain the entry argument again
pusha ; Push all 16-bit general purpose registers
;-----------------------
; convert the value in EAX to hexadecimal ASCIIs
;-----------------------
mov di,OFFSET ASCII8 ; get the offset address
mov cl,2 ; number of ASCII
P8_1: rol al,4 ; 1 Nibble
mov bl,al
and bl,0Fh ; only low-Nibble
add bl,30h ; convert to ASCII
cmp bl,39h ; above 9?
jna short P8_2
add bl,7 ; "A" to "F"
P8_2: mov [di],bl ; store ASCII in buffer
inc di ; increase target address
dec cl ; decrease loop counter
jnz P8_1 ; jump if cl is not equal 0 (zeroflag is not set)
;-----------------------
; Print string
;-----------------------
mov dx,OFFSET ASCII8 ; DOS 1+ WRITE STRING TO STANDARD OUTPUT
mov ah,9 ; DS:DX->'$'-terminated string
int 21h ; maybe redirected under DOS 2+ for output to file
; (using pipe character">") or output to printer
popa
pop ds
; return to caller...
ret
print8BitHexValue endp
.DATA
ASCII32 DB "00000000h",0Dh,0Ah,"$" ; buffer for ASCII string
ASCII16 DB "0000h",0Dh,0Ah,"$" ; buffer for ASCII string
ASCII8 DB "00h",0Dh,0Ah,"$" ; buffer for ASCII string
End