CS 312 Lecture - 7a - Machine Level Programming-Basics
CS 312 Lecture - 7a - Machine Level Programming-Basics
Instructor:
EJ Kim
Slides are taken from the Authors and may have been
modified for the class.
Authors: Randal E. Bryant and David R. O’Hallaron
• Desktop Model
– 4 cores
– Integrated graphics
– 3.3-3.8 GHz
– 65W
• Server Model
– 8 cores
– Integrated I/O
– 2-2.6 GHz
– 45W
• x86-64
– The standard
– shark> gcc hello.c
– shark> gcc –m64 hello.c
• Presentation
– Book covers x86-64
– Web aside on IA32
– We will only cover x86-64
• Example ISAs:
– Intel: x86, IA32, Itanium, x86-64
– ARM: Used in almost all mobile phones
Programmer-Visible State
– PC: Program counter – Memory
• Address of next instruction • Byte addressable array
• Called “RIP” (x86-64) • Code and user data
– Register file • Stack to support procedures
• Heavily used program data
– Condition codes
• Store status information about most recent
arithmetic or logical operation
• Used for conditional branching
• Transfer control
– Unconditional jumps to/from procedures
– Conditional branches
• Disassembler
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
source
%esi %si index
destination
%edi %di index
stack
%esp %sp
pointer
base
%ebp %bp
pointer
movq (%rcx),%rax
movq 8(%rbp),%rdx
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
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 Perspective, Third Edition 26
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
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
movq (%rcx),%rax
movq 8(%rbp),%rdx
• Special 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]]
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 33
Carnegie Mellon
• Uses
– Computing addresses without a memory reference
• E.g., translation of p = &x[i];
– Computing arithmetic expressions of the form x + k*y
long •m12(long
long k = 1, 2, 4,x)
m12(long or 8
x) Converted to ASM by compiler:
{
{
• Example
return
return x*12;
x*12; leaq
leaq (%rdi,%rdi,2),
(%rdi,%rdi,2), %rax
%rax #
# t
t <-
<- x+x*2
x+x*2
}
} salq
salq $2,
$2, %rax
%rax #
# return
return t<<2
t<<2
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 36
Carnegie Mellon