Professional Documents
Culture Documents
the Computer
CHAPTER 2
Course Instructor: Engr. Afshan Jamil
1
Software
Compiler Assembler
2
Computer Architecture
• Architecture: System attributes that have a
direct impact on the logical execution of a
program
• Architecture is visible to a programmer:
– Instruction set
– Data representation
– I/O mechanisms
– Memory addressing
3
Computer Organization
• Organization: Physical details that are
transparent to a programmer, such as
– Hardware implementation of an instruction
– Control signals
– Memory technology used
4
Instruction
• It is a bit stream of zeros and ones having
multiple fields and can be understood by a
computer
– Opcode: operation to be performed
– Address Fields: Memory or Register Address
– Mode: specifies the way the address field is to be
interpreted.
– Special Fields
• field that gives the number of positions to shift in a
shift-type instruction
• An operand field in an immediate operand instruction
5
Register Set
• Register set consists of all registers in the CPU
that are accessible to the programmer
– General Purpose registers
– Processor status register (PSR)
– Stack pointer (SP)
6
Instruction Set Architecture (ISA)
• ISA= All of the programmer visible components and operations
of the computer.
• Memory organization
– Address space: how many locations can be addressed?
– Addressability: how many bits per location?
• Register Set
– How many? What size? How are they used?
• Instruction set
– Opcodes (Operation codes)
– Data types
– Addressing modes
8
Operand Address
• Implied Address
– Address is specified either by the Opcode of the
instruction or by an address assigned to one of the
other operands.
– No need for a memory or register address field for
the operand in the instruction
• Explicit Address
– Operand has an address in the instruction
9
ISA Design Issues
• Where are operands stored?
– registers, memory, stack, accumulator
• How many explicit operands are there?
– 0, 1, 2, or 3
• How is the operand location specified?
– register, immediate, indirect, . . .
• What type & size of operands are supported?
– byte, int, float, double, string, vector. . .
• What operations are supported?
– add, sub, mul, move, compare . . .
10
Classifying ISA
• In a stack architecture, operands are implicitly taken
from the stack.
– A stack cannot be accessed randomly.
• In an accumulator architecture, one operand of a binary
operation is implicitly in the accumulator.
– One operand is in memory, creating lots of bus traffic.
• In a general purpose register (GPR) architecture,
registers can be used instead of memory.
– Faster than accumulator architecture.
– Efficient implementation for compilers.
– Results in longer instructions.
11
CONTD…
• Most systems today are GPR systems.
• There are three types:
– Memory-memory where two or three operands may be in
memory.
– Register-memory where at least one operand must be in a
register.
– Load-store/Register-register where no operands may be
in memory.
• The number of operands and the number of available
registers has a direct effect on instruction length.
12
Instruction Formats
• Basic components in each architecture:
GPR register-
Stack Accumulator GPR load/store
memory
architecture architecture architecture
architecture
Main Main Main Main
Memory Memory Memory Memory
Stack
reg reg
AC
reg reg
reg reg
ALU ALU
ALU ALU
13
Instruction Formats
Q: Show the source and destination operands in each architecture:
GPR register-
Stack Accumulator GPR load/store
memory
architecture architecture architecture
architecture
Main Main Main Main
Memory Memory Memory Memory
Stack
reg reg
AC
reg reg
reg reg
ALU ALU
ALU ALU
14
Instruction Formats
A: Show the source and destination operands in each architecture:
GPR register-
Stack Accumulator GPR load/store
memory
architecture architecture architecture
architecture
Main Main Main Main
Memory Memory Memory Memory
Stack
reg reg
AC
reg reg
reg reg
ALU ALU
ALU ALU
Stack
reg reg
AC
reg reg
reg reg
ALU ALU
ALU ALU
16
Instruction Formats
Q: Show how the statement C = A + B may be implemented in each of
these architectures.
Stack
reg reg
AC
reg reg
reg reg
ALU ALU
ALU ALU
Answers:
17
X = (A + B) x(C +D): Three-Address
Instructions
Memory-to-Memory Architecture
•ADD T1, A, B M[T1] ← M[A] +M[B]
•ADD T2, C, D M[T2] ← M[C] + M[D]
•MUL X, T1, T2 M[X] ← M[T1] x M[T2]
Register-Memory Architecture
•ADD R1, A, B R1 ← M[A] +M[B]
•ADD R2, C, D R2←M[C] + M[D]
•MUL X, R1, R2 M[X]←R1 x R2
18
X = (A + B) x(C + D): Two-Address
Instructions
Memory-to-Memory Architecture
19
X = (A + B) x(C + D): One-Address
Instructions
Accumulator Architecture
20
X = (A +B) x(C +D): Zero-Address
Instructions
Stack Architecture
21
Addressing Modes
• Implied Mode
– needs no address field at all
– operand is specified implicitly in the definition of the
opcode
• Examples
– instruction that uses an accumulator without a second
operand is an implied-mode instruction
• Complement accumulator
– data-manipulation instructions in a stack computer
• ADD (the operands are implied to be on top of stack)
22
Addressing Modes
Immediate Mode:
opcode R1 R2 600
Direct Mode:
opcode R1 R2 600 Memory
23
Indirect Mode:
24
Register Direct Mode:
opcode R1 R2 600
Processor Registers
25
Register Indirect Mode:
opcode R1 R2 600
26
Relative Addressing Mode:
opcode R1 R2 600
Memory
PC
27
Indexed Addressing Mode:
opcode R1 R2 600
Memory
IR
28
Addressing Modes (Summary)
Direct, ACC <- 800
Immediate, ACC <- 500
Indirect, ACC <- 300
Relative, ACC <- 600
Index, ACC <- 200
assuming R1 as an index register
29
Class Task
1400 60
30
MIPS Architecture
• MIPS stands for microcomputer without interlocked pipeline
stages
• All MIPS arithmetic instructions have 3 explicit operands.
(Design Principle 1: Simplicity favors regularity)
• MIPS is a load/store architecture.
• Operands must be in registers – only 32 registers provided.
(Design Principle 2: Smaller is faster)
• For MIPS, a word is 32 bits or 4 bytes.
– loading words but addressing bytes
0 32 bits of data
4 32 bits of data
Registers correspondingly hold 32 bits of data
8 32 bits of data
12 32 bits of data
31
Alignment Restrictions
• Bytes in a word can be numbered in two ways:
– byte 0 at the leftmost (most significant) to byte 3 at the rightmost
(least significant), called big-endian
– byte 3 at the leftmost (most significant) to byte 0 at the rightmost
(least significant), called little-endian
32
Operations of the computer
Hardware
• Operand order is fixed (e.g., destination first)
• Example:
C code: A = B + C
33
Operands of the Computer
Hardware
• Arithmetic instructions operands must be in registers
– MIPS has 32 registers
• Compiler associates variables with registers (e.g. $)
• What about programs with lots of variables (arrays, etc.)?
– Use memory,
• Remember MIPS arithmetic operands are registers, not
memory locations therefore, words must first be moved from
memory to registers using loads before they can be operated
on; then result can be stored back to memory
34
Load Instruction
“Used to load a word from memory”
•lw $rt, offset($rs)
•The address is computed by adding the contents of
register $rs to the sign-extended offset (which is an
immediate value).
•Value from address computed above is then loaded
into register $rt
•Example:
• lw $t0, 20($s0) # $t0 = Memory[$s0 + 20]
35
STORE Instruction
“Used to store a word to memory”
•sw $rt, offset($rs)
•The address is computed by adding the contents of
register $rs to the sign-extended offset (which is an
immediate value).
•Value from register $rt is then copied to the memory
address computed.
•Example:
• sw $t0, 20($s0) # Memory[$s0 + 20] = $t0
36
Immediate Operands
• An instruction may require a constant as input. An
immediate instruction uses a constant number as one of
the inputs (instead of a register operand)
• Design Principle # 3:
• “Make the common case fast”
• Example:
• addi $s2, $s0, 4
• There is no “subtract immediate” instruction ! Subtract is
“add a negative”
• addi $s2, $s0, -4
37
Constant 0
• MIPS register 0 ($zero) is the constant 0 !
• Cannot be overwritten !
• Useful for common operations ! e.g.,
• move between registers
• add $t2, $s1, $zero
38
Compiling C code into MIPS
assembly
• f=(g+h)-(i+j);
• g=h+A[8];
• A[12]=h+A[8];
39
Instruction Formats
• Instructions are represented as 32-bit numbers (one
word), broken into 6 fields. Two types of instruction
formats:
• R-format
• I-format
40
R-format
Instruction fields !
• op: operation code (opcode) !
• rs: first source register number !
• rt: second source register number !
• rd: destination register number !
• shamt: shift amount (00000 for now) !
• funct: function code (extends opcode)
41
I-format
44
Instructions for making decisions
• Branch instructions specify
• Opcode, two registers, target address
• PC-relative addressing
• Target address = PC + offset × 4 (PC already
incremented by 4 by this time)
45
CONTD….
• Jump (j and jal) targets could be anywhere in text
segment.
46
CONTD…
• EXAMPLE
• C code:
if (i==j) bne $s3, $s4, Else
f = g+h; add $s0, $s1, $s2
else j Exit
f = g-h; Else: sub $s0,$s1,$s2
Exit:
47
CONTD…
EXAMPLE
C code: Loop: sll $t1, $s3, 2
while(save[i]==k) add $t1, $t1, $s6
i += 1; lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j Loop
Exit:
48
CONTD…
• Set result to 1 if a condition is true
– Otherwise, set to 0
• slt rd, rs, rt
– if (rs < rt) rd = 1; else rd = 0;
• slti rt, rs, constant
– if (rs < constant) rt = 1; else rt = 0;
• Use in combination with beq, bne
slt $t0, $s1, $s2 # if ($s1 < $s2)
bne $t0, $zero, L # branch to L
49
Procedure calling
Steps required
1. Place parameters in registers
2. Transfer control to procedure
3. Acquire storage for procedure
4. Perform procedure’s operations
5. Place result in register for caller
6. Return to place of call
50
Register usage
• $a0 – $a3: arguments (reg’s 4 – 7)
• $v0, $v1: result values (reg’s 2 and 3)
• $t0 – $t9: temporaries
– Can be overwritten by callee
• $s0 – $s7: saved
– Must be saved/restored by callee
• $gp: global pointer for static data (reg 28)
• $sp: stack pointer (reg 29)
• $fp: frame pointer (reg 30)
• $ra: return address (reg 31)
51
Procedure call instructions
• Procedure call: jump and link
• jal ProcedureLabel
– Address of following instruction put in $ra
– Jumps to target address
• Procedure return: jump register
• jr $ra
– Copies $ra to program counter
– Can also be used for computed jumps
• e.g., for case/switch statements
52
Example
• procedure adds 10 to input parameter
int main()
{ int i, j;
i = 5;
j = add10(i);
i = j;
}
int add10(int i)
{ return (i + 10);}
53
Contd…
• main:
addi $s0, $0, 5
add $a0, $s0, $0
jal add10
add $s1, $v0, $0
add $s0, $s1, $0
• add10:
55
CONTD…
• add $t0, $a0, $a1
add $t1, $a2, $a3
sub $s0, $t0, $t1
•
addi $sp, $sp, -12
sw $t1,8($sp)
• sw $t0,4($sp)
• sw $s0,0($sp)
add $v0, $s0, $zero
•
lw $s0, 0($sp)
• lw $t0, 4($sp)
• lw $t1, 8($sp)
addi $sp, $sp, 12
•
jr $ra
56