I wrote a zero page allocator for Kick Assembler.

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
geon
Posts: 8
Joined: 14 Sep 2021
Contact:

I wrote a zero page allocator for Kick Assembler.

Post by geon »

https://github.com/geon/zpallocator/blo ... xample.asm

The idea is to be able to use the zero page for all variables. You allocate a zp address when you need it, and when you are done with it (ie. at the end of the macro/function scope), you deallocate it.

Code: Select all

	// BackgroundColor will contain a free zp address.
	.var backgroundColor = allocateZpByte()

	// Do stuff with backgroundColor
	lda #BROWN
	sta backgroundColor
	// ...
	deallocateZpByte(backgroundColor)
This way, I can write macros and pass arguments by reference without worrying about collisions.

I could use some help to define a couple of ranges of reserved zp addresses. I don't care about basic, so I can use $02-$70 or so without problem, but I might want to use some kernal routines like screen io and the jiffy clock. I'm not sure what other areas of the zp are important.
fachat
Posts: 1124
Joined: 05 Jul 2005
Location: near Heidelberg, Germany
Contact:

Re: I wrote a zero page allocator for Kick Assembler.

Post by fachat »

geon wrote:
https://github.com/geon/zpallocator/blob/master/zpallocator.example.asm

The idea is to be able to use the zero page for all variables. You allocate a zp address when you need it, and when you are done with it (ie. at the end of the macro/function scope), you deallocate it.
Not sure I understand. It looks like you're allocating at build time, right? How do you handle run time conflicts like one routine reusing the location reserved by another routine but calling each other?

André
Author of the GeckOS multitasking operating system, the usb65 stack, designer of the Micro-PET and many more 6502 content: http://6502.org/users/andre/
geon
Posts: 8
Joined: 14 Sep 2021
Contact:

Re: I wrote a zero page allocator for Kick Assembler.

Post by geon »

fachat wrote:
It looks like you're allocating at build time, right?
Yes.
fachat wrote:
How do you handle run time conflicts like one routine reusing the location reserved by another routine but calling each other?
For a function, you would need to allocate the address permanently. It works better with macros.
fachat
Posts: 1124
Joined: 05 Jul 2005
Location: near Heidelberg, Germany
Contact:

Re: I wrote a zero page allocator for Kick Assembler.

Post by fachat »

Then don't you use macros in functions?

Did you try to 'simulate' it e.g. with a paper walk through?

Writing up how it works for a few real world test cases helps a great deal in understanding if it really works or where the pitfalls are.
Author of the GeckOS multitasking operating system, the usb65 stack, designer of the Micro-PET and many more 6502 content: http://6502.org/users/andre/
geon
Posts: 8
Joined: 14 Sep 2021
Contact:

Re: I wrote a zero page allocator for Kick Assembler.

Post by geon »

fachat wrote:
Then don't you use macros in functions?
Actually no. Or rather, I haven't used functions at all so far, just macros.
fachat wrote:
Writing up how it works for a few real world test cases helps a great deal in understanding if it really works or where the pitfalls are.
Yes, I do TDD. I'll have to look at functions.
User avatar
BigDumbDinosaur
Posts: 9426
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Re: I wrote a zero page allocator for Kick Assembler.

Post by BigDumbDinosaur »

This sort of thing is easily done at run-time with the 65C816. Functions (subroutines) save the current value of DP (direct page pointer) on the stack, reserve stack space as needed and then point DP to that space. when the function exits it releases the stack space and restores DP to its entry value.
x86?  We ain't got no x86.  We don't NEED no stinking x86!
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: I wrote a zero page allocator for Kick Assembler.

Post by BigEd »

Well done geon, thanks for sharing your creation!
Post Reply