johnnystarr wrote:Code: Select all
; using asm6
ControlFlow:
lda mem
cmp #1
bne @sub_one
cmp #2
bne @sub_two
@sub_one:
; sub routine one goes here
jmp @done ; <-------------- without this jmp, @sub_two will execute
@sub_two:
; sub routine two goes here
@done:
rts
Personally, I would love a switch statement or some other control flow structure. The JMP above concerns me as well. It seems like there is a better way to handle multiple cases without this type of spaghetti code.
I understand the code was just an example, but I think the naming of labels is very confusing. One would expect sub_one to be executed when A is 1, and sub_two when A is 2, but that doesn't happen.
The closest you're going to get to switch statement with 6502 is jump tables. They're explained in the wiki:
http://wiki.nesdev.com/w/index.php/Jump_Table
Anyway, personally I tend to use these kind of structures and indentation. It's just a different take:
Code: Select all
; For CA65.
.proc foo
lda mem
cmp #1
bne not_sub_one
; sub1 stuff goes here.
jmp out ; Could be RTS. Pros: Less code, faster. Cons: Less maintainable (in case clean up code gets added after the "out" label later).
not_sub_one:
cmp #2
bne not_sub_two
; sub2 stuff goes here.
; Since it's the last condition, we don't need jmp, but let's use an assert to make sure
; we don't get in trouble if the code gets modified later.
.assert * = out, error
not_sub_two:
out:
rts
.endproc
Problem with this is that the code must fit within a page (because of the branches), or you'll have to start substituting the bne with beq+jmp for long branches.
P.S. How hard would it be to bolt a 6502 syntax hilighter in to phpBB? Maybe a different tag so it doesn't mess with other code. JavaScript hilighter such as SyntaxHilighter should be easy enough to add, and almost zero maintenance.