Arch 03&4 2022 PPT
Arch 03&4 2022 PPT
em Architecture
Lecturre 33-44
Words and Bytes
• 232 by
bytes
es : by
byte
e add
addresses
esses from
o 0 to
o 232-1
• Big-endian: byte numbers start att the big (most significant) end
Big-Endian Little-Endian
Byte Wordd Byte
Address Addres
ss Address
C D E F C F E D C
8 9 A B 8 B A 9 8
4 5 6 7 4 7 6 5 4
0 1 2 3 0 3 2 1 0
MSB LSB MSB LSB
Big Endian Address
sing
• With Big Endian addressing, the
t byte binary address
x . . . x00
is in the most significant position (big end) of a 32 bit
word (IBM, Motorolla, Sun, HP P).
M SB
S LSB
S
0 1 2 3
4 5 6 7
Little Endian Addre
essing
• With Little Endian addressing, th he byte binary address
x . . . x00
is in the least significant position
n (little end) of a 32 bit word (DEC,
Intel).
)
M SB LSB
3 2 1 0
7 6 5 4
• Big-endian: 0x00000045
• Little-endian: 0x00000067
Big-Endian
g Little-Endian
Wordd
Byte Address 0 1 2 3 Address 3 2 1 0 Byte Address
Data Value 23 45 67 89 0 23 45 67 89 Data Value
MSB LSB MSB LSB
Operands: Constants/Immediates
• lw and sw illustrate the use of co onstants or immediates
• Called immediates because theyy are immediately available
from the instruction
• Immediates dondon’tt require a regisster or memory access.
• Add immediate (addi) instruction n adds an immediate to a
variable ((held in a register).
g )
• An immediate is a 16-bit two’s co omplement number.
High-level code M
MIPS assembly code
# $s0 = a
a, $s1 = b
a = a + 4; ad
ddi $s0, $s0, 4
b = a – 12; ad
ddi $s1, $s0, -12
Constants in MIPS instructions
1010101010101010 1111000011110000
Loading larger consttants
• "load upper immediate" instructio on
l i $t0,
lui $t0 1010101010101010
• then get the lower order bits right, i.e.,
orii $t0,
$t0 $t0,
$t0 11110000111100000
1010101010101010 0000000000000000
0000000000000000 1111000011110000
1010101010101010 1111000011110000
Instruction Formats
R -T y p e
op rs rt rd sham t fu n c t
6 b its 5 b its 5 b its 5 b its 5 b its 6 b its
I- T y p e
op rs rt im m
6 b its 5 b its 5 b its 1 6 b its
J -T y p e
op addr
6 b its 2 6 b i ts
R-Type
• Register-type
Register type
• 3 register operands:
– rs,
rs rtrt: source
so rce registers
– rd: destination register
• Oth fields:
Other fi ld
– op: the operation code or opccode (0 for R-type instructions)
– funct:
f t the th function
f ti
together, the opcodee and function tell the computer
what
h t operation
ti to
t bbee performed
f d
– shamt: the shift amount for shift s instructions, otherwise it’s 0
R -T
T yp
y e
op rs rt rd sham t funct
6 b its 5 b its 5 b its 5 b its 5 b its 6 b its
R-Type Examples
Assembly Code F ie ld V a lu e s
op rs rt rd sham t fu n ct
0 17 18 16 0 32
add $s0
$s0, $s1,
$s1 $s2
0 11 13 8 0 34
sub $t0, $t3, $t5 6 b its 5 b its 5 b its 5 b its 5 b its 6 b its
Assembly Code
F ie
i ld V a llu e s
op rs rt rd sham t fu n ct
add $s0,
$s0 $s1,
$s1 $s2 0 17 18 16 0 32
A s s e m b ly C o d e F ie ld V a lu e s
op rs rt rd sham t fu n ct
M achine
hi C ode
de
op rs rt rd s ham t funct
I- T y p e
op rs rt im m
6 b its 5 b its 5 b its 1 6 b its
I-Type Examples
addi $
$t0,
, $s3,
$ , -12 8 19 8 -12
lw $t2, 32($0) 35 0 10 32
sw $s1, 4($t1) 43 9 17 4
6 bits 5 bits
b 5 bits 16 bits
lw $t2
$t2, 32($0) 35 0 10 32
sw $s1, 4($t1) 43 9 17 4
6 bit
bits 5 bits 5 bit
bits 16 bit
bits
Machine Code
Differing order of registers in op r
rs rt im m
100011 00000
0 01010 0000 0000 0010 0000 (0x8C0A0020)
addi rt, rs, imm
101011 01001
0 10001 0000 0000 0000 0100 (0xAD310004)
lw rt,
rt imm(rs) 6 bits 5 bits 5 bits 16 bits
2’s comp
plement of (-12)=1111 1111 1111 0100
sw rt, imm(rs)
Machine Language: J--Type
Jump-type
J -T y p e
op addr
6 b its 2 6 b its
Power of Stored Program
S to re d P ro g r a m
A d d re s s I s tr
In t u c tio
ti n s
0040000C 0 1 6 D 4 0 2 2
00400008 2 2 6 8 F F F 4
00400004 0 2 3 2 8 0 2 0
00400000 8 C 0 A 0 0 2 0 PC
M a in M e m o r y
Interpreting Machine La
anguage Code
• Start with opcode
• Opcode tells how to parse the remaining bits
• If opcode is all 0’s
– R R-type
type instruction
– Function bits tell what instructtion it is
• Otherwise
– opcode tells what instruction it is
op
p rs rt rd shamt funct op
p rs rt rd shamt funct
(0x02F34022) 000000 10111 10011 01000 00000 100010 0 23 19 8 0 34 sub $t0, $s7, $s3
0 2 F 3 4 0 2 2
Logical Instructions
• and, or, xor, nor
– and:d useful
f l ffor masking
ki b bits
bit
• Masking all but the leeast significant byte of a value:
0 F234012F AND
0xF234012F D 00x000000FF
000000FF = 00x0000002F
0000002F
– or: useful for combining bit fields
• Combine
C bi 00xF2340000
F2340000 with
ith 00x000012BC:
000012BC
0xF2340000 OR 0x000012BC
0 = 0xF23412BC
– nor: usefulf l for
f inverting
i ti bit
bits:
• A NOR $0 = NOT A
• andi,
di ori,
i xorii
– 16-bit immediate is zero-extended (not sign-extended)
– norii not needed
d d
Logical Instruction Exa
amples
S
Source Registers
R i t
$s1 1111 1111 1111 1111 0000 0000 0000 0000
Source Registers
$s1 1111 11
111 1111 1111 0000 0000 0000 0000
$s2 0100 01
110 1010 0001 1111 0000 1011 0111
Source Values
$s1 0000 0000
0 0000 0000 0000 0000 1111 1111
S
Source Values
V l
$s1 0000
0 0000 0000 0000 0000 0000 1111 1111
imm 0000
0 0000 0000 0000 1111 1010 0011 0100
zero-extended
Assembly Code Result
andi $
$s2,
, $ , 0xFA34 $s2 0000
$s1, 0 0000 0000 0000 0000 0000 0011 0100
ori $s3, $s1, 0xFA34 $s3 0000
0 0000 0000 0000 1111 1010 1111 1111
xori $s4 $s1, 0xFA34 $s4 0000
$s4, $s1 0 0000 0000 0000 1111 1010 1100 1011
Shift Instructions
srl $
$s2,
, $
$s1,
, 2 0 0 17
7 18 2 2
Machine Code
op rs rt rd shamtt f
funct
target: # label
add $s1, $s1, $s0 # $s1 = 4 + 4 = 8
Branch Not Taken (bn
ne)
# MIPS assembly
addi $s0,
$s0 $0,
$0 4 # $s0 = 0 + 4 = 4
addi $s1, $0, 1 # $s1 = 0 + 1 = 1
sll $s1 $s1,
$s1, $s1 2 # $s1 = 1 << 2 = 4
bne $s0, $s1, target # branch not taken
addi
ddi $s1,
$ 1 $s1,
$ 1 1 # $s1
$ 1=4+1=5
sub $s1, $s1, $s0 # $s1 = 5 – 4 = 1
target:
add
dd $s1,
$ 1 $s1,
$ 1 $$s00 # $s1
$ 1=1+4=5
Unconditional Branchin
ng / Jumping (j)
# MIPS assembly
addi $s0,
$s0 $0,
$0 4 # $s0 = 4
addi $s1, $0, 1 # $s1 = 1
j target # jump p to target
sra $s1, $s1, 2 # not executed
e
addi
ddi $ 1 $s1,
$s1, $ 1 1 # nott executed
e t d
sub $s1, $s1, $s0 # not executed
e
target:
add
dd $ 1 $s1,
$s1, $ 1 $$s00 # $s1
$ 1=1+4=5
Unconditional Branching (jr)
(
# MIPS assembly
0x00002004 jr $s0
• if statements
t t t
• if/else
if/ l statements
• while loops
• for loops
If Statement
L sub $s0
L1: $s0, $s0
$s0, $s3
If / Else Statement