256 ch5
256 ch5
Machine language
Topics:
Instruction operations
Instruction format/encoding
Assembly and disassembly
Linking and loading
Reading: Patterson and Hennessy
2.5, 2.6, 2.10, 2.12
Appendix B B-49 to B-71
Instruction operations:
what operations (+, - etc) are available
Instruction format/encoding:
how an instruction is represented in binary
in memory
One machine language instruction -> 32-bit
word
Recall: some MIPS assembly instructions are
pseudoinstructions
Major difference between assembly language
and machine language:
Machine language has no labels (!)
(Only raw numeric addresses are used.)
Arithmetic/logic instructions
all operands in registers
General format:
op rd, rs, rt
means rd = rs op rt
add rd, rs, rt
sub rd, rs,rt
addu rd, rs, rt
subu rd, rs, rt
addu, subu like add, sub, but overflow ignored
mult rs, rt
64-bit result <- rs * rt
Two extra 32-bit registers: LO and HI
LO = low 32 bits of 64-bit result
HI = high 32 bits of 64-bit result
(or, HI || LO = 64-bit result)
HI
LO
multu rs, rt
same as mult, but overflow ignored
div rs, rt
LO = rs/rt
HI = rs % rt
divu rs, rt same as div, but ignore overflow
assembly language: div $23, $22, $21
machine language:
11
11
add rd,rs,rt
sub rd,rs,rt
mult rs,rt
div rs,rt
addu rd,rs,rt
subu rd,rs,rt
multu rs,rt
divu rs,rt
mfhi rd
mthi rs
mflo rd
mtlo rs
and rd,rs,rt
nor rd,rs,rt
or rd,rs,rt
xor rd,rs,rt
sllv rd,rt,rs
srlv rd,rt,rs
srav rd,rt,rs
10
11
I-format:
16
16
12
Translate:
Assembly language: add $13, $23, 0x12345678
Machine language:
13
16
16
14
15
.data
.word
.word
.word
0:3
16
addi rt,rs,I
addiu rt,rs,I
andi rt,rs,I
lui rt,I
ori rt,rs,I
xori rt,rs,I
sll rd,rt,I
srl rd,rt,I
sra rd,rt,I
lw rt,I(rb)
lb rt,I(rb)
lbu rt,I(rb)
sw rt,I(rb)
sb rt,I(rb)
17
Conditional branches
I-format is used.
Six machine language conditional branches:
beq rs,rt,I
bne rs,rt,I
bltz rs,I
blez rs,I
bgtz rs,I
bgez rs,I
16-bit immediate I gives information on
branch target address (explained later).
18
Machine Language:
19
20
21
22
23
J-format instructions
For jump (j I) and jump and link (jal I)
(I is a 26-bit constant in J-format)
26
PC = [PC]31..28 || I25..0 || 02
Example: given address of here = 0x400104
here: j there
label
here:
address contents
0x400104 000010 00 0101 0010
Target address =
24
25
26
bltz rs,I
bgez rs,I
blez rs,I
bgtz rs,I
beq rs,rt,I
bne rs,rt,I
slt rd,rs,rt
slti rt,rs,I
j I
jal I
jr rs
Special instructions:
0000 0000 0000 0000 0000 0000 0000 1100
syscall
27
Assembly:
assembly language code is translated into
machine code
Disassembly:
machine code (binary) is translated into
assembly language
28
.data
.word
.word
0:4
3
.text
main:
loop:
lw
la
sw
add
ble
$23,y
$16,x
$23,($16)
$16,$16,4
$16,12,loop
Given:
address of x = 0x10010000
address of main = 0x400020
address of y =
29
30
31
32
Disassembly example:
0x400000
0x400004
0x400008
0x40000c
0x400010
0x400014
0x400018
0011
0011
1010
0010
0010
0010
0001
0100
1100
1110
0010
0010
1010
0100
0001
0001
0011
0001
0011
0000
0010
0000
0001
0000
0000
0001
0001
0000
0000
0001
0000
0000
0000
0000
1111
0000
0000
0000
0000
0000
0000
1111
0000
0000
0000
0000
0000
0000
1111
0001
0001
0000
0001
0100
0101
1011
Given:
address of main = 0x400000
address of loop = 0x400008
33
34
35
Example:
[file 1 contains main]
[file 2 contains:
function A
declaration for global variable X]
.data
x: .word
?
# more allocations not shown
.text
A: lw $a0,??
# load X
# code not shown
jr $31
36
[file 3 contains:
function B
declaration for global variable Y]
.data
y: .word
?
# more allocations not shown
.text
B: sw $a1, ??
# store Y
# code not shown
jr $31
37
38
P&H p. 144
Loader copies executable file into memory,
starts execution.
Static linking is fine for user code. But
libraries can be large! Executables will
become too large.
39
3 15:53 ref
3 15:53 ref
40
In dynamic linking:
only user functions are linked at compile time
(library functions remain unresolved)
at run time, libraries are linked with
executable
executable (user code + libraries) then loaded
into memory, start execution
With simple dynamic linking, executables are
smaller (include user code only). But entire
libraries are still loaded into memory at run
time.
Refinement (lazy procedure linkage): a library
routine is linked only after it is called.
(More in P&H 2.12)
41
Summary
Topics covered in this chapter:
MIPS machine language instructions
MIPS binary format
Arithmetic/logic R-type instructions
Arithmetic/logic I-format instructions
Loads and stores
Conditional branches and jumps
Assemble a MIPS assembly language program
Disassemble a MIPS binary program
Basic concepts of linking and loading
42