Professional Documents
Culture Documents
05 Machine Basics
05 Machine Basics
Instructors:
Randal
E.
Bryant
and
David
R.
O’Hallaron
Our
Coverage
¢ IA32
§ The
tradi�onal
x86
§ For
15/18-‐213:
RIP,
Summer
2015
¢ x86-‐64
§ The
standard
§ shark> gcc hello.c
§ shark> gcc –m64 hello.c
¢ Presenta�on
§ Book
covers
x86-‐64
§ Web
aside
on
IA32
§ We
will
only
cover
x86-‐64
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec�ve,
Third
Edi�on
9
Carnegie Mellon
Defini�ons
¢ Architecture:
(also
ISA:
instruc�on
set
architecture)
The
parts
of
a
processor
design
that
one
needs
to
understand
or
write
assembly/machine
code.
§ Examples:
instruc�on
set
specifica�on,
registers.
¢ Microarchitecture:
Implementa�on
of
the
architecture.
§ Examples:
cache
sizes
and
core
frequency.
¢ Code
Forms:
§ Machine
Code:
The
byte-‐level
programs
that
a
processor
executes
§ Assembly
Code:
A
text
representa�on
of
machine
code
¢ Example
ISAs:
§ Intel:
x86,
IA32,
Itanium,
x86-‐64
§ ARM:
Used
in
almost
all
mobile
phones
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec�ve,
Third
Edi�on
11
Carnegie Mellon
Programmer-‐Visible
State
§ PC:
Program
counter
§ Memory
§ Address
of
next
instruc�on
§ Byte
addressable
array
§ Called
“RIP”
(x86-‐64)
§ Code
and
user
data
§ Stack
to
support
procedures
§ Register
file
§ Heavily
used
program
data
§ Condi�on
codes
§ Store
status
informa�on
about
most
recent
arithme�c
or
logical
opera�on
§ Used
for
condi�onal
branching
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec�ve,
Third
Edi�on
12
Carnegie Mellon
¢ Floa�ng point data of 4, 8, or 10 bytes
Object
Code
Code
for
sumstore
0x0400595: ¢ Assembler
§ Translates
.s
into
.o
0x53
0x48 § Binary
encoding
of
each
instruc�on
0x89 § Nearly-‐complete
image
of
executable
code
0xd3
0xe8
§ Missing
linkages
between
code
in
different
0xf2 files
0xff ¢ Linker
0xff
0xff
§ Resolves
references
between
files
• Total
of
14
bytes
0x48 § Combines
with
sta�c
run-‐�me
libraries
0x89 • Each
instruc�on
E.g.,
code
for
malloc,
printf
§
0x03 1,
3,
or
5
bytes
0x5b • Starts
at
address
§ Some
libraries
are
dynamically
linked
0xc3 0x0400595 § Linking
occurs
when
program
begins
execu�on
¢ Disassembler
objdump –d sum
§ Useful
tool
for
examining
object
code
§ Analyzes
bit
pa�ern
of
series
of
instruc�ons
§ Produces
approximate
rendi�on
of
assembly
code
§ Can
be
run
on
either
a.out
(complete
executable)
or
.o
file
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec�ve,
Third
Edi�on
19
Carnegie Mellon
Alternate
Disassembly
Disassembled
Object
0x0400595:
0x53 Dump of assembler code for function sumstore:
0x48 0x0000000000400595 <+0>: push %rbx
0x89 0x0000000000400596 <+1>: mov %rdx,%rbx
0xd3 0x0000000000400599 <+4>: callq 0x400590 <plus>
0xe8 0x000000000040059e <+9>: mov %rax,(%rbx)
0xf2 0x00000000004005a1 <+12>:pop %rbx
0xff 0x00000000004005a2 <+13>:retq
0xff
0xff
0x48 ¢ Within
gdb
Debugger
0x89 gdb sum
0x03
0x5b disassemble sumstore
0xc3 § Disassemble
procedure
x/14xb sumstore
§ Examine
the
14
bytes
star�ng
at
sumstore
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec�ve,
Third
Edi�on
20
Carnegie Mellon
No symbols in "WINWORD.EXE".
Disassembly of section .text:
30001000 <.text>:
30001000: 55 push %ebp
30001001: 8b ec mov %esp,%ebp
30001003: 6a ff
Reverse
e ngineering
push
forbidden
by
$0xffffffff
30001005: 68Microso�
90 10 00E30 nd
push
User
License
Agreement
$0x30001090
3000100a: 68 91 dc 4c 30 push $0x304cdc91
%esi
source
%si index
%edi
destination
%di index
stack
%esp %sp pointer
base
%ebp %bp
pointer
void swap
(long *xp, long *yp)
{ swap:
long t0 = *xp; movq (%rdi), %rax
long t1 = *yp; movq (%rsi), %rdx
*xp = t1; movq %rdx, (%rdi)
*yp = t0; movq %rax, (%rsi)
} ret
Understanding
Swap()
Memory
void swap Registers
(long *xp, long *yp)
{ %rdi
long t0 = *xp;
%rsi
long t1 = *yp;
*xp = t1; %rax
*yp = t0;
} %rdx
Register
Value
%rdi xp
%rsi yp swap:
%rax t0 movq (%rdi), %rax # t0 = *xp
%rdx t1 movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec�ve,
Third
Edi�on
29
Carnegie Mellon
Understanding
Swap()
Memory
Registers
Address
123 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 0x108
%rdx 456
0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
Understanding
Swap()
Memory
Registers
Address
123 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
Understanding
Swap()
Memory
Registers
Address
123 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 456 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
Understanding
Swap()
Memory
Registers
Address
456 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 456 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
Understanding
Swap()
Memory
Registers
Address
456 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 123
0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
%rcx 0x0100
¢ Uses
§ Compu�ng
addresses
without
a
memory
reference
E.g.,
transla�on
of
p = &x[i];
§
§ Compu�ng
arithme�c
expressions
of
the
form
x
+
k*y
§ k
=
1,
2,
4,
or
8
¢ Example
long m12(long x)
{ Converted
to
ASM
by
compiler:
return x*12; leaq (%rdi,%rdi,2), %rax # t <- x+x*2
} salq $2, %rax # return t<<2