[cc65] Other uses of BRK, stack snooping

Date view Thread view Subject view

From: David Holz (davidh_at_otterspace.com)
Date: 2002-08-13 01:39:13


Hi, I had an idea.  I don't know if you guys have thought of it before, but
using BRK followed by data bytes is a great way to save space in user code.
It mimics system calls in larger CPUs (kinda like INT in x86 world).  Say
that we want to push a 16-bit literal to the stack:

lda #lo
ldx #hi
jsr pushax

That's 7 bytes.  If system call $00 was "push immediate 16-bit to stack",
the compiler could generate

brk
.byte $00
.word $hilo

which is only 4 bytes, almost half the size.  The BRK handler would snoop
the $0100 stack for the return address, dispatch on the next byte, and
adjust the return address to return to the next instruction.  Obviously,
JSRing straight into the utility code with preloaded registers is faster, so
it definitely would fall into a speed-vs-memory tradeoff option, and
practical only when calling built-in routines with compile-time-known
parameters.

I think Lunix or the C128 kernal or somebody did this kind of trick with
outputting strings, which is where I got the idea from:

jsr stroutThingy
.word message
...
message: .text "I'm a little teapot"

I see that there's support for setting up the BRK vector from C now; do you
think that it's used much, because it'd need a bit of work to make it
interoperate with this (something like making 'brk $ff' the user BRK
handler).

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2002-08-13 01:29:55 CEST