Sie sind auf Seite 1von 22

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 architectures 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 architectures machine code instructions.

5
Why high-level languages?

Stronger abstractions
e.g. object oriented programming
e.g. C, C++, Java, Python
Increased portability
e.g. interpreted languages
e.g. Java, Python
Faster development cycles
...

6
Why assembly?
y?

Debugging often requires reading assembly


Understand how things work at the machine level
Helps you write faster code (even in high-level languages)

7
Assembly languages (architectures)
rchitectures)


I servers)

x86, x64 (desktops, laptop, servers)



I ARM (phones)
(phones)


I SPARC
SPARC (Sun)
(Sun)


I MIPS


I ...

8
Registers

Registers

Storage close to the CPU



Most instructions manipulate registers
Manipulation of register content
Load and store from registers
Register are fast, memory access is slow

9
x86 syntax (AT&T / GAS)

I Registers
I 8 registers in x86 (32-bit)
I 16 registers in x64 (64-bit)
I 16-bit: ax, bx, ...
I 32-bit: eax, ebx, ...
I 64-bit: rax, rbx, ..., r8, r9, ...
I Referenced by %REGISTER
I Constants: $0, $1, $0x20 (32), ...

10
x86 syntax (AT&T / GAS)
GAS)


I Arithmetic instructions
I
OP a, b b = b OP a

I e.g. add %edx, %eax %eax = %eax + %edx

I Assignment instructions
I
OP a, b b = a

I e.g. mov %edx, %eax %eax = %edx

I Condition testing
I
OP a, b

I e.g. test %eax, %eax

I Control o w
flow
I
OP address

I e.g. jmp 0x420e80 jump unconditionally to 0x420e80

I ...

11
Live demos!

12
Hailstone sequences: code

13
disassembly

Hailstone sequences: unoptimized disassembly


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

14
Hailstone sequences: unoptimized disassembly

15
Hailstone sequences: optimzed disassembly
I gcc -O3 hailstone.c -o hailstone
I gdb ./hailstone

16
Intrinsics: code

17
disassembly

Intrinsics: vadd optimized disassembly

NOTE: THIS CODE IS UNSAFE (why?)


I gcc -O3 intrin.c -o intrin

I gdb ./intrin

18
Intrinsics: sadd disassembly


I Try it yourself !


I Too large to fit
t on the screen! (why?)

19
Further questions
questions


I Ho
How functions?

w are parameters passed to functions?



I How are values returned from functions?


I Do all instructions take the same amount of time?


I How
How does caching work?

I
What are the differences
dierences between ARM and x86?

20
Further material

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

21
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.

Das könnte Ihnen auch gefallen