6.S096: Assembly Daniel Kang Massachusetts Institute of Technology

advertisement
6.S096: Assembly
Daniel Kang
Massachusetts Institute of Technology
1
A typical computer
Fig. 01-06 from Englander, Irv. The Architecture of Computer Hardware and System
Software: An Information Technology pproach. 2nd edition. John Wiley & Sons, Inc.
2
Von Neumann Architecture: CPU
Image of Von Neumann architecture removed due to copyright restrictions.
3
What is assembly?
“An assembly language is a low-level programming language for a
computer, or other programmable device, in which there is a very
strong (generally one-to-one) correspondence between the
language and the architecture’s machine code instructions.”
4
What is assembly?
“An assembly language is a low-level programming language for a
computer, or other programmable device, in which there is a very
strong (generally one-to-one) correspondence between the
language and the architecture’s machine code instructions.”
5
Why high-level languages?
�
Stronger abstractions
�
�
�
Increased portability
�
�
6
e.g. object oriented programming
e.g. C, C++, Java, Python
e.g. interpreted languages
e.g. Java, Python
�
Faster development cycles
�
...
Why assembly?
y?
7
�
Debugging often requires reading assembly
�
Understand how things work at the machine level
�
Helps you write faster code (even in high-level languages)
Assembly languages (architectures)
rchitectures)
8
I
�
x86, x64 (desktops, laptop, servers)
servers)
�
I
ARM (phones)
(phones)
�
I
SPARC
SP
ARC (Sun)
(Sun)
�
I
MIPS
�
I
...
Registers
Registers
�
�
Storage close to the CPU
Most instructions manipulate registers
�
�
�
9
Manipulation of register content
Load and store from registers
Register are fast, memory access is slow
x86 syntax (AT&T / GAS)
I
Registers
I
8 registers in x86 (32-bit)
16 registers in x64 (64-bit)
I
I
I
I
I
I
10
16-bit: ax, bx, ...
32-bit: eax, ebx, ...
64-bit: rax, rbx, ..., r8, r9, ...
Referenced by %REGISTER
Constants: $0, $1, $0x20 (32), ...
x86 syntax (AT&T / GAS)
GAS)
�
I
Arithmetic instructions
�
I
�
I
I
�
Assignment instructions
�
I
�
I
�
I
�
I
�
I
11
OP a, b
e.g. test %eax, %eax
w
Control flo
flow
�
I
�
I
OP a, b → b = a
e.g. mov %edx, %eax → %eax = %edx
Condition testing
�
I
�
I
OP a, b → b = b OP a
e.g. add %edx, %eax → %eax = %eax + %edx
...
OP address
e.g. jmp 0x420e80 → jump unconditionally to 0x420e80
Live demos!
12
Hailstone sequences: code
13
Hailstone sequences: unoptimized disassembly
disassembly
14
I
�
Compiled with gcc version 4.8.1
�
I
CPU: Intel(R) Core(TM) i7-3612QM CPU @ 2.10GHz
�
I
gcc -O0 hailstone.c -o hailstone
I
�
gdb ./hailstone
Hailstone sequences: unoptimized disassembly
15
Hailstone sequences: optimzed disassembly
16
I
gcc -O3 hailstone.c -o hailstone
I
gdb ./hailstone
Intrinsics: code
17
Intrinsics: vadd optimized disassembly
disassembly
NOTE: THIS CODE IS UNSAFE
18
I
�
gcc -O3 intrin.c -o intrin
I
�
gdb ./intrin
(why?)
Intrinsics: sadd disassembly
19
I
�
Try it yourself !
I
�
fit on the screen! (why?)
Too large to fit
Further questions
questions
20
I
�
How
Ho
w are parameters passed to functions?
functions?
I
�
How are values returned from functions?
�
I
Do all instructions take the same amount of time?
�
I
How
w does caching work?
Ho
I
�
What are the differences
differences between ARM and x86?
Further material
21
I
Intel syntax
I
External assembly: yasm, masm, etc.
I
Intel manuals: http:
//www.intel.com/content/www/us/en/processors/
architectures-software-developer-manuals.html
I
Classes: 6.004, 6.033, 6.172
MIT OpenCourseWare
http://ocw.mit.edu
6.S096 Effective Programming in C and C++
IAP 2014
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
Download