csse132 introduc0on to computer systems
play

CSSE132 Introduc0on to Computer Systems 13 : Machine - PowerPoint PPT Presentation

Adapted from Carnegie Mellon 15-213 CSSE132 Introduc0on to Computer Systems 13 : Machine level programming March 25, 2013 1 Today: Machine Level Programming Review


  1. Adapted from Carnegie Mellon 15-213 CSSE132 ¡ Introduc0on ¡to ¡Computer ¡Systems ¡ 13 ¡: ¡Machine ¡level ¡programming ¡ March ¡25, ¡2013 ¡ 1

  2. Today: ¡Machine ¡Level ¡Programming ¡ ¢ Review ¡ ¢ History ¡of ¡Intel ¡processors ¡ ¢ Assembly ¡programming ¡ § GCC ¡demo ¡ ¢ Intel ¡architecture ¡ § Data ¡sizes ¡ § Registers ¡ § Operands ¡ ¢ Data ¡movement ¡instruc0ons ¡ 2

  3. Review ¡ ¢ First ¡week ¡ § Bit, ¡bytes, ¡and ¡hexadecimal ¡ § Two’s ¡complement ¡and ¡signed ¡numbers ¡ § Boolean ¡logic ¡and ¡bitwise ¡operaHons ¡ § Integer ¡arithmeHc ¡ ¢ Second ¡week ¡ § FloaHng ¡point ¡representaHon ¡ § Boolean ¡algebra ¡ ¢ Third ¡week ¡ § K-­‑maps ¡ § 1 ¡bit ¡and ¡larger ¡adders ¡ § Flipflops ¡& ¡registers ¡ ¡ § ALU ¡design ¡ 3

  4. Review ¡ ¢ Computa0onal ¡model ¡ § CPU ¡components ¡: ¡registers, ¡memory, ¡ALU ¡ 4 Add Add Data Register # Address ALU PC Registers Address Instruction Register # Data Instruction memory memory Register # Data 4

  5. Today: ¡Machine ¡Level ¡Programming ¡ ¢ Review ¡ ¢ History ¡of ¡Intel ¡processors ¡ ¢ Assembly ¡programming ¡ § GCC ¡demo ¡ ¢ Intel ¡architecture ¡ § Data ¡sizes ¡ § Registers ¡ § Operands ¡ ¢ Data ¡movement ¡instruc0ons ¡ 5

  6. Intel ¡x86 ¡Processors ¡ ¢ Totally ¡dominate ¡laptop/desktop/server ¡market ¡ ¢ Evolu0onary ¡design ¡ § Backwards ¡compaHble ¡up ¡unHl ¡8086, ¡introduced ¡in ¡1978 ¡ § Added ¡more ¡features ¡as ¡Hme ¡goes ¡on ¡ ¢ Complex ¡instruc0on ¡set ¡computer ¡(CISC) ¡ § Many ¡different ¡instrucHons ¡with ¡many ¡different ¡formats ¡ § But, ¡only ¡small ¡subset ¡encountered ¡with ¡Linux ¡programs ¡ § Hard ¡to ¡match ¡performance ¡of ¡Reduced ¡InstrucHon ¡Set ¡Computers ¡ (RISC) ¡ § But, ¡Intel ¡has ¡done ¡just ¡that! ¡ § In ¡terms ¡of ¡speed. ¡ ¡Less ¡so ¡for ¡low ¡power. ¡ 6

  7. Intel ¡x86 ¡Processors: ¡Overview ¡ Architectures ¡ Processors ¡ X86-­‑16 ¡ 8086 ¡ ¡ 286 ¡ X86-­‑32/IA32 ¡ 386 ¡ 486 ¡ Pen0um ¡ Pen0um ¡MMX ¡ MMX ¡ ¡ Pen0um ¡III ¡ SSE ¡ ¡ Pen0um ¡4 ¡ SSE2 ¡ ¡ Pen0um ¡4E ¡ SSE3 ¡ X86-­‑64 ¡/ ¡EM64t ¡ Pen0um ¡4F ¡ 0me ¡ ¡ Core ¡2 ¡Duo ¡ SSE4 ¡ Core ¡i7 ¡ 8

  8. x86 ¡Clones: ¡Advanced ¡Micro ¡Devices ¡(AMD) ¡ ¢ Historically ¡ § AMD ¡has ¡followed ¡just ¡behind ¡Intel ¡ § A ¡liale ¡bit ¡slower, ¡a ¡lot ¡cheaper ¡ ¢ Then ¡ § Recruited ¡top ¡circuit ¡designers ¡from ¡Digital ¡Equipment ¡Corp. ¡and ¡ other ¡downward ¡trending ¡companies ¡ § Built ¡Opteron: ¡tough ¡compeHtor ¡to ¡PenHum ¡4 ¡ § Developed ¡x86-­‑64, ¡their ¡own ¡extension ¡to ¡64 ¡bits ¡ 12

  9. Intel’s ¡64-­‑Bit ¡ ¢ Intel ¡AXempted ¡Radical ¡ShiY ¡from ¡IA32 ¡to ¡IA64 ¡ § Totally ¡different ¡architecture ¡(Itanium) ¡ § Executes ¡IA32 ¡code ¡only ¡as ¡legacy ¡ § Performance ¡disappoinHng ¡ ¢ AMD ¡Stepped ¡in ¡with ¡Evolu0onary ¡Solu0on ¡ § x86-­‑64 ¡(now ¡called ¡“AMD64”) ¡ ¢ Intel ¡Felt ¡Obligated ¡to ¡Focus ¡on ¡IA64 ¡ § Hard ¡to ¡admit ¡mistake ¡or ¡that ¡AMD ¡is ¡beaer ¡ ¢ 2004: ¡Intel ¡Announces ¡EM64T ¡extension ¡to ¡IA32 ¡ § Extended ¡Memory ¡64-­‑bit ¡Technology ¡ § Almost ¡idenHcal ¡to ¡x86-­‑64! ¡ ¢ All ¡but ¡low-­‑end ¡x86 ¡processors ¡support ¡x86-­‑64 ¡ § But, ¡lots ¡of ¡code ¡sHll ¡runs ¡in ¡32-­‑bit ¡mode ¡ 13

  10. Today: ¡Machine ¡Level ¡Programming ¡ ¢ Review ¡ ¢ History ¡of ¡Intel ¡processors ¡ ¢ Assembly ¡programming ¡ § GCC ¡demo ¡ ¢ Intel ¡architecture ¡ § Data ¡sizes ¡ § Registers ¡ § Operands ¡ ¢ Data ¡movement ¡instruc0ons ¡ 15

  11. Defini0ons ¡ ¢ Architecture: ¡(also ¡instruc0on ¡set ¡architecture: ¡ISA) ¡The ¡ parts ¡of ¡a ¡processor ¡design ¡that ¡one ¡needs ¡to ¡understand ¡ to ¡write ¡assembly ¡code. ¡ ¡ § Examples: ¡ ¡instrucHon ¡set ¡specificaHon, ¡registers. ¡ ¢ Microarchitecture: ¡Implementa0on ¡of ¡the ¡architecture. ¡ § Examples: ¡cache ¡sizes ¡and ¡core ¡frequency. ¡ ¡ ¢ Example ¡ISAs ¡(Intel): ¡x86, ¡IA, ¡IPF ¡ 16

  12. Assembly ¡Programmer’s ¡View ¡ Memory ¡ CPU ¡ Addresses ¡ Registers ¡ Object ¡Code ¡ PC ¡ Data ¡ Program ¡Data ¡ OS ¡Data ¡ Condi0on ¡ InstrucHons ¡ Codes ¡ Stack ¡ ¢ Programmer-­‑Visible ¡State ¡ § PC: ¡Program ¡counter ¡ § Address ¡of ¡next ¡instrucHon ¡ § Called ¡“EIP” ¡(IA32) ¡or ¡“RIP” ¡(x86-­‑64) ¡ § Register ¡file ¡ § Memory ¡ § Heavily ¡used ¡program ¡data ¡ § Byte ¡addressable ¡array ¡ § CondiHon ¡codes ¡ § Code, ¡user ¡data, ¡(some) ¡OS ¡data ¡ § Store ¡status ¡informaHon ¡about ¡most ¡ § Includes ¡stack ¡used ¡to ¡support ¡ recent ¡arithmeHc ¡operaHon ¡ procedures ¡ § Used ¡for ¡condiHonal ¡branching ¡ 17

  13. Turning ¡C ¡into ¡Object ¡Code ¡ § Code ¡in ¡files ¡ ¡ p1.c p2.c � § Compile ¡with ¡command: ¡ ¡ gcc –O1 p1.c p2.c -o p � § Use ¡basic ¡opHmizaHons ¡( -O1 ) ¡ § Put ¡resulHng ¡binary ¡in ¡file ¡ p ¡ text ¡ C ¡program ¡( p1.c p2.c ) ¡ Compiler ¡( gcc -S ) ¡ text ¡ Asm ¡program ¡( p1.s p2.s ) ¡ Assembler ¡( gcc ¡or ¡ as ) ¡ binary ¡ Object ¡program ¡( p1.o p2.o ) ¡ Sta0c ¡libraries ¡ ( .a ) ¡ Linker ¡( gcc ¡or ld ) ¡ binary ¡ Executable ¡program ¡( p ) ¡ 18

  14. Compiling ¡Into ¡Assembly ¡ Generated ¡IA32 ¡Assembly ¡ C ¡Code ¡ ¡ sum: ¡ int sum(int x, int y) pushl %ebp { movl %esp,%ebp int t = x+y; movl 12(%ebp),%eax return t; addl 8(%ebp),%eax } popl %ebp ret Some ¡compilers ¡use ¡ instruc0on ¡“ leave ” ¡ Obtain ¡with ¡command ¡ /usr/local/bin/gcc -m32 –O1 -S code.c Produces ¡file ¡ code.s 19

  15. Assembly ¡Characteris0cs: ¡Data ¡Types ¡ ¢ “Integer” ¡data ¡of ¡1, ¡2, ¡or ¡4 ¡bytes ¡ § Data ¡values ¡ § Addresses ¡(untyped ¡pointers) ¡ § Word ¡size ¡is ¡2 ¡bytes ¡(16 ¡bits) ¡ ¢ Floa0ng ¡point ¡data ¡of ¡4, ¡8, ¡or ¡10 ¡bytes ¡ ¢ No ¡aggregate ¡types ¡such ¡as ¡arrays ¡or ¡structures ¡ § Just ¡conHguously ¡allocated ¡bytes ¡in ¡memory ¡ 20

  16. Assembly ¡Characteris0cs: ¡Opera0ons ¡ ¢ Perform ¡arithme0c ¡func0on ¡on ¡register ¡or ¡memory ¡data ¡ ¢ Transfer ¡data ¡between ¡memory ¡and ¡register ¡ § Load ¡data ¡from ¡memory ¡into ¡register ¡ § Store ¡register ¡data ¡into ¡memory ¡ ¢ Transfer ¡control ¡ § UncondiHonal ¡jumps ¡to/from ¡procedures ¡ § CondiHonal ¡branches ¡ 21

  17. Object ¡Code ¡ Code ¡for ¡ sum ¡ ¢ Assembler ¡ ¡ 0x401040 <sum>: § Translates ¡ .s ¡into ¡ .o 0x55 § Binary ¡encoding ¡of ¡each ¡instrucHon ¡ 0x89 0xe5 § Nearly-­‑complete ¡image ¡of ¡executable ¡code ¡ 0x8b § Missing ¡linkages ¡between ¡code ¡in ¡different ¡ 0x45 files ¡ 0x0c ¢ Linker ¡ 0x03 0x45 § Resolves ¡references ¡between ¡files ¡ 0x08 • Total ¡of ¡11 ¡bytes ¡ § Combines ¡with ¡staHc ¡run-­‑Hme ¡libraries ¡ 0x5d • Each ¡instruc0on ¡ 0xc3 § E.g., ¡code ¡for ¡ malloc , ¡ printf 1, ¡2, ¡or ¡3 ¡bytes ¡ § Some ¡libraries ¡are ¡ dynamically ¡linked ¡ • Starts ¡at ¡address ¡ § Linking ¡occurs ¡when ¡program ¡begins ¡ 0x401040 execuHon ¡ 22

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend