Transcript sept15
CS 111 – Sept. 15
• Chapter 2 – Manipulating data by performing
instructions
• “What is going on in the CPU?”
• Commitment:
– Please read through section 2.3
– Meet here tomorrow.
Basic anatomy
• Inside a computer are 2 parts
– CPU
– Memory
– These are connected by a data bus: an “HOV lane” where traffic
can go either way.
• CPU contains:
– ALU: arithmetic and logic unit
– Control unit: figures out what to do next
– Registers to hold values needed for calculation
• Memory (RAM) contains:
– Software: list of instructions the CPU needs to perform
– Data: Input and output values need to be stored while program
runs
Stored program idea
• Program = software = list of instructions for CPU to do
• Programs reside in memory
• CPU will do 1 instruction at a time
• For each instruction, we do the following:
–
–
–
–
Fetch it from memory
Decode – figure out what it means
Execute – do it
And then we continue with the next instruction… until the
program is finished.
Simple example
• A program to add two numbers.
• This program may reside at bytes 100-116 in RAM.
• The two numbers we wish to add are located at bytes
200 and 204 in RAM.
• We want the result to go into memory at byte 208.
• Program may go something like this:
–
–
–
–
Load the value at Memory[200] into register 1.
Load the value at Memory[204] into register 2.
Add registers 1 and 2, and put result in register 3.
Store the value from register 3 into Memory[208].
• Note that the bus is communicating instructions (RAM to
CPU) as well as data (both ways).
Machine language
• Unfortunately, instructions for CPU can’t be in English,
French, etc.
• Machine language = binary (or hex) representation of
our instructions.
– Each type of computer has its own machine
language.
• This is the oldest form of “computer programming”.
Later we’ll look at much more intuitive ways to convey
instructions.
• Verbs: Instruction set. e.g. Add, subtract, load, store…
• Nouns: Operands such as: registers, memory locations,
constants, other instructions
Verbs
3 kinds of instructions (instruction set)
• Data transfer, using the bus
– Load a value from memory into a CPU register
Very similar to fetching an instruction!
– Store a value from a CPU register into memory
• ALU
– Bit manipulation: AND, OR, XOR, NOT, shift left, shift right, …
– Arithmetic: add, sub, mul, div, remainder, =, <, >, , , ≠, …
• Control
– “Go to” another instruction in program. In other words, interrupt
normal sequence of instructions.
– Can be conditional or unconditional
Example language
• Our book illustrates with an example HW.
• 256 bytes of RAM: addressable by 8 bits
• CPU contains
– Instruction register
– Program counter
– 16 general purpose registers: addressable by 4 bits
• Each register is 1 byte
• Each instruction is 2 bytes = 16 bits = 4 hex digits long
• Instruction format:
– First 4 bits are the opcode = specify which instruction type
– Other 12 bits are operand(s)
• What do instructions mean? See pp. 602-603.
Example instructions
• Note: 16 possible opcodes: 4 bit opcode
• Note: 16 possible registers: register number also 4 bits
• Opcode 5 is used for adding
– Expects 3 register operands
– 5RST means R = S + T, where R, S and T are register numbers
– Ex. 5123 means
Add registers 2 and 3 and put result in register 1.
• Opcode 2 is for putting a constant in a register
– Expects a register operand, and an 8-bit constant operand
– 2RXX means R = XX, where XX is some 8-bit pattern
– Ex. 27c9 means
Put the hexidecimal “c9” into register 7.
• Try an example using both types of instructions.
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.