1
CSSE132 ¡ Introduc0on ¡to ¡Computer ¡Systems ¡
13 ¡: ¡Machine ¡level ¡programming ¡ March ¡25, ¡2013 ¡
Adapted from Carnegie Mellon 15-213
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
2
¢ Review ¡ ¢ History ¡of ¡Intel ¡processors ¡ ¢ Assembly ¡programming ¡
¢ Intel ¡architecture ¡
¢ Data ¡movement ¡instruc0ons ¡
3
¢ First ¡week ¡
¢ Second ¡week ¡
¢ Third ¡week ¡
4
¢ Computa0onal ¡model ¡
Data PC Address Instruction Instruction memory Registers ALU Address Data Data memory Add Add 4 Register # Register # Register #
5
¢ Review ¡ ¢ History ¡of ¡Intel ¡processors ¡ ¢ Assembly ¡programming ¡
¢ Intel ¡architecture ¡
¢ Data ¡movement ¡instruc0ons ¡
6
¢ Totally ¡dominate ¡laptop/desktop/server ¡market ¡ ¢ Evolu0onary ¡design ¡
¢ Complex ¡instruc0on ¡set ¡computer ¡(CISC) ¡
§ But, ¡only ¡small ¡subset ¡encountered ¡with ¡Linux ¡programs ¡
§ In ¡terms ¡of ¡speed. ¡ ¡Less ¡so ¡for ¡low ¡power. ¡
8
MMX ¡ ¡ SSE ¡ ¡ SSE2 ¡ ¡ SSE3 ¡ SSE4 ¡
12
¢ Historically ¡
¢ Then ¡
13
¢ Intel ¡AXempted ¡Radical ¡ShiY ¡from ¡IA32 ¡to ¡IA64 ¡
¢ AMD ¡Stepped ¡in ¡with ¡Evolu0onary ¡Solu0on ¡
¢ Intel ¡Felt ¡Obligated ¡to ¡Focus ¡on ¡IA64 ¡
¢ 2004: ¡Intel ¡Announces ¡EM64T ¡extension ¡to ¡IA32 ¡
¢ All ¡but ¡low-‑end ¡x86 ¡processors ¡support ¡x86-‑64 ¡
15
¢ Review ¡ ¢ History ¡of ¡Intel ¡processors ¡ ¢ Assembly ¡programming ¡
¢ Intel ¡architecture ¡
¢ Data ¡movement ¡instruc0ons ¡
16
¢ Architecture: ¡(also ¡instruc0on ¡set ¡architecture: ¡ISA) ¡The ¡
¢ Microarchitecture: ¡Implementa0on ¡of ¡the ¡architecture. ¡
¢ Example ¡ISAs ¡(Intel): ¡x86, ¡IA, ¡IPF ¡
17
¢ Programmer-‑Visible ¡State ¡
§ Address ¡of ¡next ¡instrucHon ¡ § Called ¡“EIP” ¡(IA32) ¡or ¡“RIP” ¡(x86-‑64) ¡
§ Heavily ¡used ¡program ¡data ¡
§ Store ¡status ¡informaHon ¡about ¡most ¡
recent ¡arithmeHc ¡operaHon ¡
§ Used ¡for ¡condiHonal ¡branching ¡
§ Byte ¡addressable ¡array ¡ § Code, ¡user ¡data, ¡(some) ¡OS ¡data ¡ § Includes ¡stack ¡used ¡to ¡support ¡
procedures ¡
18
§ Use ¡basic ¡opHmizaHons ¡(-O1) ¡ § Put ¡resulHng ¡binary ¡in ¡file ¡p ¡
19
20
¢ “Integer” ¡data ¡of ¡1, ¡2, ¡or ¡4 ¡bytes ¡
¢ Floa0ng ¡point ¡data ¡of ¡4, ¡8, ¡or ¡10 ¡bytes ¡ ¢ No ¡aggregate ¡types ¡such ¡as ¡arrays ¡or ¡structures ¡
21
¢ Perform ¡arithme0c ¡func0on ¡on ¡register ¡or ¡memory ¡data ¡ ¢ Transfer ¡data ¡between ¡memory ¡and ¡register ¡
¢ Transfer ¡control ¡
22
¢ Assembler ¡
¢ Linker ¡
§ E.g., ¡code ¡for ¡malloc, ¡printf
§ Linking ¡occurs ¡when ¡program ¡begins ¡
23
¢ C ¡Code ¡
¢ Assembly ¡
§ “Long” ¡words ¡in ¡GCC ¡parlance ¡ § Same ¡instrucHon ¡whether ¡signed ¡
¢ Object ¡Code ¡
24
¢ Disassembler ¡
25
¢ Within ¡gdb ¡Debugger ¡
26
¢ Anything ¡that ¡can ¡be ¡interpreted ¡as ¡executable ¡code ¡ ¢ Disassembler ¡examines ¡bytes ¡and ¡reconstructs ¡assembly ¡source ¡
27
¢ Generate ¡assembly ¡code ¡
¢ Generate ¡object ¡code ¡
¢ Using ¡a ¡debugger ¡
§ disassemble ¡sum
§ x/17xb ¡sum
§ quit
¢ Conver0ng ¡to ¡object ¡code ¡
28
¢ Review ¡ ¢ History ¡of ¡Intel ¡processors ¡ ¢ Assembly ¡programming ¡
¢ Intel ¡architecture ¡
¢ Data ¡movement ¡instruc0ons ¡
29
30
accumulate counter data base source index destination index
31
¢ Moving ¡Data ¡
¢ Operand ¡Types ¡
§ Example: ¡$0x400, ¡$-533 ¡ § Like ¡C ¡constant, ¡but ¡prefixed ¡with ¡‘$’ § Encoded ¡with ¡1, ¡2, ¡or ¡4 ¡bytes ¡
§ Example: ¡%eax, %edx § But ¡%esp and ¡%ebp reserved ¡for ¡special ¡use ¡ § Others ¡have ¡special ¡uses ¡for ¡parHcular ¡instrucHons ¡
§ Simplest ¡example: ¡(%eax) § Various ¡other ¡“address ¡modes” ¡
32
33
¢ Normal
¢ Displacement
34
35
36
37
38
39
40
41
42
43
44
¢ Most ¡General ¡Form ¡
§ Unlikely ¡you’d ¡use ¡%ebp, ¡either ¡
¢ Special ¡Cases ¡
45
46
Instruction Effect Description mov S, D D ← S Move movb Move byte movw Move word movl Move double word movs S, D D ← SignExtend(S) Move with sign extension movsbw Move sign-extended byte to word movsbl Move sign-extended byte to double word movswl Move sign-extended word to double word movz S, D D ← ZeroExtend(S) Move with zero extension movzbw Move zero-extended byte to word movzbl Move zero-extended byte to double word movzwl Move zero-extended word to double word pushl S R[%esp] ← R[%esp] − 4; Push double word M[R[%esp]] ← S popl D D ← M[R[%esp]]; Pop double word R[%esp] ← R[%esp] + 4