Lecture3 Language1
Lecture3 Language1
Lecture 3:
Language of the computer
1/35
Outline
Introduction
4/35
© by Hakem Beitollahi
Outline
Introduction
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)
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
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)
Load word
lw $t0, memory-address
Register Memory
Store word
sw $t0, memory-address
Register Memory
15/35
© by Hakem Beitollahi
Memory operand (III)
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
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
21/35
© by Hakem Beitollahi
The Constant Zero
22/35
© by Hakem Beitollahi
Check Yourself
23/35
© by Hakem Beitollahi
Outline
Introduction
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
0 17 18 8 0 32
28/35
© by Hakem Beitollahi
Hexadecimal
Base 16
Compact representation of bit strings
4 bits per hex digit
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
35 19 8 32
32/35
© by Hakem Beitollahi
Representing instructions in
the computer (V|||)
33/35
© by Hakem Beitollahi
Representing instructions in
the computer (IX)
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
35/35
© by Hakem Beitollahi
Stored Program Computers
OP rs rt rd shamt func
0 16 17 18 0 34