tiny_printf_6502

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
laubzega
Posts: 6
Joined: 07 Oct 2020

tiny_printf_6502

Post by laubzega »

Hi folks.

Working on a recent asm project I had the need for a flexible way of printing text strings. All the usual approaches made me impatient, so I put together a macro/function combination that basically makes it possible to use syntax close to C printf()'s. Some examples:

Code: Select all

	lda #42
	sta value
	printf "8-bit value: %02d at %04ld\n", value, &value
	rts
value:	.byte 0
Output: "8-bit value: 42 at 3163".

Code: Select all

printf "$%X = %d dec\n", value, value
Output: "$2A = 42 dec".

Code: Select all

	lda #<text
	sta ptr
	lda #>text
	sta ptr + 1
	printf "Pointer at $%04lx, pointing to string at $%04lx, which is \'%ps\'.\n", &ptr, ptr, ptr
	rts
text:	.byte "Hello, Underworld", 0
ptr:    .word 0
Output: "Pointer at $0ce3, pointing to string at $0cd1, which is 'Hello, Underworld'."

Code: Select all

	ldx #2
loop:	printf "Content of register X is $%02x\n", ^X
	dex
	bpl loop
Output:
"Content of register X is $02
Content of register X is $01
Content of register X is $00"

It's been immensely useful so far, and hugely accelerated my work, so I decided to release it at https://github.com/laubzega/tiny_printf_6502, where you will find more details.

Hopefully somebody has some use for it.


And since this is my first (I think) post here, let me introduce myself - I used to program 8-bit Ataris in the '80s as Thorgal/WFMH, mostly on the demoscene, although I built one or two utilities, too. A few years ago I purchased several 6502 computers to help me learn electrical engineering and that somehow reignited the old passion - although this time the C64 is my machine of choice. Last year, working with a friend we released the BeamRacer (http://beamracer.net) and are now working on follow up products.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: tiny_printf_6502

Post by BigEd »

Welcome! Nice idea, and thanks for sharing your code.
teamtempest
Posts: 443
Joined: 08 Nov 2009
Location: Minnesota
Contact:

Re: tiny_printf_6502

Post by teamtempest »

Very cool!
pzembrod
Posts: 22
Joined: 05 Sep 2020
Contact:

Re: tiny_printf_6502

Post by pzembrod »

This is very cool indeed!
And funnily enough, without having seen your post or project, I have recently done something similar, a printf() for my Small C compiler:
https://github.com/pzembrod/cc64/blob/m ... e/printf.a
Post Reply