240-334 by Wannarat 240-334 Computer System Design Lecture 2 Instruction Set Archit ecture
Dec 14, 2015
240-334 by Wannarat
What’re the co mponent of ISA
?Machine Instruction SetInstruction formatNature of the fetch through execute
wannarat:
machine instruction set :make use of storage cells, formats, and result of fetch/execute , register transfer
instruction format : size and meaning of field within instruction
the nature of the fetch-execute cycle : things that are done before the operation code is known.
wannarat:
machine instruction set :make use of storage cells, formats, and result of fetch/execute , register transfer
instruction format : size and meaning of field within instruction
the nature of the fetch-execute cycle : things that are done before the operation code is known.
240-334 by Wannarat
What must an inst ruction specify?
Which Operation is perform?
ADD r1,r2,r3Where to find the operands
ADD r1,r2,r3Place to store the result
ADD r1,r2,r3Location of next instruction
240-334 by Wannarat
Basic ISA Class Accumulator (1 register)
1 address add A; acc <= acc + mem[A]
1 + x address addx A; acc <= acc + mem[A+x] Stack :
0 address add tos <= tos + next General Purpose Register
2 address add A, B
3 address add A, B, C
wannarat:
add A, B ; A = A + B
add A B C; A = B + C
wannarat:
add A, B ; A = A + B
add A B C; A = B + C
240-334 by Wannarat
Basic ISA Classes(con’t) Load/Store
load Ra, RbRa <= mem[Rb]
Store Ra, Rb mem[Rb] <= Ra
240-334 by Wannarat
Compare numb er of instruction
Code Sequence for C = A + B
Stack Accumulator Register Register
(reg. - mem) (load/store)
Push A Load A Load R1,A Load R1,A
Push B Add B Add R1,B Load R2,B
Add Store C Store C, R1 Add R3,R1,R2
Pop C Store C,R3
240-334 by Wannarat
General PurposeRegi ster 1975 - 1995 all machines use general purpose re
gisters. Advanced of Registers
- faster than memory
- easier for compiler to use
- hold variables
wannarat:
easier for compiler to use : (A*B) - (C*D) - (E*F) can do multiplies in any order vs. stack
hold variables: memory traffic is reduced (program speed up), code density improves ( register named with fewer bits than memory location)
wannarat:
easier for compiler to use : (A*B) - (C*D) - (E*F) can do multiplies in any order vs. stack
hold variables: memory traffic is reduced (program speed up), code density improves ( register named with fewer bits than memory location)
240-334 by Wannarat
Summary Instru ction Set Class
Data Movement Instructions
- Load
- Store Arithmetic and Logic (ALU) Instruction
- Add, Sub, Shift … Branch Instructions
- Br, Brz, …
240-334 by Wannarat
- 3 Address Mac hine and ISA
wannarat:
Address of next instruction kept in Proces sor state register (PC)
wannarat:
Address of next instruction kept in Proces sor state register (PC)
240-334 by Wannarat
- 0 Address Machi neandI SA
wannarat:
- Push down stack in CPU,
Arithmetic uses stack for both operands and result,
- Computer must have 1 address instructi on to push and pop operands to and fro
m the stack
wannarat:
- Push down stack in CPU,
Arithmetic uses stack for both operands and result,
- Computer must have 1 address instructi on to push and pop operands to and fro
m the stack
240-334 by Wannarat
EEEEEEEE - *a = (b+c) d e
-3 address -2 address -1 address EEEEE ,, load a,b load b push b ,, add a,c EEE E push c
sub a,a,e mpy a,d mpy d EEE sub a,e sub e push d
store a mpy push e sub pop a
240-334 by Wannarat
Real Machi ne Have mixture of 3, 2, 1 or 0 address instructions if ALU instructions only use registers for operan
ds and result, machine type is load-store mix of register-memory and memory-memory
240-334 by Wannarat
Addressing ModE
Addressing Mode Examples MeaningRegister Add r4,r3 r4 <= r4 + r3Immediate Add r4,#3 r4 <= r4 + 3
Displacement Add r4,100(r1) r4 <= r4 + mem[100+r1]indirect(r) Add r4,(r1) r4 <= r4 + mem[r1]
index+base Add r3,(r1+r2) r3 <= r3 + mem[r1+r2]Direct Add r1,(1001) r1 <= r1 + mem[1001]
indirect(m) Add r1,@(r3) r1 <= r1 + mem[mem[r3]]-auto incre Add r1,(r2)+ r1 <= r1+mem[r2];r2=r2+d-auto decre -Add r1, (r2) -r2 <=r2 d,r1<=r1+mem[r2]
scaled Add r1,100(r2)[r3] r1 <=r1+mem[100+r2+r3*d]
240-334 by Wannarat
MIPS Registers - 3132 0 0x bitGPR(R= ) - 32 32x bit FP registerPC EE EE-EEEEEEEEEE EEEEEE EEEEEEEE0R1R
31R
PC
lohi
240-334 by Wannarat
Memory AddresEEEE EEEEEEE EE EEEEE EE E-EEEE EEEEEE1980, 8()
How do byte address map onto words? Can a word be placed on any byte boundary?
240-334 by Wannarat
Summary ISA Variable length instructions, if code
size is very important. Fixed length instructions, if perform
ance is most important. Embedded Machine (ARM, MIPS) hav
-e optional mode to execute 16 bitwide.
(decide performance or density)
240-334 by Wannarat
MIPS ArithmeticInstructionInstruction Exampl e Meaning1 .add 1 2 3add $ ,$ ,$ 1 2 3$ = $ + $2.subtract sub $1,$2,$3 ------------------
3.add imme addi $1,$2,100 ------------------ 4.add unsign 1 2 3addu $ ,$ ,$ ------------------
5. 123subu $ ,$ ,$ ------------------6. -----------------1 2100addiu $ ,$ ,7.multiply EEEE 2 3$ ,$
2 3Hi,Lo = $ x$8. multu $2,$3 ------------------
240-334 by Wannarat
MIPS ArithmeticInstructionInstruction Example Meani ng9.divide 2 3 =2/3 ,
=2 3 ,10. 2 3Divu $ ,$11.mov mfhi $112. mflo $1
240-334 by Wannarat
MIPS Logical InstructionInstruction Example Meani ng13.AND and14.OR or15.XOR xor16.NOR nor17. andi18. ori19. xori
20.shift left logical sll $1 ,$2 ,1 021. 1 210Srl $ ,$ ,
240-334 by Wannarat
MIPS Logical InstructionInstruction Example Meani ng
22 210.shift rightarithmsra $,$ , EEEEE EEEEEEE23. sl l v24. srl v25. srav
240-334 by Wannarat
MIPS data transEEE EEEEEEEEEEEE
26. 50043sw (r),r EEEEE EEEE27. 502 4 3sh (r ),r
store half word 28. 41 4 3sb (r ,r store byte
29. 1,30(2) load word30. 1,40(2) load half word31. 140 2Lb r , (r ) load byte32 140lui r , load upper
16 16immediate ( bits shifted left by )
240-334 by Wannarat
EEEEEEE EEEEEEEEE Condition Code
EEE 1 2 3r ,r ,rbz label
Condition RegisterEEE 1 2 3r ,r ,rEEE 1r ,label
Compare and BranchEEE 1 2r ,r ,label
240-334 by Wannarat
MIPS Compare a nd Branch
Compare and BranchBEQ rs,rt,offsetBNE rs,rt,offset compare to zero and BranchBLEZ rs, offsetBGTZ rs, offsetBLT <BGEZ >=BLTZAL if R[rs] < 0 then branch and link(to R31)
BGEZAL >=
240-334 by Wannarat
EEEE EEE EEEE EE structi onSet
0 0R always = “ ” (even if u try to write) / EEE EEEE -+ 4 > 3 1 Imme arith and logical are extended
- EE EEE EEEE EEEEEE EE EE EEEE3 2- arith imme op are sign extend to 32 bits
data loaded by lb, lh extended- lbu, lhu are zero extended- lb, lh are sign extedned
Overflow occur in ADD, SUB, ADDI EEEEE EE ’ ADDU, SUBU, ADDIU, AND, OR, XOR, N
OR, SHIFT, MULT, MULTU, DIV, DIVU
240-334 by Wannarat
MIPS arithmetic 3Instruction has operands EEEEEEE EEEEE EE EEEEE
Pascal Code : a := b + c; MIPS Code :
0 1 2add $s , $s , $s
240-334 by Wannarat
MIPS Arithmetic Pascal Code : E EE: = + + ;
- e := f a; MIPS Code : 0 1 2add $t , $s , $s
00 3add$s ,$t ,$s 4 5 0sub $s , $s , $s
240-334 by Wannarat
Register & Memory Registers were used in Arithmetic In
- structions 32 registers
240-334 by Wannarat
Memory OrganiEEEEEE Memory is an index into the array Byte Addressing = points to a byte of
memory
1 8 bits of Data8 bits of Data8 bits of Data8 bits of Data8 bits of Data8 bits of Data8 bits of Data
234567
240-334 by Wannarat
Memory OrganiEEEEEE - 32 4For MIPS, a word is bit or by
EEE232 bytes with byte addresses fro
0 2m to -321
230 words with byte address from 0 4 6 2, , , …, -324
0 32 bits of Data32 bits of Data32 bits of Data32 bits of Data32 bits of Data32 bits of Data32 bits of Data
48
12162024
240-334 by Wannarat
MIPS Load/Stor e Instruction
Code : 8 8A[ ] = h + A[ ];
8 8 4 32A[ ] ==> x = (word alignment)
MIPS Code : lw $t0, 32(3); 0,2,0;
0sw $t , 32 3($s );
Arithmetic Operand is Register, not Memory!!
240-334 by Wannarat
Example : Swap (int v[], int k);
{ Int temp;
temp = v[k]; swap: muli $2, $5, 4
v[k] = v[k+1]; add $2,$4,$21v[k+ ]=temp; 15 0 2lw $ , [$ ]
} lw $16, 4[$2] sw $16, 0[$2] sw $15, 4[$2]
31jr $
240-334 by Wannarat
Meaning
EEE 2 4 2$ , $ , $ 2 4 2$ = $ + $ ;lw 16 0 2$ , [$ ]
16 0 2$ = Memory[ + $ ]sw 15 4 2$ , [$ ]
4 2 15Memory[ +$ ] = $
240-334 by Wannarat
Machine Language Instructions, like register & words of
32data are bits long.- 0 1 2add $t , $s , $s
- 0 9 1 17 2register : $t = , $s = , $s 18
Instruction Format
240-334 by Wannarat
Machine Language - I type for Data transfer instruction Example : lw $t0 , 3 2 ($s2 )
240-334 by Wannarat
Control Decision Making instructions MIPS conditional branch instructions:
- bne $t0, $t1, label- 0 1beq $t , $t , label
Example : if (i=j) h= i +j;EEE 0 1$s , $s , Labeladd 3 0 1$s , $s , $s
EEEEE E EEE
240-334 by Wannarat
Control MIPS unconditional Branch Example :
if ( i != j) beq $s4, $s5, label;h=i+j; add 345
else j lab2-h=i j; lab1: sub $s3, $s4, $s5
lab2: …
240-334 by Wannarat
EEEEEEEE
Use MIPS Assembly to write program.
1. “Factorial Program” n is input, Example : if n=3,
3 3 2 1 6result = ! = x x =
240-334 by Wannarat
EEEEEEEE
Use MIPS Assembly to write program.
2 E EEEE EEEEEEE EEEE EEE E EEE EE E he MIPS instructions