Chapter03 Machine Level Programming(1)Basics
Chapter03 Machine Level Programming(1)Basics
SYSTEMS PROGRAMMING
MACHINE-LEVEL REPRESENTATION OF
PROGRAMS (1) – BASICS
(based on chapter 3.1-5)
o Historical Perspective
o Program Encoding: C, assembly, machine code
o Assembly Basics
o Arithmetic & Logical Operations
oAdded Features
• Instructions to support multimedia operations
• Instructions to enable more efficient conditional operations
• Transition from 32 bits to 64 bits
• More cores
o Historically
• AMD has followed just behind Intel
• A little bit slower, a lot cheaper
o Then
• Recruited top circuit designers from Digital Equipment Corp. and other
downward trending companies
• Built Opteron: tough competitor to Pentium 4
• Developed x86-64, their own extension to 64 bits
o Recent Years
• Intel got its act together
Leads the world in semiconductor technology
• AMD has fallen behind
Relies on external semiconductor manufacturer
Computer Systems: A Programmer’s Perspective 3rd Edition 7
INTEL’S 64-BIT HISTORY
o 2001: Intel Attempts Radical Shift from IA32 to IA64
• Totally different architecture (Itanium)
• Executes IA32 code only as legacy
• Performance disappointing
o 2003: AMD Steps in with Evolutionary Solution
• x86-64 (now called “AMD64”)
o Intel Felt Obligated to Focus on IA64
• Hard to admit mistake or that AMD is better
o 2004: Intel Announces EM64T extension to IA32
• Extended Memory 64-bit Technology
• Almost identical to x86-64!
o All but low-end x86 processors support x86-64
• But, lots of code still runs in 32-bit mode
Computer Systems: A Programmer’s Perspective 3rd Edition 8
OUR COVERAGE
o IA32
• The traditional x86
• For 15/18-213: RIP, Summer 2015
o x86-64
• The standard
linux> gcc hello.c
linux> gcc –m64 hello.c
o Presentation
• Book covers x86-64
• Web aside on IA32
• We will only cover x86-64
o Code Forms:
• Machine Code: The byte-level programs that a processor executes
• Assembly Code: A text representation of machine code
o Example ISAs:
• Intel: x86, IA32, Itanium, x86-64
• ARM: Used in almost all mobile phones
oDisassembler
objdump –d sum
• Useful tool for examining object code
• Analyzes bit pattern of series of instructions
• Produces approximate rendition of assembly code
• Can be run on either a.out (complete executable) or .o file
Disassembled:
0000000000400595 <sumstore>:
400595: 53 push %rbx
400596: 48 89 d3 mov %rdx,%rbx
400599: e8 f2 ff ff ff callq 400590 <plus>
40059e: 48 89 03 mov %rax,(%rbx)
4005a1: 5b pop %rbx
4005a2: c3 retq
Computer Systems: A Programmer’s Perspective 3rd Edition 19
ALTERNATE DISASSEMBLY
No symbols in "WINWORD.EXE".
Disassembly of section .text:
30001000 <.text>:
30001000: 55 push %ebp
30001001: 8b ec mov %esp,%ebp
30001003: 6a ffReverse engineering
push forbidden by
$0xffffffff
30001005: 68Microsoft
90 10 00 End User License
30 push Agreement
$0x30001090
3000100a: 68 91 dc 4c 30 push $0x304cdc91
source
%esi %si index
destination
%edi %di index
stack
%esp %sp
pointer
base
%ebp %bp
pointer
Memory
void swap Registers
(long *xp, long *yp) %rdi
{
long t0 = *xp; %rsi
long t1 = *yp;
*xp = t1; %rax
*yp = t0; %rdx
}
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
oSpecial Cases
(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]]
D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D]
(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]
%rdx 0xf000
%rcx 0x0100
oExample
long m12(long x) Converted to ASM by compiler:
{ leaq (%rdi,%rdi,2), %rax # t <- x+x*2
return x*12; salq $2, %rax # return t<<2
}
Computer Systems: A Programmer’s Perspective 3rd Edition 38
SOME ARITHMETIC OPERATIONS
oTwo Operand Instructions:
Format Computation
addq Src,Dest Dest = Dest + Src
subq Src,Dest Dest = Dest Src
imulq Src,Dest Dest = Dest * Src
salq Src,Dest Dest = Dest << Src Also called shlq
sarq Src,Dest Dest = Dest >> Src Arithmetic
shrq Src,Dest Dest = Dest >> Src Logical
xorq Src,Dest Dest = Dest ^ Src
andq Src,Dest Dest = Dest & Src
orq Src,Dest Dest = Dest | Src
o Arithmetic
• C compiler will figure out different instruction combinations to carry out
computation