Chapter2 Instructions Architecture Set
Chapter2 Instructions Architecture Set
Chapter2 Instructions Architecture Set
Chapter 2
Instructions: Language of the
Computer
Binh Tran-Thanh
thanhbinh@hcmut.edu.vn
Objectives
swap(int v[], int k){ swap: multi $2, $5, 4
int temp; add $2, $4, $2
temp = v[k]; lw $15, 0($2)
v[k] = v[k+1]; Compiler lw $16, 4($2)
v[k+1] = temp; sw $16, 0($2)
} sw $15, 4($2)
jr $31
00000000101000100000000100011000
00000000100000100001000000100001
10001101111000100000000000000000
10001110000100100000000000000100 Assembler
10101110000100100000000000000000
10101101111000100000000000000100
00000011111000000000000000001000
Instruction
Data memory Control unit
memory
Input/
output
ALU
I/O BR
data
data
I/O Module …
data
PC: Program Counter
IR: Instruction Register
MAR: Memory Address Register
MBR: Memory Buffers Register
I/O AR: Input Output Address Register
Buffers I/O BR: Input Output Buffer Register
8/13/2021 Faculty of Computer Science and Engineering 6
Instruction execution process
Fetch Stage Execute Stage
Smaller is faster
▪ Example: negate +2
▪ +2 = 0000 0000 … 00102
▪ –2 = 1111 1111 … 11012 + 1
= 1111 1111 … 11102
8/13/2021 Faculty of Computer Science and Engineering 25
Sign Extension
▪ Representing a number using more bits
▪ Preserve the numeric value
▪ In MIPS instruction set
▪ addi: extend immediate value
▪ lb, lh: extend loaded byte/halfword
▪ beq, bne: extend the displacement
▪ Replicate the sign bit to the left
▪ c.f. unsigned values: extend with 0s
▪ Examples: extend 8-bit to 16-bit for signed number
▪ +2: 0000 0010 => 0000 0000 0000 0010
▪ –2: 1111 1110 => 1111 1111 1111 1110
8/13/2021 Faculty of Computer Science and Engineering 26
Representing Instructions
▪ Instructions are encoded in binary
▪ Called machine code
▪ MIPS instructions
▪ Encoded as 32-bit instruction words
▪ Small number of formats encoding operation code
(opcode), register numbers, …
▪ Regularity!
▪ Register numbers
▪ $t0 – $t7 are reg’s 8 – 15
▪ $t8 – $t9 are reg’s 24 – 25
▪ $s0 – $s7 are reg’s 16 – 23
0 17 18 8 0 32
000000100011001001000000001000002 = 0232402016
C compiler
(machine code) ▪ Programs can operate on programs
▪ e.g., compilers, linkers, …
Payroll data
▪ Binary compatibility allows
compiled programs to work on
Book text
different computers
Source code in C ▪ Standardized ISAs
for editor program
$sp $sp
$fp Saved argument
register (if any)
Saved return
address
Saved saved
register (if any)
Local arrays and
$sp structure (if any)
low address
5. Pseudo-direct addressing
op address
Memory
PC Word
8/13/2021 Faculty of Computer Science and Engineering 69
Synchronization
▪ Two processors sharing an area of memory
▪ P1 writes, then P2 reads
▪ Data race if P1 and P2 don’t synchronize
▪ Result depends of order of accesses
▪ Hardware support required
▪ Atomic read/write memory operation
▪ No other access to the location allowed between the read
and write
▪ Could be a single instruction
▪ E.g., atomic swap of register memory
▪ Or an atomic pair of instructions
Static linking
Indirection table
Linker/loader code
Dynamically
mapped code
Compiles
Interprets
bytecodes of
bytecodes
“hot” methods
into native
code for host
machine
1.5
1
1
0.5 0.12
0
C/none C/O1 C/O2 C/O3 Java/int Java/JIT
0.5 0.29
0.05
0
C/none C/O1 C/O2 C/O3 Java/int Java/JIT
500 338
0
C/none C/O1 C/O2 C/O3 Java/int Java/JIT