NU32v2: Digital I/O Assembly Code

From Mech
Revision as of 20:39, 24 January 2011 by Lynch (talk | contribs)
Jump to navigationJump to search

Let's say we called our source code digio.c. When we compile it, we find an "object code" file digio.o somewhere. I can "disassemble" this object code to see an assembly language version of the code. (You can also do this directly in the MPLAB IDE.) When we look at our disassembled code, we see a lot of stuff, including the block of code below. I added the comments beginning with //, since we don't know assembly language.

 354:  afc00010  sw     zero,16(s8)         // set i to zero

// start loop

 358:  8fc30010  lw     v1,16(s8)           // load word i (from memory location 16(s8)) into register v1
 35c:  3c020098  lui    v0,0x98             // load 0x98 into upper 16 bits of v0
 360:  3442967f  ori    v0,v0,0x967f        // load 0x967f into lower 16, to give v0 = 9,999,999 in base 10
 364:  0043102a  slt    v0,v0,v1            // if v0 < v1, sets v0 to 1, otherwise to 0
 368:  1440ffe4  bnez   v0,2fc <main+0x2fc> // if v0 is nonzero (i.e., 10 M <= i), exit "for" loop
 36c:  00000000  nop                        // no operation 
 370:  8fc20010  lw     v0,16(s8)           // load i into v0
 374:  24420001  addiu  v0,v0,1             // (unsigned integer) add 1 to v0
 378:  afc20010  sw     v0,16(s8)           // store v0 back to i
 37c:  1000fff6  b      358 <main+0x358>    // go back to start of loop, instruction 358
 380:  00000000  nop                        // no operation

The first number in each row is the address of the first byte of each instruction. The second number is the 32-bit instruction written in hexadecimal; the first several bits are for the instruction type, or "opcode," and the remaining bits are to specify registers, memory locations, values, etc., used by the instruction.

I see 10 or 11 instructions in the loop, just less than my estimated 12 instructions. Usually we have one assembly instruction per machine instruction (cycle), but sometimes an assembly instruction expands to more than one machine instruction. Is that what's happening here---there's an extra machine instruction we're not seeing? Or bad timing by me? Not sure.

Notice our use of the constant 10,000,000 in the code actually requires two instructions to load the value into register v0: one for the most significant half-word (16 bits), and one for the least significant half-word. We can't load an entire 32-bit constant in one cycle, due to the limit of 32 bits total to specify the instruction (and 6 of them are for the "opcode" or instruction type). Thus we could shorten our loop time by almost 10% if we simply created an int variable, stored the value 10,000,000 in it, and read the entire word into the register in one cycle. (This would cost us 4 bytes of RAM, of course.)

If you're interested to learn more about assembly instruction sets, you can start with http://en.wikipedia.org/wiki/MIPS_architecture, but we won't go into assembly in more detail.