Subroutine parameters
Re: Subroutine parameters
Downside of mixing code and data is that branches get out of range quicker. For big chunks of data, you could place them somewhere else, and then just use a pointer word after the JSR to get to it.
Re: Subroutine parameters
Could you use a macro to declare all the strings inline for readability but put their actual declaration somewhere else like a compiler does? Something like this:
After expansion:
Code: Select all
Print .macro msg
???
.endm
Print "Hello!"
Print "Test"Code: Select all
LDA #<str1
LDX #>str1
JSR print_sub
LDA #<str2
LDX #>str2
JSR print_sub
...
str1: .DB "Hello!"
str2: .DB "Test"Re: Subroutine parameters
Druzyek wrote:
Could you use a macro to declare all the strings inline for readability but put their actual declaration somewhere else like a compiler does? Something like this:
After expansion:
Code: Select all
Print .macro msg
???
.endm
Print "Hello!"
Print "Test"Code: Select all
LDA #<str1
LDX #>str1
JSR print_sub
LDA #<str2
LDX #>str2
JSR print_sub
...
str1: .DB "Hello!"
str2: .DB "Test"Code: Select all
.code
ldx #>msg1
ldy #<msg1
jsr strout
.data
msg1: .byte "Hello, world",13,10,0
.code ; Back to the code segment
... more 6502 program code here
I've had a look at the macro features of ca65 and I'm not sure it can do that though - however if you were clever with something like m4 (that runs under Unix/Linux, etc.) then it's possible - you effectively divert the generated output (ie. the string data) into a temporary file as part of your PRINT macro, then you need an ending macro that pulls the diverted text into the main document at the end. It should be possible to write a macro that uses the segments though - let me check... I don't think so.
It looks like you can change segments - that's fine, but can't change segments inside a macro - at least not with ca65
This is my test file:
Code: Select all
strout = $F000 ; Faking it
.org $1000
;
; .macro print string
; .code
; ldx #>:+
; ldy #<:+
; jsr strout
; .data
;:
; .asciiz string
; .code
; .endmacro
;
;
; print "Hello, world"
; print "Another test"
;
;
.code
ldx #>:+
ldy #<:+
jsr strout
.data
:
.asciiz "Hello World"
.code
ldx #>:+
ldy #<:+
jsr strout
.data
:
.asciiz "Another Test"
Code: Select all
ca65 -g --cpu 65c02 -l test2.lst test2.s
ld65 -t none -S 0x1000 -vm -m test2.map -o test2 test2.o
So have a look at the .lst and .map files and comment out the bottom and un-comment the top to experiment.
.... but before I hit submit, I did a few more tests and it looks like you CAN use the macros, however only if you keep it all relocatable until the link state, so no .ORG in the source file, but keep that -S 0x1000 (or whatever) in the link command.
So using the macros, assembling, linking to $1000 (the data segment follows on), the hex output file looks like:
Code: Select all
000000 a2 10 a0 0e 20 00 f0 a2 10 a0 1b 20 00 f0 48 65 >.... ...... ..He<
000010 6c 6c 6f 2c 20 77 6f 72 6c 64 00 41 6e 6f 74 68 >llo, world.Anoth<
000020 65 72 20 74 65 73 74 00 >er test.<
Code: Select all
Name Start End Size Align
CODE 001000 00100D 00000E 00001
DATA 00100E 001027 00001A 00001
Cheers,
-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
Re: Subroutine parameters
Arlet wrote:
Downside of mixing code and data is that branches get out of range quicker. For big chunks of data, you could place them somewhere else, and then just use a pointer word after the JSR to get to it.
Consider:
Code: Select all
JSR add
db $1234
db $4567
Code: Select all
LDA #<data
LDX #>data
JSR add
...
data:
db $1234
db $4567
Now, obviously the routine is going to chew up some memory to pull the parameters apart, etc.
But given enough calls, there could be a notable decrease in code size at the cost of some performance (as always...).
-
White Flame
- Posts: 704
- Joined: 24 Jul 2012
Re: Subroutine parameters
ca65 macros can absolutely generate segment-hopping output, using .pushseg, .segment "foo", .popseg. I use that heavily in AcheronVM and some older projects to generate data tables and even documentation.
Re: Subroutine parameters
White Flame wrote:
ca65 macros can absolutely generate segment-hopping output, using .pushseg, .segment "foo", .popseg. I use that heavily in AcheronVM and some older projects to generate data tables and even documentation.
However the one thing I found when playing with the stuff above is that it didn't seem to work when you'd specified a .org in the source file and I'm not sure if this is a bug/feature of ca65 or by design.
But now this is opening up some ideas for me...
Cheers,
-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/