Carnegie Mellon
1 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Machine-Level Programming I: Basics 15-213/18-213: - - PowerPoint PPT Presentation
Carnegie Mellon Machine-Level Programming I: Basics 15-213/18-213: Introducon to Computer Systems 5 th Lecture, Sep. 15, 2015 Instructors: Randal
Carnegie Mellon
1 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Carnegie Mellon
2 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ History ¡of ¡Intel ¡processors ¡and ¡architectures ¡ ¢ C, ¡assembly, ¡machine ¡code ¡ ¢ Assembly ¡Basics: ¡Registers, ¡operands, ¡move ¡ ¢ Arithmec ¡& ¡logical ¡operaons ¡
Carnegie Mellon
3 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Dominate ¡laptop/desktop/server ¡market ¡ ¢ Evoluonary ¡design ¡
¢ Complex ¡instrucon ¡set ¡computer ¡(CISC) ¡
§ But, ¡only ¡small ¡subset ¡encountered ¡with ¡Linux ¡programs ¡
§ In ¡terms ¡of ¡speed. ¡ ¡Less ¡so ¡for ¡low ¡power. ¡
Carnegie Mellon
4 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ 8086
¢ 386
¢ Penum ¡4E
¢ Core ¡2
¢ Core ¡i7
Carnegie Mellon
5 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Machine ¡Evoluon ¡
¢ Added ¡Features ¡
Carnegie Mellon
6 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Desktop ¡Model ¡
¢ Server ¡Model ¡
Carnegie Mellon
7 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Historically ¡
¢ Then ¡
¢ ¡Recent ¡Years ¡
§ Leads ¡the ¡world ¡in ¡semiconductor ¡technology ¡
§ Relies ¡on ¡external ¡semiconductor ¡manufacturer ¡
Carnegie Mellon
8 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ 2001: ¡Intel ¡Aempts ¡Radical ¡Shi ¡from ¡IA32 ¡to ¡IA64 ¡
¢ 2003: ¡AMD ¡Steps ¡in ¡with ¡Evoluonary ¡Soluon ¡
¢ Intel ¡Felt ¡Obligated ¡to ¡Focus ¡on ¡IA64 ¡
¢ 2004: ¡Intel ¡Announces ¡EM64T ¡extension ¡to ¡IA32 ¡
¢ All ¡but ¡low-‑end ¡x86 ¡processors ¡support ¡x86-‑64 ¡
Carnegie Mellon
9 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ IA32 ¡
¢ x86-‑64 ¡
¢ Presentaon ¡
Carnegie Mellon
10 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ History ¡of ¡Intel ¡processors ¡and ¡architectures ¡ ¢ C, ¡assembly, ¡machine ¡code ¡ ¢ Assembly ¡Basics: ¡Registers, ¡operands, ¡move ¡ ¢ Arithmec ¡& ¡logical ¡operaons ¡
Carnegie Mellon
11 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Architecture: ¡(also ¡ISA: ¡instrucon ¡set ¡architecture) ¡The ¡
¢ Microarchitecture: ¡Implementaon ¡of ¡the ¡architecture. ¡
¢ Code ¡Forms: ¡
¢ Example ¡ISAs: ¡ ¡
Carnegie Mellon
12 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
§ Address ¡of ¡next ¡instrucon ¡ § Called ¡“RIP” ¡(x86-‑64) ¡
§ Heavily ¡used ¡program ¡data ¡
§ Store ¡status ¡informaon ¡about ¡most ¡
§ Used ¡for ¡condional ¡branching ¡
§ Byte ¡addressable ¡array ¡ § Code ¡and ¡user ¡data ¡ § Stack ¡to ¡support ¡procedures ¡
Carnegie Mellon
13 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
§ Use ¡basic ¡opmizaons ¡(-Og) ¡[New ¡to ¡recent ¡versions ¡of ¡GCC] ¡ § Put ¡resulng ¡binary ¡in ¡file ¡p ¡
Carnegie Mellon
14 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Carnegie Mellon
15 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ “Integer” ¡data ¡of ¡1, ¡2, ¡4, ¡or ¡8 ¡bytes ¡
¢ Floang ¡point ¡data ¡of ¡4, ¡8, ¡or ¡10 ¡bytes ¡ ¢ Code: ¡Byte ¡sequences ¡encoding ¡series ¡of ¡instrucons ¡ ¢ No ¡aggregate ¡types ¡such ¡as ¡arrays ¡or ¡structures ¡
Carnegie Mellon
16 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Perform ¡arithmec ¡funcon ¡on ¡register ¡or ¡memory ¡data ¡ ¢ Transfer ¡data ¡between ¡memory ¡and ¡register ¡
¢ Transfer ¡control ¡
Carnegie Mellon
17 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Assembler ¡
¢ Linker ¡
§ E.g., ¡code ¡for ¡malloc, ¡printf
§ Linking ¡occurs ¡when ¡program ¡begins ¡
Carnegie Mellon
18 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ C ¡Code ¡
¢ Assembly ¡
§ Quad ¡words ¡in ¡x86-‑64 ¡parlance ¡
¢ Object ¡Code ¡
Carnegie Mellon
19 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Disassembler ¡
Carnegie Mellon
20 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Within ¡gdb ¡Debugger ¡
Carnegie Mellon
21 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Anything ¡that ¡can ¡be ¡interpreted ¡as ¡executable ¡code ¡ ¢ Disassembler ¡examines ¡bytes ¡and ¡reconstructs ¡assembly ¡source ¡
Carnegie Mellon
22 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ History ¡of ¡Intel ¡processors ¡and ¡architectures ¡ ¢ C, ¡assembly, ¡machine ¡code ¡ ¢ Assembly ¡Basics: ¡Registers, ¡operands, ¡move ¡ ¢ Arithmec ¡& ¡logical ¡operaons ¡
Carnegie Mellon
23 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Carnegie Mellon
24 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
accumulate counter data base source index destination index
Carnegie Mellon
25 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Moving ¡Data ¡
¢ Operand ¡Types ¡
§ Example: ¡$0x400, ¡$-533 ¡ § Like ¡C ¡constant, ¡but ¡prefixed ¡with ¡‘$’ § Encoded ¡with ¡1, ¡2, ¡or ¡4 ¡bytes ¡
§ Example: ¡%rax, %r13 § But ¡%rsp reserved ¡for ¡special ¡use ¡ § Others ¡have ¡special ¡uses ¡for ¡parcular ¡instrucons ¡
§ Simplest ¡example: ¡(%rax) § Various ¡other ¡“address ¡modes” ¡
Carnegie Mellon
26 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Carnegie Mellon
27 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Normal
¢ Displacement
Carnegie Mellon
28 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Carnegie Mellon
29 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Carnegie Mellon
30 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Address ¡
Carnegie Mellon
31 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Address ¡
Carnegie Mellon
32 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Address ¡
Carnegie Mellon
33 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Address ¡
Carnegie Mellon
34 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Address ¡
Carnegie Mellon
35 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Normal
¢ Displacement
Carnegie Mellon
36 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ Most ¡General ¡Form ¡
¢ Special ¡Cases ¡
Carnegie Mellon
37 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Carnegie Mellon
Carnegie Mellon
38 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ History ¡of ¡Intel ¡processors ¡and ¡architectures ¡ ¢ C, ¡assembly, ¡machine ¡code ¡ ¢ Assembly ¡Basics: ¡Registers, ¡operands, ¡move ¡ ¢ Arithmec ¡& ¡logical ¡operaons ¡
Carnegie Mellon
39 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Carnegie Mellon
¢ leaq ¡Src, ¡Dst ¡
¢ Uses ¡
§ E.g., ¡translaon ¡of ¡p = &x[i]; ¡
§ k ¡= ¡1, ¡2, ¡4, ¡or ¡8 ¡
¢ Example ¡
Carnegie Mellon
40 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Carnegie Mellon
¢ Two ¡Operand ¡Instrucons: ¡
¢ Watch ¡out ¡for ¡argument ¡order! ¡ ¢ No ¡disncon ¡between ¡signed ¡and ¡unsigned ¡int ¡(why?) ¡
Carnegie Mellon
41 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Carnegie Mellon
¢ One ¡Operand ¡Instrucons ¡
¢ See ¡book ¡for ¡more ¡instrucons ¡
Carnegie Mellon
42 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Carnegie Mellon
§
Carnegie Mellon
43 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
Carnegie Mellon
Carnegie Mellon
44 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspecve, ¡Third ¡Edion ¡
¢ History ¡of ¡Intel ¡processors ¡and ¡architectures ¡
¢ C, ¡assembly, ¡machine ¡code ¡
¢ Assembly ¡Basics: ¡Registers, ¡operands, ¡move ¡
¢ Arithmec ¡