]> git.dujemihanovic.xyz Git - nameless-os.git/blob - print.s
3a46c724efa996310ab365d46c3c6f5330e47fb2
[nameless-os.git] / print.s
1 %define CURRENT_ADDR cs:di
2
3 ; Prints a null terminated string of your choice.
4 ; Arguments:
5 ; CS:DI - pointer to the string you want to print
6 ; BL - 0 prints newline, otherwise no
7 print
8 pusha
9 mov ah, 0Eh ; teletype print
10 .write2
11 mov al, [CURRENT_ADDR] ; get current char and put it in al
12 cmp al, 0 ; check if al is null (string terminator)
13 je .newline ; if it is, start a new line
14 int 10h ; otherwise write the char
15 inc di ; increment pointer to string
16 jmp .write2 ; jump back to this section
17 .newline
18 cmp bl, 1
19 je .done
20 mov al, 0Dh ; carriage return
21 int 10h
22 mov al, 0Ah ; line feed
23 int 10h
24 .done
25 popa
26 ret ; return
27
28 ; Prints a word (16-bit value) in hex format.
29 ; Arguments:
30 ; DX - word to print
31 ; BL - 0 will print a newline, 1 won't
32 print_word
33 pusha ; push all regs to stack
34 mov di, HEX_OUT+5 ; set destination index to last char in HEX_OUT
35 mov ax, dx ; copy argument to accumulator
36 and al, 00001111b ; extract the low nibble of the low half of the accumulator
37 call .compare ; fill in the string with correct value
38 dec di ; decrement string pointer
39 mov ax, dx ; copy argument to accumulator
40 and al, 11110000b ; extract high nibble of low half of accumulator
41 shr al, 4 ; shift high nibble to low nibble
42 call .compare ; fill in string with correct value
43 dec di
44 mov al, ah ; copy high portion of accumulator to low
45 and al, 00001111b ;
46 call .compare
47 dec di
48 mov al, ah
49 and al, 11110000b
50 shr al, 4
51 call .compare
52 mov di, HEX_OUT
53 call print ; write string
54 popa
55 ret
56
57 .compare
58 cmp al, 0 ; compare al with 0
59 jne .one ; if not equal, compare with 1
60 mov byte [CURRENT_ADDR], '0' ; set character to ASCII 0
61 ret ; return
62 .one
63 cmp al, 1
64 jne .two
65 mov byte [CURRENT_ADDR], '1'
66 ret
67 .two
68 cmp al, 2
69 jne .three
70 mov byte [CURRENT_ADDR], '2'
71 ret
72 .three
73 cmp al, 3
74 jne .four
75 mov byte [CURRENT_ADDR], '3'
76 ret
77 .four
78 cmp al, 4
79 jne .five
80 mov byte [CURRENT_ADDR], '4'
81 ret
82 .five
83 cmp al, 5
84 jne .six
85 mov byte [CURRENT_ADDR], '5'
86 ret
87 .six
88 cmp al, 6
89 jne .seven
90 mov byte [CURRENT_ADDR], '6'
91 ret
92 .seven
93 cmp al, 7
94 jne .eight
95 mov byte [CURRENT_ADDR], '7'
96 ret
97 .eight
98 cmp al, 8
99 jne .nine
100 mov byte [CURRENT_ADDR], '8'
101 ret
102 .nine
103 cmp al, 9
104 jne .ten
105 mov byte [CURRENT_ADDR], '9'
106 ret
107 .ten
108 cmp al, 0Ah
109 jne .eleven
110 mov byte [CURRENT_ADDR], 'A'
111 ret
112 .eleven
113 cmp al, 0Bh
114 jne .twelve
115 mov byte [CURRENT_ADDR], 'B'
116 ret
117 .twelve
118 cmp al, 0Ch
119 jne .thirteen
120 mov byte [CURRENT_ADDR], 'C'
121 ret
122 .thirteen
123 cmp al, 0Dh
124 jne .fourteen
125 mov byte [CURRENT_ADDR], 'D'
126 ret
127 .fourteen
128 cmp al, 0Eh
129 jne .fifteen
130 mov byte [CURRENT_ADDR], 'E'
131 ret
132 .fifteen
133 mov byte [CURRENT_ADDR], 'F'
134 ret
135
136 HEX_OUT db "0x0000", 0