-
Notifications
You must be signed in to change notification settings - Fork 3
/
printf.s
284 lines (269 loc) · 2.76 KB
/
printf.s
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
/ C library -- printf
.globl _printf
/.globl pfloat
/.globl pscien
.globl _putchar
.globl csv
.globl cret
_printf:
jsr r5,csv
sub $126.,sp
mov 4(r5),formp / format
mov r5,r4
add $6,r4 / arglist
loop:
movb *formp,r0
beq 1f
inc formp
cmp r0,$'%
beq 2f
3:
mov r0,(sp)
jsr pc,*$_putchar
br loop
1:
jmp cret
2:
/ clr rjust
clr ndigit
/ cmpb *formp,$'-
/ bne 2f
/ inc formp
/ inc rjust
2:
jsr r3,gnum
mov r1,width
/ clr ndfnd
/ cmp r0,$'.
/ bne 1f
/ jsr r3,gnum
/ mov r1,ndigit
1:
mov sp,r3
add $4,r3
mov $swtab,r1
1:
mov (r1)+,r2
beq 3b
cmp r0,(r1)+
bne 1b
jmp (r2)
.data
swtab:
decimal; 'd
/ octal; 'o
/ hex; 'x
/ float; 'f
/ scien; 'e
charac; 'c
string; 's
longorunsg; 'l
/ unsigned; 'u
/ remote; 'r
0; 0
.text
decimal:
mov (r4)+,r1
bge 1f
neg r1
movb $'-,(r3)+
br 1f
longorunsg:
cmpb *formp,$'d
bne unsigned
inc formp
br long
unsigned:
mov (r4)+,r1
1:
jsr pc,1f
br prbuf
1:
clr r0
div $10.,r0
mov r1,-(sp)
mov r0,r1
beq 1f
jsr pc,1b
1:
mov (sp)+,r0
add $'0,r0
movb r0,(r3)+
rts pc
long:
mov (r4)+,r2
mov (r4)+,r0
mov r4,-(sp)
mov r3,r4
mov r0,r3
tst r2
bpl 1f
neg r2
neg r3
sbc r2
movb $'-,(r4)+
1:
jsr pc,1f
mov r4,r3
mov (sp)+,r4
br prbuf
1:
clr r1
mov $32.,r0
3:
ashc $1,r2
rol r1
cmp r1,$10.
blo 2f
sub $10.,r1
inc r3
2:
sob r0,3b
add $'0,r1
mov r1,-(sp)
ashc $0,r2
beq 1f
jsr pc,1b
1:
movb (sp)+,(r4)+
rts pc
charac:
movb (r4)+,(r3)+
bne 1f
dec r3
1:
movb (r4)+,(r3)+
bne prbuf
dec r3
br prbuf
string:
mov ndigit,r1
clr r3
mov (r4),r2
1:
tstb (r2)+
beq 1f
inc r3
sob r1,1b
1:
mov (r4)+,r2
br prstr
/hex:
/ mov $1f,r2
/ .data
/1:
/ -4; !17
/ .text
/ br 2f
/
/octal:
/ mov $1f,r2
/ .data
/1:
/ -3; !7
/ .text
/2:
/ mov (r4)+,r1
/ beq 2f
/ tst ndigit
/ beq 2f
/ movb $'0,(r3)+
/2:
/ clr r0
/ jsr pc,1f
/ br prbuf
/1:
/ mov r1,-(sp)
/ ashc (r2),r0
/ beq 1f
/ jsr pc,1b
/1:
/ mov (sp)+,r0
/ bic 2(r2),r0
/ add $'0,r0
/ cmp r0,$'9
/ ble 1f
/ add $'A-'0-10.,r0
/1:
/ movb r0,(r3)+
/ rts pc
/
/float:
/ mov ndigit,r0
/ mov ndfnd,r2
/ jsr pc,pfloat
/ br prbuf
/
/scien:
/ mov ndigit,r0
/ mov ndfnd,r2
/ jsr pc,pscien
/ br prbuf
/
/remote:
/ mov (r4)+,r4
/ mov (r4)+,formp
/ jmp loop
/
prbuf:
mov sp,r2
add $4,r2
sub r2,r3
prstr:
mov r4,-(sp)
mov $' ,-(sp)
mov r3,r4
neg r3
add width,r3
ble 1f
/ tst rjust
/ bne 1f
2:
jsr pc,*$_putchar
sob r3,2b
1:
tst r4
beq 2f
1:
movb (r2)+,(sp)
jsr pc,*$_putchar
sob r4,1b
2:
tst r3
ble 1f
mov $' ,(sp)
2:
jsr pc,*$_putchar
sob r3,2b
1:
tst (sp)+
mov (sp)+,r4
jmp loop
gnum:
/ clr ndfnd
clr r1
1:
movb *formp,r0
inc formp
sub $'0,r0
cmp r0,$'*-'0
bne 2f
mov (r4)+,r0
br 3f
2:
cmp r0,$9.
bhi 1f
3:
/ inc ndfnd
mul $10.,r1
add r0,r1
br 1b
1:
add $'0,r0
rts r3
.bss
width: .=.+2
formp: .=.+2
/rjust: .=.+2
/ndfnd: .=.+2
ndigit: .=.+2