summaryrefslogtreecommitdiffstats
path: root/printmsg.asm
blob: 516b5e2d73bce3523a0dba45987a58e94602b497 (plain)
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
/* -*- asm -*- ----------------------------------------------------------- *
 *   
 *   Copyright 2013 H. Peter Anvin - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *   
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *   
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

#include "v9fs.h"

	.code16
	.section ".text","ax"

/* ------------------------------------------------------------------------- *
 *  puterr
 *
 *  Print a message (pointer on the stack) on standard error, prefixed by
 *  "v9fs: "
 * ------------------------------------------------------------------------- */
GPROC(puterr)
	printmsg "v9fs: "
	/* Fall through */
END(puterr)

/* ------------------------------------------------------------------------- *
 *  puts
 *
 *  Print a message (pointer on the stack) on standard error.
 * ------------------------------------------------------------------------- */

GPROC(puts)
	pushaw
	movw %sp,%bp
	movw 18(%bp),%di
	movw %di,%dx
	xorb %al,%al
	movw %di,%cx
	notw %cx
	cld
	repne scasb
	/* Now %di points to the byte *after* the terminating null */
	leaw -1(%di),%cx
	subw %dx,%cx
	movw $2,%bx		/* 2 = stderr */
	movb $0x40,%ah
1:
	jcxz 2f
	int $0x21
	jc 2f
	subw %ax,%cx
	jmp 1b
2:
	popaw
	retw $2
END(puts)

/* ------------------------------------------------------------------------- *
 *  printnum
 *
 *  Print an unsigned 32-bit integer on the stack
 * ------------------------------------------------------------------------- */

GPROC(printnum)
	pushl %eax
	pushl %edx
	pushl %ecx
	pushw %di
	movw %sp,%di
	movl (2+1*2+3*4)(%di),%eax
	subw $12,%sp
	decw %di
	xorl %ecx,%ecx
	movb %cl,(%di)
	movb $10,%cl
1:
	xorl %edx,%edx
	divl %ecx
	decw %di
	addb $0x30,%dl
	movb %dl,(%di)
	andl %eax,%eax
	jnz 1b

	pushw %di
	call puts

	addw $12,%sp
	popw %di
	popl %ecx
	popl %edx
	popl %eax
	retw $4
END(printnum)

/* ------------------------------------------------------------------------- *
 *  printhex4
 *
 *  Print an hexadecimal 4-digit number from the stack
 * ------------------------------------------------------------------------- */

GPROC(printhex4)
	pushw %ax
	pushw %cx
	pushw %dx
	pushw %di
	cld
	subw $6,%sp
	movw %sp,%di
	movw (2+4*2+6)(%di),%dx
	movw $4,%cx
1:
	rolw $4,%dx
	movb %dl,%al
	andb $0x0f,%al
	addb $0x30,%al
	cmpb $0x3a,%al
	jb 2f
	addb $(0x41-0x3a),%al
2:
	stosb
	loopw 1b
	movb %cl,(%di)		/* %cl == 0 */
	pushw %sp		/* %sp -> address of buffer */
	call puts

	addw $6,%sp
	popw %di
	popw %dx
	popw %cx
	popw %ax
	retw $2
END(printhex4)

/* ------------------------------------------------------------------------- *
 *  printhex8
 *
 *  Print an hexadecimal 8-digit number from the stack
 * ------------------------------------------------------------------------- */

GPROC(printhex8)
	pushw %bp
	movw %sp,%bp
	pushw 6(%bp)
	call printhex4
	pushw 4(%bp)
	call printhex4
	popw %bp
	retw $4
END(printhex8)

/* ------------------------------------------------------------------------- *
 *  crlf
 *
 *  Print a newline
 * ------------------------------------------------------------------------- */

GPROC(crlf)
	printmsg "\r\n"
	retw
END(crlf)

#ifdef DEBUG

#ifndef DEBUG_PORT
# define DEBUG_PORT 0xe9
#endif
	
	.section ".rtext","ax"

/* ------------------------------------------------------------------------- *
 *  debug_puts
 *
 *  Print a message (pointer on the stack) on the debug port
 * ------------------------------------------------------------------------- */

GPROC(debug_puts)
	pushw %bp
	movw %sp,%bp
	pushw %si
	pushw %ax
	movw 4(%bp),%si
1:
	lodsb
	andb %al,%al
	jz 2f
	outb %al,$DEBUG_PORT
	jmp 1b
2:
	popw %ax
	popw %si
	popw %bp
	retw $2
END(debug_puts)

/* ------------------------------------------------------------------------- *
 *  debug_printnum
 *
 *  Print an unsigned 32-bit integer on the stack
 * ------------------------------------------------------------------------- */

GPROC(debug_printnum)
	pushl %eax
	pushl %edx
	pushl %ecx
	pushw %di
	movw %sp,%di
	movl (2+1*2+3*4)(%di),%eax
	subw $12,%sp
	decw %di
	xorl %ecx,%ecx
	movb %cl,(%di)
	movb $10,%cl
1:
	xorl %edx,%edx
	divl %ecx
	decw %di
	addb $0x30,%dl
	movb %dl,(%di)
	andl %eax,%eax
	jnz 1b

	pushw %di
	call debug_puts

	addw $12,%sp
	popw %di
	popl %ecx
	popl %edx
	popl %eax
	retw $4
END(debug_printnum)

/* ------------------------------------------------------------------------- *
 *  debug_printhex4
 *
 *  Print an hexadecimal 4-digit number from the stack
 * ------------------------------------------------------------------------- */

GPROC(debug_printhex4)
	pushw %ax
	pushw %cx
	pushw %dx
	pushw %di
	cld
	subw $6,%sp
	movw %sp,%di
	movw (2+4*2+6)(%di),%dx
	movw $4,%cx
1:
	rolw $4,%dx
	movb %dl,%al
	andb $0x0f,%al
	addb $0x30,%al
	cmpb $0x3a,%al
	jb 2f
	addb $(0x41-0x3a),%al
2:
	stosb
	loopw 1b
	movb %cl,(%di)		/* %cl == 0 */
	pushw %sp		/* %sp -> address of buffer */
	call debug_puts

	addw $6,%sp
	popw %di
	popw %dx
	popw %cx
	popw %ax
	retw $2
END(debug_printhex4)

/* ------------------------------------------------------------------------- *
 *  debug_printhex8
 *
 *  Print an hexadecimal 8-digit number from the stack
 * ------------------------------------------------------------------------- */

GPROC(debug_printhex8)
	pushw %bp
	movw %sp,%bp
	pushw 6(%bp)
	call debug_printhex4
	pushw 4(%bp)
	call debug_printhex4
	popw %bp
	retw $4
END(debug_printhex8)

/* ------------------------------------------------------------------------- *
 *  debug_crlf
 *
 *  Print a newline
 * ------------------------------------------------------------------------- */

GPROC(debug_crlf)
	debugmsg "\r\n"
	retw
END(debug_crlf)

#endif /* DEBUG */