instruction-set-design
instruction-set-design
Computer Architecture
Instruction Set Design
Bhaskaran Raman
Room 406, KR Building
Department of CSE, IIT Bombay
http://www.cse.iitb.ac.in/~br
Instruction Set: What and Why
HLL code examples:
C/C++
f()->next = (g() > h()) ? k() : NULL;
Perl
$line =~ s/abc/xyz/g;
Assembler
specific
Machine Language
design
• Interface before
Computer designer
implementation!
Digital Logic • Allows abstraction,
independence
• Challenge: should be easy
to use by the layer above
Instruction Set Defines a Machine
HLL code examples:
C/C++
f()->next = (g() > h()) ? k() : NULL;
Perl
$line =~ s/abc/xyz/g;
MIPS's x86's ARM's
instruction instruction instruction
set set set
Processor
path =
Program
Data + Output
path Data
Processor
path
Registers Memory
Data Output
path
32-bit address
Memory offset
0x0000 000C space Question: why is this useful?
0x0000 0008 Alignment restriction: address
0x0000 0004
0x0000 0000 has to be a unit of 4 (why?
32-bits answered later)
Test Your Understanding...
● Is a subi instruction needed? Why or why not?
● Is sub instruction needed? Why or why not?
Test Your Understanding (continued)...
● Translate the following C-code into assembly lang.:
● Ex1: a[300]=x+a[200]; // all 32-bit int
● What more information do you need?
● Ex2: a[300]=x+a[i+j]; // all 32-bit int
● Can you do it using instructions known to you so far?
# a in s0, x in s1 # a in s0, x in s1
lw $t0, 800($s0) # i in s2, j in s3
add $t1, $t0, $s1 add $t2, $s2, $s3
sw 1200($s0), $t1 muli $t2, $t2, 4
add $t3, $t2, $s0
Registers $t0-$t9 lw $t0, 0($t3)
usually used for add $t1, $t0, $s1
temporary values sw 1200($s0), $t1
Notion of Register Assignment
● Registers are statically assigned by the
compiler to registers
● Register management during code generation:
one of the important jobs of the compiler
● Example from previous exercise...
Instructions for
Bit-Wise Logical Operations
Logical C/C++/Java MIPS
Operators Operators Instructions
Shift Left << sll
Shift Right >> srl
Bit-by-bit AND & and, andi
Bit-by-bit OR | or, ori
Bit-by-bit NOT ~ nor
The Notion of the Program Counter
The program is fetched and
executed instruction-by-
Program
instruction
(in memory) Program Counter (PC)
PC
A special 32-bit register
Convention in my slides:
s0, s1... assigned to variables# in order of appearance
# s0 is x, s1 is y
bne $s0, $zero, ELSE
add $s1, $s0, $s1
j EXIT
ELSE:
sub $s1, $s0, $s1
EXIT:
# Further instructions below
Note: use of $zero register (make the common case fast)
Using Branches for Loops
while(a[i] != 0) i++;
# s0 is a, s1 is i
BEGIN:
sll $t0, $s1, 2
add $t0, $t0, $s0
lw $t1, 0($t0)
beq $t1, $zero, EXIT
addi $s1, $s1, 1
j BEGIN
EXIT:
# Further instructions below
# s0 is i, s1 is a
addi $s0, $zero, 0
BEGIN:
slti $t0, $s0, 10
beq $t0, $zero, EXIT
sll $t1, $s0, 2
add $t2, $t1, $s1
sw 0($t2), $zero
addi $s0, $s0, 1
j BEGIN
EXIT:
# Further instructions below