LOGO
Lecture 3:
Language of the computer
Dr. Hakem Beitollahi
Department of Computer Engineering,
Iran University of Science and Technology
1/35
Outline
Introduction
Operations of the computer hardware
Operands of the computer hardware
Representing instructions in the computer
2/35
© by Hakem Beitollahi
Introduction (I)
To command a computer’s hardware, you must
speak its language
The words of a computer’s language are called
instructions
Its vocabulary is called an instruction set
Computer languages are quite similar, more like
regional dialects than like independent languages
If you learn one, you can learn all other computers
languages easily
To design a computer, you should first know its
language
We focus on MIPS computer’s language
3/35
© by Hakem Beitollahi
Introduction (II)
Understanding the language of the
hardware is a key to understanding the
hardware/software interface
Different computers have different
instruction sets
But they are very similar
• How much British accent is similar with American
accent
High-level language (e.g., C++, java) Assembly language
Machine language (e.g., 0011001110)
4/35
© by Hakem Beitollahi
Outline
Introduction
Operations of the computer hardware
5/35
© by Hakem Beitollahi
Arithmetic operations
(I)
Add operation
In c++ langauge a = b + c;
In MIPS assembly language add a, b, c #a=b+c
In MIPS machine language
00000010001100100100000000100000
High level language (e.g., C++) are human friendly
language
Assembly languages are human machine instructions
Machine language are hardware friendly language
In MIPS computer # sign is for comments and it is
ignored by the compiler
Add gets three operands:
add destination, source 1, source 2
6/35
© by Hakem Beitollahi
Arithmetic operations
(II)
Add example: in C++ code we have
a = b + c + d + e;
Translate it to MIPS assembly language
Solution:
add a, b, c # a = b+c
add a, a, d #a=a+d=b+c+d
add a, a, e # a = a + e = b + c + d+ e
• A single line of C code is converted into
multiple lines of assembly code
7/35
© by Hakem Beitollahi
Arithmetic operations
(III)
Subtract
In c++ a = b – c;
In MIPS assembly sub a, b, c # a = b – c
Example: what is the assembly of
f = (g + h) - (i + j);
Solution:
add t0, g, h # t0 = g + h
add t1, i, j # t1 = i + j
sub f, t0, t1 # f = t0 – t1
8/35
© by Hakem Beitollahi
Outline
Introduction
Operations of the computer hardware
Operands of the computer hardware
9/35
© by Hakem Beitollahi
Operands (I)
In C++, each “variable” is a location in memory
In hardware, each memory access is expensive – if
variable a is accessed repeatedly,
The hardware must bring the variable into an on-chip
scratchpad and operate on the scratchpad (registers)
To simplify the instructions, we require that each
instruction (add, sub) only operates on registers
Note: the number of operands (variables) in a C
program is very large; the number of operands in
assembly is fixed…there can be only so many
scratchpad registers
Registers are the bricks of the computer
construction.
10/35
© by Hakem Beitollahi
Operands (II)
Arithmetic instructions use register
operands
MIPS has a 32 × 32-bit register file
Use for frequently accessed data
Numbered 0 to 31
32-bit data called a “word”
Assembler names
$t0, $t1, …, $t9 for temporary values
$s0, $s1, …, $s7 for saved variables
The natural unit of access
In a computer, usually a group of 32 bits;
corresponds to the size of a register
Modern 64-bit architectures have 64-bit
wide registers
Pentium 4, dual core, core i7
© by Hakem Beitollahi
Register Operand
Example
C code:
f = (g + h) - (i + j);
f, …, j in $s0, …, $s4
Compiled MIPS code:
add $t0, $s1, $s2
add $t1, $s3, $s4
sub $s0, $t0, $t1
12/35
© by Hakem Beitollahi
Memory operand (I)
The processor can keep only a small amount of
data in registers
Computer memory contains billions of data
elements.
Data structures (arrays, variables, pointers, etc)
are kept in memory
Values must be fetched from memory before
(add and sub) instructions can operate on them
To apply arithmetic operations
Load values from memory into registers
Store result from register to memory
We need instructions to transfer data from
13/35 memory to registers and vice versa
© by Hakem Beitollahi
Memory operand (I)
Data Transfer Instruction: A command that moves data between
memory and registers
Load word
lw $t0, memory-address
Register Memory
Store word
sw $t0, memory-address
Register Memory
How is memory-address determined?
Address: A value used to delineate the location of a specific data element
within a memory array.
14
Memory operand (II)
To access a word in memory, the
instruction must supply the memory
address
15/35
© by Hakem Beitollahi
Memory operand (III)
• The compiler organizes data in memory… it knows the
location of every variable (saved in a table)… it can fill
in the appropriate mem-address for load-store instructions
int a, b, c, d[10]
…
Memory
Base address
16
Memory operand (IV)
Memory is byte addressed
Each address identifies an 8-bit byte
Words are aligned in memory
Address must be a multiple of 4
MIPS is Big Endian
Most-significant byte locates at least address of a word
c.f. Little Endian: least-significant byte locates at least address
Alignment restriction: A requirement that data be aligned in memory on
natural boundaries
17/35
© by Hakem Beitollahi
Memory Operand
Example 1
C code:
g = h + A[8];
g in $s1, h in $s2, base address of A in $s3
Compiled MIPS code:
Index 8 requires offset of 32
• 4 bytes per word
lw $t0, 32($s3) # load word
add $s1, $s2, $t0
Offset (4*8) base register
Note: in MIPS words must start at address that are multiple of 4
18/35
© by Hakem Beitollahi
Memory Operand
Example 2
C code:
A[12] = h + A[8];
h in $s2, base address of A in $s3
Compiled MIPS code:
Index 8 requires offset of 32
• lw $t0, 32($s3) #load word A[8] into t0
• add $t0, $s2, $t0 # t0 = h + A[8]
• sw $t0, 48($s3) #store word t0 in A[12]
19/35
© by Hakem Beitollahi
Registers vs.
Memory
Registers are faster to access than
memory
Operating on memory data requires
loads and stores
More instructions to be executed
Compiler must use registers for
variables as much as possible
Only spill to memory for less frequently used
variables
Register optimization is important!
20/35
© by Hakem Beitollahi
Immediate instructions
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)
addi $s3, $s3, 4 # s3 = s3 + 4
No subtract immediate instruction
Just use a negative constant
addi $s2, $s1, -1
21/35
© by Hakem Beitollahi
The Constant Zero
MIPS register 0 ($zero) is the
constant 0
Cannot be overwritten
Useful for common operations
E.g., move between registers
add $t2, $s1, $zero
22/35
© by Hakem Beitollahi
Check Yourself
Given the importance of registers, what is
the rate of increase in the number of
registers in a chip over time?
1. Very fast: They increase as fast as Moore’s law, which
predicts doubling the number of transistors on a chip every 18
months.
2. Very slow: Since programs are usually distributed in the
language of the computer, there is inertia in instruction set
architecture, and so the number of registers increases only as
fast as new instruction sets become viable.
23/35
© by Hakem Beitollahi
Outline
Introduction
Operations of the computer hardware
Operands of the computer hardware
Representing instructions in the computer
24/35
© by Hakem Beitollahi
Representing instructions in
the computer (I)
Ready to explain the difference between the way humans
instruct the computers and the way computer sees
instructions.
Instructions are kept in RAM as a series of 0 and 1
Each piece of an instruction can be considered as 0s and
1s, and placing these numbers side by side forms the
instruction.
Instructions are called machine codes
In MIPS each instruction is 32 bits
Registers are referred to by almost all instructions, so we
convinced to map register names into numbers
$t0 – $t7 are reg’s 8 – 15
$t8 – $t9 are reg’s 24 – 25
$s0 – $s7 are reg’s 16 – 23
25/35
© by Hakem Beitollahi
Representing instructions in
the computer (II)
MIPS instructions are classified into 3
categories:
R-format (R for register)
I-format (I for immidate)
J-format (J for jump)
26/35
© by Hakem Beitollahi
MIPS R-format
Instructions
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
Instruction fields
op: operation code (opcode)
• opcode The field that denotes the operation and format of an
instruction.
rs: first source register number
rt: second source register number
rd: destination register number
shamt: shift amount (00000 for now)
27/35
funct: function code (extends opcode)
© by Hakem Beitollahi
R-format Example
op rs Tell
rt the CPU
rd thatshamt funct
instruction is add
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
Destination is register
number 8
add $t0, $s1, $s2
special $s1 $s2 $t0 0 add
0 17 18 8 0 32
000000 10001 10010 01000 00000 100000
First source is register Second source is
number 17 register number
00000010001100100100000000100000 18
2 = 02324020 16
28/35
© by Hakem Beitollahi
Hexadecimal
Base 16
Compact representation of bit strings
4 bits per hex digit
0 0000 4 0100 8 1000 c 1100
1 0001 5 0101 9 1001 d 1101
2 0010 6 0110 a 1010 e 1110
3 0011 7 0111 b 1011 f 1111
Example: eca8 6420
1110 1100 1010 1000 0110 0100 0010 0000
29/35
© by Hakem Beitollahi
MIPS I-format
Instructions
op rs rt constant or address
6 bits 5 bits 5 bits 16 bits
Immediate arithmetic and load/store
instructions
rt: destination or source register number
Constant: –215 to +215 – 1
Address: offset added to base address in rs
30/35
© by Hakem Beitollahi
I-format Example
op rs rt constant or address
The source register is
6 bits 5 bits 5 bits 16 bits
register number 19
The destination
lw $t0, 32($s3) register is register
number 8
op
The instruction isrslw rt constant or address
Op for lw $s3 $t0 32
35 19 8 32
100011 10011 01000 0000000000100000
The address offset is
10001110011010000000000000100000 2 = 8D680020
32 16
31/35
© by Hakem Beitollahi
Check Yourself
Why doesn’t MIPS have a subtract
immediate instruction?
1. Negative constants appear much less frequently in C and
Java, so they are not the common case and do not merit
special support.
2. Since the immediate field holds both negative and positive
constants, add immediate with a negative number is equivalent
to subtract immediate with a positive number, so subtract
immediate is superfluous.
32/35
© by Hakem Beitollahi
Representing instructions in
the computer (V|||)
33/35
© by Hakem Beitollahi
Representing instructions in
the computer (IX)
Example: translating a MIPS assembly
language into Machine code
suppose $t1 register has the base of the array A
and $s2 corresponds to h, the statement is
A[300] = h + A[300];
solution:
First convert the high-level language to assembly:
lw $t0, 1200($t1) #Temporary reg $t0 gets A[300]
add $t0, $s2, $t0 # Temporary reg $t0 gets h +A[300]
sw $t0, 1200($t1) #stores h + A[300] back to A[300]
34/35
© by Hakem Beitollahi
Now convert assembly language into
machine code
I-format OP rs rt Address
R-format OP rs rt rd shamt func
35 9 8 1200
0 18 8 8 0 32
43 9 8 1200
100011 01001 01000 0000 0100 1011 0000
000000 10010 01000 01000 00000 100000
101011 01001 01000 0000 0100 1011 0000
35/35
© by Hakem Beitollahi
Stored Program Computers
The BIG Picture Instructions represented
in binary, just like data
Instructions and data
stored in memory
Programs can operate on
programs
e.g., compilers, linkers, …
Binary compatibility
allows compiled programs
to work on different
computers
Standardized ISAs
36/35
© by Hakem Beitollahi
Check yourself: what MIPS instruction
does this represent? Choose from one
of the four options below
OP rs rt rd shamt func
0 16 17 18 0 34
1. add $s0, $s1, $s2
2. add $s2, $s0, $s1
3. sub $s2, $s1, $s0
4. sub $s2, $s0, $s1
37/35
© by Hakem Beitollahi