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