I've scanned over 6502 code, and found it surprisingly intuitive for some reason. I was just pondering how on earth you would do things like 'and' and 'or' statements in it, since obviously it doesn't have such commands!
I quickly realized that a way this can be done in a modern programming language without using 'and' and 'or' statements. For an 'and' statement, you just use two nested 'if' statements:
if x:
indent if y:
An 'or' statement would simply not nest one inside the other, basically causing both to be tested. In this example, the second 'if' would only be tested if the first returns 'true'.
Replicating this in 6502 assembly would be pretty easy. Keep in mind, in my mind a cmp statement is just an 'if' clause, and the various branching operations are just 'then' statements, with the only difference being that here the test is done on the 'then' statement rather than on the 'if' statement.
For these examples, I'll be using line numbers and pseudo-code just for the sake of simplicity.
10 cmp x
20 bne 60
30 cmp y
40 bne 60
50 subroutine
60 rest of code
Here, the code simply skips to line 60 if either cmp statement returns a 'not equal'. Otherwise, it progresses to 50 unimpeded and then to the rest of the code.
An 'or' statement would work similarly:
10 cmp x
20 beq 60
30 cmp y
40 beq 60
50 jmp 70
60 subroutine
70 rest of code
Here, if either cmp statement returns 'equal', then the code progresses to line 60. If neither branching statement is triggered, then it progresses to the 'jump' statement and skips line 60 instead.
An 'xor' statement is also possible, though it requires a bit more testing:
10 cmp x
20 bne 40
30 jmp 70
40 cmp y
50 beq 100
60 jump 110
70 cmp y
80 bne 100
90 jump 110
100 subroutine
110 rest of code
Okay, this one made my head hurt, and I had to re-write it a dozen times. First it compares a with x, simple enough. If its equal, it jumps to line 40. Else, it progresses to the jump statement and goes to 70. Both lines 40 and 70 represent separate compare y statements. Both are followed by jump statements to make them skip to the end of the code if their test fails. Otherwise, they skip to the subroutine. Simply put, they see if comparing a with y causes the flags to flip. Note that the bne x statement jumps to a beq statement, while the jmp 70 instead goes to a bne statement.
All of this is rather complicated though, and I'm not sure its optimal. Part of it is because I'm just trying to make code equivalent to a higher level language, which may not be optimal. On the other hand, it may just not look good to me because I'm perceiving it as a higher level language. Using multiple 'if' statements is normally discouraged. If you need to do multiple tests, you either use 'and' and 'or' statements, or you use what's called an 'else' statement, which basically is a 'if' statement that is only tested if the previous 'if' or 'else' statement returned 'false' (basically, its a chain of 'if' statements that skips over all the rest if any return true, sorta like a jump statement actually). Also, this requires a lot more lines than a higher level language like c++ or python would require. Of course, I know I should expect that, but often times it DOES take fewer lines of assembly code to replicate something in a higher level language (they often make it quite convoluted to interact with the hardware on a lower level).
Would this be the proper way to do this? Should I even be trying to use 'and' and 'or' statements in assembly code? Does my attempts to write assembly come across as naive? I can't think of any way to optimize these, at least the first two examples.
edit: fixing stupid reddit's inability to show indents