The Let's Play Archive

SHENZHEN I/O

by Quackles

Part 7: Engineer's Corner #2: MC Math 'n' More!

Engineer's Corner 2: MC Math 'n' More!

Sharp-eyed readers might have noticed that when I used mul to multiply the input value in the last post, I never specified either a destination or the other half of the equation. This is because microcontroller math works a little bit differently than doing it on your phone.

The basic rule is this: Every math instruction uses the acc register as the source, and also the place to store the result.



From one point of view, this is easy, because you don't have to worry about where you're getting one of your numbers from. From another, this can be really annoying - you have to move your value from the accumulator (acc) to wherever you wanted to use it (like p1 for output, in the last job I had to do), and that's another instruction.

Also, if you're using the accumulator as, say, a counter, and you want to do math in it, you have to do something with the value you *had* in acc so that you can do your math. You can't do math and leave acc untouched.

The other thing about MC math is that you can't divide. No, really. Here's the list of all the arithmetic instructions in the spec, with a few arithmetic-adjacent ones thrown in for good measure:


add - Add this number to the accumulator, and store the result there.
    add number
sub - Subtract this number from the accumulator, and store the result there.
    sub number
mul - Multiply this number by the number in the accumulator, and store the result there.
    mul number

No, there is no divide instruction. Dividing is a lot harder than multiplying, adding, or subtracting, computationally. So these low-level MCs don't have the option.

not - If the number in the accumulator is not 0, set it to 0. If it is already 0, set it to 100.
    not  

not is useful for logic stuff, mostly. But it does only affect the accumulator, so here it is in this list.

dgt - Get the nth digit of the number in acc and store that digit in acc. The ones digit is digit #0, the tens digit is digit #1, and the hundreds digit is digit #2.
    dgt target_digit
dst - Set the nth digit of acc to the number provided. Same positioning scheme as dgt.
    dgt target_digit new_number

dgt and dst strike me as the type of instructions a clever programmer could use in efficiency-related hacks. It's a far cry from the cool things you could do with bit-shifting instructions on the binary microcontrollers I learned on back in college, but decimalized MCs appear to be all the rage in Shenzhen, so that's what I'm using.


That's all of the math(-like) instructions. I think I've explained, like, half of the MC4000 instruction set to all of you already...!
Of course, it's not the size of the language, but what you do with it that matters.







...you know, MC Math would be a good name for a DJ, come to think of it.