Wednesday, 24 October 2012

More Numbers

We're gradually decyphering the instructions in our world's smallest PIC program and turning numbers into names to make the program more readable. So far we know that register 7 is called PORTC and register 3 is called STATUS. We've also seen that register 87 is called TRISC but that 87 doesn't mean 87 decimal but 87 hex (87h).

Well you probably won't be surprised to hear that the instruction Bit Set File (bsf) is also just a name representing a number. Take another look at the PIC16F690 INSTRUCTION SET and you'll see that the  instruction bsf 7,0 is fully represented by a 14 bit opcode. The least significant 7 bits define the register (or file) and are shown as fffffff. The next 3 bits define which bit of the register is being tested or altered. These are shown as bbb. But the most significant 4 bits (the left hand 4 bits) are shown as 0101. So 'bsf' is just a symbol representing the code 0101.

Similarly the complete instruction 'bsf 7,0' is just a more readable version of the instruction 0101 000 0000111. I've split this binary number into three groups of bits representing the instruction, the bit and the file. Note that in the readable version, the file number comes first and the bit number second. In the binary version it's the other way around.

When the assembly language program is turned into machine code and transferred into the PICs program memory, it's in a pure binary format. So inside the PIC, the world's smallest program looks like this:


What about 'end', the 4th line of our program. There's no 'end' instruction in the PIC16F690 INSTRUCTION SET.

That's because 'end' isn't an instruction - it's an assembler directive. The assembler doesn't convert it into machine code. The programmer doesn't program it into the microcontroller. It just tells the assembler to stop assembling. I wouldn't have included it if I didn't have to, but without it the assembler generates an error and won't produce any object code.

No comments:

Post a Comment