[ca65] Placing words at specific locations in segment

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
User avatar
and3rson
Posts: 163
Joined: 17 Feb 2023
Location: Lviv, Ukraine
Contact:

[ca65] Placing words at specific locations in segment

Post by and3rson »

I'm trying to improve the jumptable for my simple REPL environment.
Basically I'm mapping every ASCII character to some routine as such:

Code: Select all

.segment "JUMPTABLE"
CMD_JUMPTABLE:
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    .word cmd_noop, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, cmd_printmem
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, cmd_jmp, 0, 0, cmd_printmem, 0, 0
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

.code
  ; ...
  ; Register A contains ASCII character code
    asl a  ; jumptable addresses are word-sized
    tax
    jmp (CMD_JUMPTABLE, X)
This works, but I really want to get rid of all the zeroes and do something like this:

Code: Select all

.segment "JUMPTABLE"
.align $100
JUMPTABLE_START:
    .repeat 128
    .word $0
    .endrepeat
; The following lines are pseudocode.
JUMPTABLE_START + ' ' = .word cmd_noop
JUMPTABLE_START + 'j' = .word cmd_jmp
JUMPTABLE_START + 'm' = .word cmd_printmem
JUMPTABLE_START + '?' = .word cmd_printmem
Any thoughts on whether this is achievable with ca65?
/Andrew

deck65 - 6502 slab with screen and keyboard | ПК-88 - SBC based on KM1810VM88 (Ukrainian i8088 clone) | leo80 - simple Z80 SBC
nice65 - 6502 assembly linter | My parts, footprints & 3D models for KiCad/FreeCAD
tmr4
Posts: 147
Joined: 19 Feb 2022

Re: [ca65] Placing words at specific locations in segment

Post by tmr4 »

Try using a macro that takes the ascii character and jump address as parameters. Have the macro keep track of the current position (using the set command) and use the repeat control command to fill in the empty entries up to that point. So something like this:

Code: Select all

.macro jmptbl char addr
    .repeat char - JTcount - 1
       .word 0
    .endrep
    .word addr
    JTcount .set char
.endmacro
JUMPTABLE_START:
JTcount .set 0  ; initialize count
jmptbl ' ' cmd_noop
jmptbl 'j' cmd_jmp
jmptbl 'm' cmd_printmem
jmptbl '?' cmd_printmem
You might need to do some conversion in using the ascii character in expressions. I'm not sure if ca65 will treat it as a value.
User avatar
and3rson
Posts: 163
Joined: 17 Feb 2023
Location: Lviv, Ukraine
Contact:

Re: [ca65] Placing words at specific locations in segment

Post by and3rson »

tmr4 wrote:
Try using a macro that takes the ascii character and jump address as parameters. Have the macro keep track of the current position (using the set command) and use the repeat control command to fill in the empty entries up to that point.
This is cool! I assume the calls need to be sorted by "char" in ascending order, right?

Thanks for the tip, I'll try it out. I missed the .set command when I went through the documentation!
/Andrew

deck65 - 6502 slab with screen and keyboard | ПК-88 - SBC based on KM1810VM88 (Ukrainian i8088 clone) | leo80 - simple Z80 SBC
nice65 - 6502 assembly linter | My parts, footprints & 3D models for KiCad/FreeCAD
User avatar
and3rson
Posts: 163
Joined: 17 Feb 2023
Location: Lviv, Ukraine
Contact:

Re: [ca65] Placing words at specific locations in segment

Post by and3rson »

With a minor tweak it worked - thanks!

Code: Select all

.macro jmptbl char, addr
    .repeat char - jt_count  ; removed -1
        .word 0
    .endrep
    .word addr
    jt_count .set char + 1  ; added +1
.endmacro
/Andrew

deck65 - 6502 slab with screen and keyboard | ПК-88 - SBC based on KM1810VM88 (Ukrainian i8088 clone) | leo80 - simple Z80 SBC
nice65 - 6502 assembly linter | My parts, footprints & 3D models for KiCad/FreeCAD
tmr4
Posts: 147
Joined: 19 Feb 2022

Re: [ca65] Placing words at specific locations in segment

Post by tmr4 »

and3rson wrote:
I assume the calls need to be sorted by "char" in ascending order, right?
Yes, that's important.
and3rson wrote:
With a minor tweak it worked - thanks!
Ha, ha. Off by one has always been my bane!
Post Reply