summaryrefslogtreecommitdiff
path: root/stdio.asm
blob: 161d1a3a01f8cbe23752bcc8fc1af7c8f82e1b9d (plain) (blame)
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
%ifndef _STDIO_ASM
%define _STDIO_ASM

%include "console.asm"

puts:
	; Write string buffer at cursor position
	push bp
	mov bp, sp
	pusha

	mov si, [bp + 4]	; address of string buffer
	mov bx, 0000h		;
	mov ah, 0eh		; BIOS - teletype

.loop:
	lodsb			; load byte at [si] into al
	or al, 0		; 0 | 0 = 0 (detect null terminator)
	je .end
	call putc		; write character
	jmp .loop
.end:
	popa
	mov sp, bp
	pop bp
	ret


printi:
	push bp
	mov bp, sp
	push ax
	push cx

	mov ax, [bp + 4]
	mov cx, 0
	push cx
	push word [bp + 4]    ; integer WORD
	call putint
	add sp, 2 * 2
.return:
	pop cx
	pop ax
	mov sp, bp
	pop bp
	ret


printh:
	push bp
	mov bp, sp
	push ax
	push cx

	mov cx, 0
	push cx
	push word [bp + 4]    ; integer WORD
	call puthex
	add sp, 2 * 2
.return:
	pop cx
	pop ax
	mov sp, bp
	pop bp
	ret


puthex:
	push bp
	mov bp, sp

	push ax
	push bx
	push cx
	push dx
	push di

	xor di, di
	mov ax, [bp + 4]	; value to print
	mov cx, [bp + 6]	; padding count (+leading zeros)

	cmp cx, 0
	jg .divide
	inc cx

	.divide:
		mov bx, 10h		; set divisor

		xor dx, dx		; clear mod
		div bx			; divide by 16

		cmp dl, 10		; don't adjust values less than 10
		jl .decimal
		.alpha:
			sub dl, 10	; (remainder - 10) -> align with ascii (base 10)
			add dl, 'A'	; (remainder + 'A') -> ascii offset conversion
			jmp .collect
		.decimal:
			or dl, 30h	; remainder -> ascii

	.collect:
		push dx			; push ascii value onto stack
		inc di			; increment stack counter

		cmp ax, 0		; loop if al != 0
		jne .divide

		cmp di, cx		; only pad zeros if padding count is greater than
					; the number of elements pushed to the stack
		jge .write

	mov dx, 30h			; store padding byte (ascii zero)
	sub cx, di			; How many zeros will we pad?
	.padding:
		push dx			; push padding byte
		inc di			; increment stack counter
		dec cx			; decrement padding counter
		jne .padding		; loop until CX == 0

	.write:
		dec di			; decrement stack counter

		pop ax			; pop ascii value off stack
		call putc		; print value

		cmp di, 0
		jne .write
.return:
	pop di
	pop dx
	pop cx
	pop bx
	pop ax
	mov sp, bp
	pop bp
	ret


putint:
	push bp
	mov bp, sp
	pusha

	mov ax, [bp + 4]	; value to print
	mov cx, [bp + 6]	; padding count (+leading zeros)
	mov di, 00h			; inner loop count

	.divide:
		mov bx, 0Ah		; set divisor

		xor dx, dx		; clear mod
		div bx			; divide by 10
		or dl, 30h		; remainder -> ascii

	.collect:
		push dx			; push ascii value onto stack
		inc di			; increment stack counter

		cmp al, 0		; loop if al != 0
		jne .divide

		cmp di, cx		; only pad zeros if padding count is greater than
					; the number of elements pushed to the stack
		jge .write

	mov dx, 30h			; store padding byte (ascii zero)
	sub cx, di			; How many zeros will we pad?
	.padding:
		push dx			; push padding byte
		inc di			; increment stack counter
		dec cx			; decrement padding counter
		jne .padding		; loop until CX == 0

	.write:
		pop ax			; pop first value of integer
		dec di			; decrement our stack counter
		call putc		; write character
		cmp di, 0		; done?
		jne .write
.return:
	popa

	mov sp, bp
	pop bp
	ret


printf:
	push bp
	mov bp, sp
	pusha

	mov si, [bp + 4]	; source index is format string address
	add bp, 6		; set base pointer to beginning of '...' args

	xor cx, cx		; initialize width modifier count

	cld					; clear direction flag
	.main_string:
		xor ax, ax
		lodsb				; load byte in format string
						; BEGIN READING FORMAT STRING

		cmp al, '\'			; trigger control code expansion
		je .parse_control

		cmp al, '%'			; trigger parser on '%' symbol
		je .parse_fmt

		cmp al, 0			; if we are at the end of format string
		je .return			; then we are done printing

		call putc			; write character
						; when character is not a format specifier

		jmp .main_string		; read until the end of the string

		.parse_control:
			lodsb				; get next byte

			cmp al, 't'			; TAB
			je .do_TAB

			cmp al, 'n'			; new line
			je .do_LF

			cmp al, 'r'			; carriage return
			je .do_CR

			jmp .do_default

			.do_TAB:
				mov ax, ASCII_TAB
				call putc
				jmp .main_string

			.do_LF:
				mov ax, ASCII_CR	; home the line
				call putc

				mov ax, ASCII_LF	; increment line
				call putc
				jmp .main_string

			.do_CR:
				mov ax, ASCII_CR	; home the line
				call putc
				jmp .main_string

		.parse_fmt:
			lodsb				; get next byte

			mov cx, ax			; store incoming byte into CX
			sub cx, 30h			; subtract '0' from CX
			cmp cx, 9			; is ascii number?
			jbe .do_width_modifier		; then, use CX as format width

			xor cx, cx			; else, clear format width counter

		.parse_fmt_post_width:
			cmp al, '%'			; '%%' - just print the character
			je .do_percent_escape

			cmp al, 'c'			; '%c' - process character
			je .do_char

			cmp al, 'd'			; '%d' - process signed int
			je .do_int

			cmp al, 'i'			; '%i' - process signed int
			je .do_int

			cmp al, 'u'			; '%u' - process unsigned int
			je .do_uint

			cmp al, 'x'			; '%x' - process hexadecimal
			je .do_hex

			cmp al, 's'			; '%s' - process string
			je .do_string

			cmp al, 'p'			; '%p' - process pointer
			je .do_pointer

			jmp .do_default			; Matched nothing, so handle it

			; ---REMEMBER---
			; Our base pointer has been shifted
			; ---------------------------------
			; fmt  = bp + 4
			; arg1 = bp + 6 [<- we are here]
			; arg2 = bp + 8
			; arg3 = bp + 10
			; ...  = bp + ??

			.do_percent_escape:
				mov ax, '%'
				call putc
				jmp .main_string

			.do_char:
				mov ax, [bp]
				call putc
				jmp .main_string

			.do_hex:
				mov ax, [bp]
				push cx
				push ax
				call puthex
				add sp, 2 * 2
				jmp .parse_fmt_done

			.do_int:
			.do_uint:
				mov ax, [bp]
				push cx
				push ax
				call putint
				add sp, 2 * 2
				jmp .parse_fmt_done

			.do_string:
				mov ax, [bp]
				push ax
				call puts
				add sp, 2
				jmp .parse_fmt_done

			.do_pointer:
				push cx
				push word [bp]
				call puthex
				add sp, 2 * 2
				jmp .parse_fmt_done

			.do_width_modifier:
				lodsb	; get next byte (hopefully a format char)
					; and jump back into format parser to finish
					; things up
				jmp .parse_fmt_post_width

			.do_default:
				; nothing found

	.parse_fmt_done:
		add bp, 2		; increment base pointer by one WORD
					; <<< these are our function arguments >>>
		jmp .main_string	; keep reading the format string

.return:
	popa				; restore all registers
	mov sp, bp
	pop bp
	ret

%endif