Lecture 2

Machine Language

Computer programs operate on data

Computer programs are data

von Neumann Architecture

Programs reside in the same memory as the data they operate on

Therefore possible to write programs that manipulate other programs. (operating system, compilers, viruses)

Q: How do we know which parts of memory represent code and which represent data?

A: We do not.

Q: What does a machine instruction look like? What instructions are there?

MIPS Machine

           CPU                    Main Memory
 ________________________         ___________
|  __   __    ___   ___  |       |___________|
| |PC| |IR|  |R00| |ALU| |       |___________| 
| |__| |__|  |R01| |___| |  BUS  |___________|
|  _______   |...| |MAR| |<----->|___________|
| |Control|  |R30| |___| |       |___________|
| |Unit   |  |R31| |MDR| |       |___________|
| |_______|  |___| |___| |       |___________|
|________________________|       |___________|

CPU(Central Processing Unit)

"brain of the computer"

Control Unit:

ALU(Arithmetic/Logic Unit)

"does math"


Many kinds

Fast -> Slow

On the CPU, small amount of very fast memory: register


CPU can only operate directly on data in registers called $0, ..., $31

Q: How many bits does it take to encode a register number?

A: 5

Therefore 15 bits to encode 3 register numbers - leaves 17 bits to encode operation


Communicating with RAM (two commands)


Transfer word from a specified address to a specified register, the desired address goes in to Memory Address Register(MAR)



Do not know which words in memory contain data & which contain code, then how does the machine execute code?

Special register called PC (program counter) holds the address of the next instruction to run.

By convention, we guarantee that a specific address (say 0) contains code to initialize PC to 0.

Computer then runs the fetch-execute cycle:

PC <- 0
    IR <- MEM[PC] \\IR = instruction register, holds the current instruction
    PC <- PC + 4
    decode and execute instruction in IR
end loop

The only program the machine really runs

Again - PC holds the address of the next instruction while the current instruction is running

Q: How does a program get executed?

A: Program called a loader puts your program in memory and sets PC to the address of the first instruction of the program

Q: What happens when a program ends?

A: Need to return control to the loader.

Set PC to the address of the next instruction in the loader.

$31 will contain the right address. Need to set PC to $31

Example 1: Add the value in reg 5 to reg 7 store the result in $3 the return.

Location|Binary                                 |hex       |meaning
00000000|0000 0000 1010 0111 0001 1000 0010 0000|0x00a71820|add $3 $5 $7
00000004|0000 0011 1110 0000 0000 0000 0000 1000|0x03e00008|jr $31