I use mostly Java and C# for my day job and am familiar with intermediate level C, which until is the lowest I've gone.
As a gamer and hobbyist game dev (unity engine), I really want to reach a point where I can make games and other applications for the C16. I started learning assembly using matt H's videos on youtube. I'm currently on video 2.
I wanted to give myself a little bit of an assignment based on some of the things I learned in his videos. I wrote a small program to copy an array of chars ("hello!!!") from one memory location to another, and then reverse the string.
Code: Select all
.org $080D
.segment "STARTUP"
.segment "INIT"
.segment "ONCE"
.segment "CODE"
SRC_PTR = $30
DEST_PTR = $32
TEMP1 = $34
TEMP2 = $36
FIRST = $38
LAST = $40
jmp start
source:
.byte $48, $65, $6C, $6C, $6F, $21, $21, $21
destination:
.byte 0,0,0,0,0,0,0,0
start:
ldy #0
lda #<source
sta SRC_PTR
lda #>source
sta SRC_PTR + 1
lda #<destination
sta DEST_PTR
lda #>destination
sta DEST_PTR + 1
loop:
lda (SRC_PTR), Y
sta (DEST_PTR), Y
iny
cpy #8
bne loop
jmp do_reverse
do_nothing:
rts ;
do_reverse:
ldx #0
ldy #7
reverse_loop:
stx FIRST
sty LAST
lda (DEST_PTR), Y
sta TEMP2
ldy FIRST
lda (DEST_PTR), Y
sta TEMP1
; each value is temporarily stored, now we can switch
lda TEMP2
sta (DEST_PTR), Y
ldy LAST
lda TEMP1
sta (DEST_PTR), Y
;both values have been switched, set up next run
tya
dec
tay
txa
inc
tax
cmp #4
bne reverse_loop
rts
Overall, if I look at this program, there are some dead giveaways that the person who wrote it comes from a higher level language. I feel like some of the writes to memory that I made could have been things that could have been juggled around in the registers without needing to persist to memory. I noodled on this for hours and couldn't figure out the best way to move forward.
I might actually try and see if I can re-write this to not use pointers and open myself up to indexing with X and Y registers and see how far I get, but honestly I am not sure. Matt's videos are great but I feel like this is going to be trickier to learn than anything I've learned before.
Any advice? Can one of you veterans show me the proper, idiomatic way to do something like this?