Moving on with chapter 5 of “A Short Course In Programming“, I’m finding some more nifty opcodes to dissect. These versions are sort of the evil twins of the first three that I went over in part 1. Key in program 5.1 if you haven’t already.
ORI – The bytecode for ORI is F9. Let’s bring back the pair we’ve been using all along. F9, 33, 55:
33 = 0011 0011
55 = 0101 0101
F7 = 1111 0111
Woah! What’s the deal? This isn’t what we are used to at all. Let’s flip this thing backwards and see what happens:
55 = 0101 0101
33 = 0011 0011
D5 = 1101 0101
Hmmm, that didn’t help. Perhaps a different approach:
FF = 1111 1111
FF = 1111 1111
FF = 1111 1111
Well I suppose that’s a little better. The brute force learning method isn’t working for this one. Time to get to the bottom of this… Ok. Slowing the computer to single step through gives a bit of a clue to what is really happening here. The 33 is not being read at all. It’s entirely being ignored. When the program runs, you could put anything in for the second value and it won’t change the outcome because C4 is hard coded into the program in location 002A which is the byte immediately following 0029 where our opcode lives. When we write it out like this, it makes a ton more sense:
33 = 0011 0011
55 = 0101 0101
C4 = 1100 0100
F7 = 1111 0111
AHA! We are back to a plain old OR. This time however, instead of taking the input value from the 0061 location, the F9 opcode ignores the SEX 6 instruction and addresses the immediate byte instead. I’m sure this will come in handy in the future, I just don’t know how yet.
ANI – Ho hum, more of the same funny business here. Try FA, 33, 55:
33 = 0011 0011
55 = 0101 0101
C4 = 1100 0100
00 = 0000 0000
ANI is AND in disguise but also ignoring the E6 opcode and simply comparing the datum residing in the next byte of memory.
XRI – Do I even need to explain this one?
33 = 0011 0011
55 = 0101 0101
C4 = 1100 0100
F7 = 1111 0111
XRI is XOR in disguise but once again ignoring the second datum, 55. As a refresher, XOR changes all bits that are different to a 1 and all bits that are the same to a 0.
ADD – You probably think ADD is going to be super easy with no gotchas. You are mostly correct. There is only one little thing… To demonstrate this, I’ll change the numbers up a bit. The bytecode is F4 BTW:
FF = 1111 1111
01 = 0000 0001
00 = 0000 0000
DOH! We just blew it. If you have your “1802 State” window in TinyELF open however, you’ll see that the DF bit is now set high. This means that FF + 01 actually equals 0100. Let’s take one step back and check out something more palatable:
33 = 0011 0011
55 = 0101 0101
88 = 1000 1000
If you thought we were through chapter 5, think again. This is only about half way there. Oddly, we are also half way through the entire book at this point. It’s been a long journey, don’t quit now.