Re: [cc65] Other uses of BRK, stack snooping

Date view Thread view Subject view

From: groepaz (groepaz_at_gmx.net)
Date: 2002-08-13 14:45:31


Hello David,

Tuesday, August 13, 2002, 1:39:13 AM, you wrote:

DH> Hi, I had an idea.  I don't know if you guys have thought of it before, but
DH> using BRK followed by data bytes is a great way to save space in user code.

thought about this before aswell, guess others also did :o) to make it
straigt, most ppl i know came to the conclusion that its not worth the
trouble for little benefit.

DH> It mimics system calls in larger CPUs (kinda like INT in x86 world).

ehrm *cough* ... BRK _is_ a "systemcall" (aka software interupt). its
just one of the least known features of the cpu :)

DH> Say that we want to push a 16-bit literal to the stack:

DH> lda #lo
DH> ldx #hi
DH> jsr pushax

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

DH> brk
DH> .byte $00
DH> .word $hilo

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

yep, that might actually look useful.... BUT, now think of the fact
that not all calls can be done this way (like you said) and still
these calls must call routines that must be callable with standard
calling-conventions (preloaded registers and jsr).

in other words, we would end up with 2 kinds of totally different
calling conventions which would make code-generation tricky, and
writing libraries very messy (each common function would need 2
entrypoints, one for each calling-method - resulting in massive
codesize bloat ;=P)

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

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

this is/was a very common technique in 8bit-asm actually, not THAT
common in terms of c64 though :o)

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

i've personally never used BRK at all - except in debug-handlers and
in that case one cannot rely on the byte after BRK anyway.

-- 
Best regards,
 groepaz                            mailto:groepaz_at_gmx.net


----------------------------------------------------------------------
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 14:46:06 CEST