310 likes | 424 Views
What we want: execute H igh L evel L anguage ( HLL ) programs What we have: computer hardware (a glorified calculator). Machine Code. Assembly Language. computer hardware: executes instructions written in machine language. assembler. assembly language. HLL source code. compiler.
E N D
What we want: execute High Level Language (HLL) programs What we have: computer hardware (a glorified calculator)
computer hardware: executes instructions written in machine language assembler assembly language HLL source code compiler
C source code C++ source code compiler Pascal source code compiler Fortran source code compiler compiler assembly language assembler machine code
assembly language assembler machine code MIPS R2000 assembly language (with occasional x86 examples) the focus ofthis class
VonNeumann Diagram CPU memory I/O CPU processor P
Stored Program Concept memory Program Variables
00000.......00... 3012 11000.......01... 3013 3014 contents at address 3013 8056 8057 addresses memory: 9
processor must fetch instruction load an operand store an operand (a result) • memory can read (given an address) write (given an address) • Therefore, fetch and load are read memory accesses, and store is a write memory access.
mult aa, bb, cc The number of operands is fixed for each unique mnemonic (instruction). The meaning of the order of the operands is fixed. For this example, it could be aa bb * cc or cc aa * bb
00000.......00010 machine code formult aa, bb, cc somewhere in memory: 12
To fetch and execute a single instruction, FETCH THE INSTRUCTION (do a read memory access)
DECODE THE INSTRUCTION • look at the machine code to find the field representing the mnemonic (also called the op code), such that the processor knows what instruction it is executing • this also specifies the number of operands • for this example,it is a multiply instruction,and so there are 3 operands
LOAD THE OPERANDS • this could be 2 steps (for 2 operands) • read memory accesses • load only necessary operandsread at address bbread at address cc
DO THE INSTRUCTION'S OPERATION • most often an arithmetic operation applied to the loaded operands • for the multiply instruction,the loaded operand value for bb is multiplied by the loaded operand for cc
STORE THE RESULTS • the result of the operation needs to go somewhere, so put it there! • for the multiply instruction,store the computed product at the memory location represented by aa
programs contain many sequential instructions . . . 21 instr 1 instr 2 22 instr 3 23 instr 4 24 addresses
Program Counter • usually called the PC • value maintained by the processor • the address of the next instruction to be fetched • Intel's terminology:Instruction pointer, or IP
Instruction Fetch and Execute Cycle • fetch instruction • update PC • decode • load operands • do the operation • store result(s)
Control Instructions • a category of instructions that may modify the PC, other than to update it • invented code example:here: beq x, y, elsewhere add z, y, xelsewhere: mult aa, bb, cc
Possible instruction sequences: 1. if x equals ybeq x, y, elsewheremult aa, bb, cc 2. if x does not equal ybeq x, y, elsewhereadd z, y, xmult aa, bb, cc
For this instruction,if x equals y, then 6. store result(s) places the third operand (the address elsewhere) into the PC
beq x, y, elsewhere 564 add z, y, x 565 mult aa, bb, cc 566 (x) 8004 300 (y) 9300 300 addresses
The name that architectures give to the control instructions is most often • branch • jump In functionality, a further categorization is • conditional • unconditional
condition codes An alternative, but equivalent method for implementing control instructions A processor will have a minimum of 2 boolean variables • zero • negative
step 6. store resultsalso changes the condition codes For example: add x, y, z if y = 1 and z = 2 then x is set to 3 and zero is set to Falsenegative is set to False
bpos elsewhere if zero is False and negative is False then PC elsewhere Must order the instructions such that the correct one to set the condition codes is directly before the one that depends on it . . .
Just for fun: Invent instructions and write assembly language code to do: for (i = 1; i < 10; i++ ) { a = a + i; }
Karen's first solution move i, 1 for: bge i, 10, end_for add a, a, i add i, i, 1 b for end_for: # next instruction would # go here
Karen's condition codes solution move i, 1 for: compare i, 10 # sets C.C. bgez end_for add a, a, i add i, i, 1 b for end_for: # next instruction would # go here