Professional Documents
Culture Documents
Programming: Basics
Computer Systems Organization (Spring 2016)
CSCI-UA 201, Section 2
A Bit of History
Evolutionary design
Name
Date
Transistors
MHz
8086
1978
29K
5-10
386
1985
275K
16-33
Pentium 4F 2004
125M
2800-3800
Core i7 2008
731M
2667-3333
Notice that the speed
is not increasing as
much any more.
Desktop Model
4 cores
Integrated graphics
3.3-3.8 GHz
65W
Server Model
8 cores
Integrated I/O
2-2.6 GHz
45W
Historically
Then
Recruited top circuit designers from Digital Equipment Corp. and other downward trending
companies
Recent Years
Intel got its act together Leads the world in semiconductor technology
64-bit History
Performance disappointing
10
Definitions
Code Forms:
Example ISAs:
Programmer-Visible State
Register file
Memory
Condition codes
text
compiler: gcc - S
assembler
binary
linker
13
sumstore:
pushq
%rbx
movq %rdx, %rbx
call plus
movq %rax, (%rbx)
popq %rbx
ret
14
Data values (it does not matter if it is signed or not at the level of assembly)
we will not really go into floating point numbers at the level of assembly
15
Assembly Operations
Transfer control
Conditional branches
Very limited in what can be done in one instruction - does only one thing:
move data, single simple arithmetic operation, memory dereference.
16
Object Code
0x4005a2:
0x53
0x48
0x89
0xd3
0xe8
0xf2
0xff
0xff
0xff
0x48
0x89
0x03
0x5b
0xc3
Assembler
Translates .s into .o
Linker
Total of 14 bytes.
Each instruction
can use a different
number of bytes.
Stats at location
0x4005a2.
C Code
Assembly
t: Register %rax
dest: Register %rbx
*dest:
Memory M[%rbx]
Object Code
3-byte instruction
Stored at address 0x40059e
18
Disassembler:
objdump d sum
sumstore:
pushq
%rbx
movq %rdx, %rbx
call plus
movq %rax, (%rbx)
popq %rbx
ret
00000000004005a2 <sumstore>:
4005a2:
53
4005a3:
48 89 d3
4005a6:
e8 f2 ff ff ff
4005ab:
48 89 03
4005ae:
5b
4005af:
c3
push
mov
callq
mov
pop
retq
%rbx
%rdx,%rbx
40059d <plus>
%rax,(%rbx)
%rbx
19
Alternate Disassembly
Within gdb Debugger
gdb sum
disassemble sumstore
Disassemble procedure
x/14xb sumstore
0x4005a2 <sumstore>:
0x4005aa <sumstore+8>:
0x53
0xff
0x48
0x48
0x89
0x89
0xd3
0x03
0xe8
0x5b
0xf2
0xc3
0xff
0xff
20
BUT:
The end user license agreement for some software
forbids reverse engineering of code.
21
22
Origin
(mostly obsolete)
accumulate
counter
data
base
source index
destination index
stack pointer
base pointer
24
Moving Data
Moving Data
movq Source, Dest
Operand Types
25
Normal
(R)
Mem[Reg[R]]
movq (%rcx),%rax
Displacement
D(R)
Mem[Reg[R]+D]
movq 8(%rbp),%rdx
27
swap:
movq
movq
movq
movq
ret
swap.c
(%rdi), %rax
(%rsi), %rdx
%rdx, (%rdi)
%rax, (%rsi)
swap.s
28
Understanding swap()
void swap (long *xp, long *yp)
{
long t0 = *xp;
long t1 = *yp;
*xp = t1;
*yp = t0;
}
Register Value
%rdi
xp
%rsi
yp
%rax
t0
%rdx
t1
swap:
movq
movq
movq
movq
ret
(%rdi), %rax
(%rsi), %rdx
%rdx, (%rdi)
%rax, (%rsi)
29
Understanding swap()
swap:
movq
movq
movq
movq
ret
(%rdi), %rax
(%rsi), %rdx
%rdx, (%rdi)
%rax, (%rsi)
#
#
#
#
t0 = *xp
t1 = *yp
*xp = t1
*yp = t0
30
Understanding swap()
swap:
movq
movq
movq
movq
ret
(%rdi), %rax
(%rsi), %rdx
%rdx, (%rdi)
%rax, (%rsi)
#
#
#
#
t0 = *xp
t1 = *yp
*xp = t1
*yp = t0
31
Understanding swap()
swap:
movq
movq
movq
movq
ret
(%rdi), %rax
(%rsi), %rdx
%rdx, (%rdi)
%rax, (%rsi)
#
#
#
#
t0 = *xp
t1 = *yp
*xp = t1
*yp = t0
32
Understanding swap()
swap:
movq
movq
movq
movq
ret
(%rdi), %rax
(%rsi), %rdx
%rdx, (%rdi)
%rax, (%rsi)
#
#
#
#
t0 = *xp
t1 = *yp
*xp = t1
*yp = t0
33
Understanding swap()
swap:
movq
movq
movq
movq
ret
(%rdi), %rax
(%rsi), %rdx
%rdx, (%rdi)
%rax, (%rsi)
#
#
#
#
t0 = *xp
t1 = *yp
*xp = t1
*yp = t0
34
Normal
(R)
Mem[Reg[R]]
movq (%rcx),%rax
Displacement
D(R)
Mem[Reg[R]+D]
movq 8(%rbp),%rdx
35
Mem[Reg[Rb]+S*Reg[Ri]+ D]
Special Cases
(Rb,Ri)
D(Rb,Ri)
(Rb,Ri,S)
Mem[Reg[Rb]+Reg[Ri]]
Mem[Reg[Rb]+Reg[Ri]+D]
Mem[Reg[Rb]+S*Reg[Ri]]
36
37
38
Uses
Example
file: leaq_example.c
long m12 (long x) {
return x*12;
}
39
Arithmetic Operations
Computation
Src,Dest
Src,Dest
Src,Dest
Src,Dest
Src,Dest
Src,Dest
Src,Dest
Src,Dest
Src,Dest
Dest
Dest
Dest
Dest
Dest
Dest
Dest
Dest
Dest
=
=
=
=
=
=
=
=
=
Dest
Dest
Dest
Dest
Dest
Dest
Dest
Dest
Dest
+ Src
- Src
* Src
<< Src
>> Src
>> Src
^ Src
& Src
| Src
40
Arithmetic Operations
Computation
Dest
Dest
Dest
Dest
Dest
Dest
Dest
Dest
= Dest + 1
= Dest - 1
= Dest
= ~Dest
41
arith:
leaq (%rdi,%rsi), %rax
addq %rdx, %rax
leaq (%rsi,%rsi,2), %rcx
salq $4, %rcx
leaq 4(%rdi,%rcx), %rcx
imulq
%rcx, %rax
ret
42
arith:
leaq (%rdi,%rsi), %rax
addq %rdx, %rax
leaq (%rsi,%rsi,2), %rcx
salq $4, %rcx
leaq 4(%rdi,%rcx), %rcx
imulq
%rcx, %rax
ret
#t1
#t2
#t4
#t5
#rval
43