Category:6502 Assembly: Difference between revisions

no edit summary
No edit summary
Line 211:
 
===Know Your Opcodes===
Of course you should know what each instruction does, but it's also very handy on the 6502 to know what their hex values are. For example, ifas yourwell program'sas entryhow pointmany isbytes labeledthey andtake itsin firstmemory, instructionand istheir <code>SEI</code>execution ortime. <code>CLD</code>,For you can use <code>BIT labelname</code>example, wherelook <code>labelname</code> isat the labelsubroutine youbelow. gaveIn the program'scomments, entrythe point,byte aslength aof makeshiftthe "Setopcode Overflowis Flag" commandlisted. This isFor becausebrevity the hexmajority valuesof forthe thosesubroutine commandswill bothbe have bit 6 equal to 1omitted, andbut thusimagine cause <code>BIT</code> to setthat the overflowsubroutine flag.is Unfortunately,more likethan with128 mostbytes assemblylong optimizations,and this makes your code harder to read, so it'sthus a goodshort practicebranch to leave a comment explaining what thisis doesimpossible.
 
<lang 6502asm>loop:
lda $2000,x ;3 bytes
bne continue ;2 bytes
jmp end ;3 bytes
continue:
;imagine this is a very long subroutine where branching isn't possible
 
 
end:
rts ;1 byte</lang>
 
It would take fewer bytes to do this:
<lang 6502asm>loop:
lda $2000,x ;3 bytes
bne continue ;2 bytes
rts ;1 byte
continue:
;imagine this is a very long subroutine where branching isn't possible
 
 
end:
rts ;1 byte</lang>
 
Unfortunately, almost all optimizations in assembly languages are at the expense of readability (probably the biggest reason why assembly isn't used as much these days), and this one is no exception. When programming in 6502 in particular you will find yourself committing all the taboos of modern programming, such as <code>BREAK</code>, <code>GOTO</code>, and sometimes even the dreaded, forbidden, <code>SELF-MODIFYING CODE!</code>
 
==Citations==
1,489

edits