CS 111 – Sept. 16 Machine language examples Instruction execution Don’t memorize… Instruction execution Closer look at operations in instruction set Commitment: Please read sections 2.5 and 2.6. Quiz next Wednesday.
More instructions Opcode 1 is for loading a memory value into a register Expects a register operand (4 bits), and a memory address from which to load (8 bits). Ex. 1820 means to go out to memory at address [20], grab the contents and load it into register 8. (It does not mean put the number 20 in register 8.) Opcode 3 is a store = opposite of load Ex. 3921 means to take the value in register 9, and put it into memory at location [21]. (It does not mean put the number 9 into memory location 21.) Opcode C (hex code for 12) is for telling CPU it’s done. Expects operand to be 12 zero-bits.
Some practice Refer to appendix C… How would we put the number 64 into memory at address 12? How would we add the numbers 6 and 8 and put the result in register 1? How would we add register 7 to register 5 and put the answer in memory at address 32? More examples: p. 91
Execution In our example, each instruction is 2 bytes long. Program counter (PC) begins at address of first instruction. For each instruction: Fetch (and increment PC by 2) Decode Execute Examples pp. 98-99 Note that RAM contains both instructions and data, separated from each other. For example, addresses 0-99 could be reserved for code.
Logic operations Work just like gates, but we do several bits in parallel. Examples 10101110 01101011 AND 11110000 AND 00011111 Try the same examples with “OR” and “XOR” Observations: What happens when you AND with a 1? With a 0? What about OR’ing with a 1 versus a 0? What about XOR?
Shift operations Given a bit pattern like 00011100, we can shift the bits left to obtain: 00111000. If we shift to the right instead, 00011100 becomes this: 00001110. We can even shift by more than one position. Shifting 01010000 by 3 bits right 00001010. Sometimes when we shift, 1’s fall off the edge. Shifting 01010000 by 2 bits left 01000000. When we shift, the “vacated” bits are usually 0.
Why shift? One application of a shift operation is to: Multiply by 2: left shift Divide by 2: right shift Try some examples – should look familiar with our earlier work on binary numbers. One funny exception: dividing a (signed) negative number by 2. In this case, we want the vacated bit to be 1 Example: –12 in signed is 11110100. If we shift right by 1, we get 01111010, but it should be this: 11111010.
Rotate Rotate operations work the same as shift… except that the vacated bits come from the other end of the number. So, instead of 1’s falling off the edge, they rotate. For example, 01010000 rotated left by 2 becomes 01000001. Also: 00001111 rotated right by 3 becomes: 11100001. Examples pp. 103-104.