Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a...

22
6.S096: Assembly Daniel Kang Massachusetts Institute of Technology 1

Transcript of Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a...

Page 1: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

6.S096: Assembly

Daniel Kang

Massachusetts Institute of Technology

1

Page 2: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

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

Page 3: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

Von Neumann Architecture: CPU

Image of Von Neumann architecture removed due to copyright restrictions.

3

Page 4: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

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

Page 5: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

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

Page 6: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

� ...

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

Page 7: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

WhyWhy assasseemblmbly?y?

� DebuggingDebugging oftenoften requirrequireess readingreading assemblyassembly

� UnderstandUnderstand hohoww thingsthings wwoorrkk atat thethe mmaacchhineine levellevel

� HelpsHelps yyouou writewrite fasterfaster cocodede (even(even inin high-levelhigh-level languages)languages)

7

Page 8: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

AAssemblyssembly lalanguagesnguages (a(architectures)rchitectures)

�I x86,x86, x64x64 (desktops,(desktops, laptop,laptop, servers)servers)

I� ARMARM (phones)(phones)

�I SPSPARCARC (Sun)(Sun)

�I MIPSMIPS

�I ......

8

Page 9: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

RegistersRegisters

StoStoragerage ccllososee toto thethe CPUCPU MostMost instructionsinstructions manipulatemanipulate registersregisters

� ManipulationManipulation ofof registeregisterr contentcontent � LoadLoad andand ststoorere fromfrom registersregisters

RegisterRegister aarere fastfast,, memomemoryry accessaccess isis slosloww

9

Page 10: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

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

Page 11: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

�� �� ��

�� �� ��

�� �� ��

�� �� ��

��

xxx868686 syntaxsyntaxsyntax (A(A(AT&TT&TT&T /// GAS)GAS)GAS)

ArithmeticArithmetic instructionsinstructions OPOP a,a, bb →→ bb == bb OPOP aa e.g.e.g. addadd %edx,%edx, %eax%eax →→ %eax%eax == %eax%eax ++ %edx%edx

AssignmentAssignment instructionsinstructions OPOP a,a, bb →→ bb a== a e.g.e.g. movmov %edx,%edx, %eax%eax →→ %eax%eax == %edx%edx

ConditionCondition testingtesting OPOP a,a, bb e.g.e.g. testtest %eax,%eax, %eax%eax

ControlControl flofloww OPOP addressaddress e.g.e.g. jmpjmp 0x420e800x420e80 →→ jumpjump unconditionallyunconditionally toto 0x420e800x420e80

......

I Arithmetic instructionsI OP a, b → b = b OP aI e.g. add %edx, %eax → %eax = %eax + %edx

I Assignment instructionsI OP a, b → b = aI e.g. mov %edx, %eax → %eax = %edx

I Condition testingI OP a, bI e.g. test %eax, %eax

I Control flowI OP addressI e.g. jmp 0x420e80 → jump unconditionally to 0x420e80

I ...

11

Page 12: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

Live demos!

12

Page 13: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

Hailstone sequences: code

13

Page 14: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

.10GHz.10GHz

HailstoneHailstoneHailstone sssequencequencequenceseses::: unoptimizedunoptimizedunoptimized disassemblydisassemblydisassembly

��I CompiledCompiledCompiled withwithwith gccgccgcc versionversionversion 4.8.14.8.14.8.1

I�� CPU:CPU:CPU: Intel(R)Intel(R)Intel(R) Core(TM)Core(TM)Core(TM) i7-3612QMi7-3612QMi7-3612QM CPUCPUCPU @@@ 222

��I gccgccgcc -O0-O0-O0 hailstone.chailstone.chailstone.c -o-o-o hailstonehailstonehailstone

��I gdbgdbgdb ./hailstone./hailstone./hailstone

.10GHz

14

Page 15: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

Hailstone sequences: unoptimized disassembly

15

Page 16: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

Hailstone sequences: optimzed disassembly

I gcc -O3 hailstone.c -o hailstone

I gdb ./hailstone

16

Page 17: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

Intrinsics: code

17

Page 18: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

AFE (why?)

Intrinsics:Intrinsics: vaddvadd optimizedoptimized disassemblydisassembly

NOTE:NOTE: THISTHIS COCODEDE ISIS UNSUNS

�I gccgcc -O3-O3 intrin.cintrin.c -o-o intrinintrin

�I gdbgdb ./intrin./intrin

AFE (why?)

18

Page 19: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

Intrinsics:Intrinsics:Intrinsics: saddsaddsadd disassemblydisassemblydisassembly

�I� TTTryryry ititit yyyourselfourselfourself!!!

��I TTToooooo lalalargergerge tototo fitfitfit ononon thethethe screen!screen!screen! (why?)(why?)(why?)

19

Page 20: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

FFFurtherurtherurther questionsquestionsquestions

�I� HoHoHowww aaarerere papaparametersrametersrameters passedpassedpassed tototo functions?functions?functions?

��I HoHoHowww aaarerere valuesvaluesvalues returnedreturnedreturned fromfromfrom functions?functions?functions?

��I DoDoDo allallall instructionsinstructionsinstructions taktaktakeee thethethe samesamesame amountamountamount ofofof timtimtim

��I HoHoHowww dododoeseses cachingcachingcaching wwwooorkrkrk???

��I WhatWhatWhat aaarerere thethethe differencesdifferencesdifferences bbbetetetwwweeneeneen ARMARMARM andandand x86?x86?x86?

e?e?

e?

20

Page 21: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

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

Page 22: Lecture 3: Assembly - MIT OpenCourseWare · What is assembly? “An assembly language is a low-level programming language for a computer, or other programmable device, in which there

MIT OpenCourseWarehttp://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.