Hey Everyone!
This is my first post here, hopefully this style of question is ok, if not I can post elsewhere or change it a bit.
I've been working through Rodney Zak's book Programming the 6502, and attempting the questions within in. One question asks the reader to write some code to find the largest value within a block.
I have written something I believe works, but as I am relatively new to writing low level languages, I was hoping for some friendly, but constructive, feedback.
The sections lowList and highList are mean to create fill a section of memory with the values: [5,4,3,2,1,15,14,13,12,11]. Then startSearch will move a part of this to $0220 (where I want to store the highest value) to give me something to compare too. compare and foundHigh then loop through the values I have stored in memory while foundHigh changes $0220 when appropriate.
Code:
LDX #05
LDY #00
lowList: ; deal with 5 to 1
TXA
INY
STA $0205, Y
DEX
BNE lowList
LDX #15
highList: ; deal with 15 to 11
TXA
INY
STA $0205, Y
DEX
CPX #10
BEQ startSearch
JMP highList
startSearch:
LDA $0205, Y ;this should be the largest num
STA $0220 ; where we'll store highest number
DEY
compare:
LDA $0205, Y
CMP $0220
BCS foundHigh ; if A>$0220, jump to foundHigh
DEY
BNE END ; check to see if we've gone through whole list
JMP compare
foundHigh:
STA $0220 ;update $0220 with new largest value
DEY
JMP compare ;back to compare loop
END:
RTS
I've been using
https://skilldrick.github.io/easy6502/ to test it out, and it looks like it works!
I'm still getting to grips with the language and trying to find the best instructions to use when, and just get a feel for the style you should be writing in. Any feedback, tips, or thoughts would be really appreciated!