- jsr GetFormatChar ; Get one char, zero in Y
- tax ; End of format string reached?
- bne NotDone ; Continue of end not reached
+ lda Format ; Remember current format pointer
+ sta FSave
+ lda Format+1
+ sta FSave+1
+
+ ldy #0 ; Index
+@L1: lda (Format),y ; Get next char
+ beq @L2 ; Jump on end of string
+ cmp #'%' ; Format spec?
+ beq @L2
+ iny ; Bump pointer
+ bne @L1
+ inc Format+1 ; Bump high byte of pointer
+ bne @L1 ; Branch always
+
+; Found a '%' character or end of string. Update the Format pointer so it is
+; current (points to this character).
+
+@L2: tya ; Low byte of offset
+ add Format
+ sta Format
+ bcc @L3
+ inc Format+1
+
+; Calculate, how many characters must be output. Beware: This number may
+; be zero. A still contains the low byte of the pointer.
+
+@L3: sub FSave
+ sta FCount
+ lda Format+1
+ sbc FSave+1
+ sta FCount+1
+ ora FCount ; Is the result zero?
+ beq @L4 ; Jump if yes
+
+; Output the characters that we have until now. To make the call to out
+; faster, build the stack frame by hand (don't use pushax)
+
+ jsr decsp6 ; 3 args
+ ldy #5
+ lda OutData+1
+ sta (sp),y
+ dey
+ lda OutData
+ sta (sp),y
+ dey
+ lda FSave+1
+ sta (sp),y
+ dey
+ lda FSave
+ sta (sp),y
+ dey
+ lda FCount+1
+ sta (sp),y
+ dey
+ lda FCount
+ sta (sp),y
+ jsr CallOutFunc ; Call the output function
+
+; We're back from out(), or we didn't call it. Check for end of string.
+
+@L4: jsr GetFormatChar ; Get one char, zero in Y
+ tax ; End of format string reached?
+ bne NotDone ; End not reached