04 Machineprog
04 Machineprog
Computer
system:
Translation
Code Time Compile Time Run Time
User
program C Assembler Hardware
in C compiler
CPU
PC Memory
Registers
¢ Registers
§ How many registers are there?
§ How wide are they?
¢ Memory
§ How do you specify a memory location?
x86
¢ Processors that implement the x86 ISA completely dominate
the server, desktop and laptop markets
¢ Evolutionary design
§ Backwards compatible up until 8086, introduced in 1978
§ Added more features as time goes on
More information
¢ References for Intel processor specifications:
§ Intel’s “automated relational knowledgebase”:
§http://ark.intel.com/
§ Wikipedia:
§ http://en.wikipedia.org/wiki/List_of_Intel_microprocessors
¢ x86-64
§ The new 64-bit x86 ISA – all lab assignments use x86-64!
Definitions
¢ Architecture: (also instruction set architecture or ISA)
The parts of a processor design that one needs to understand
to write assembly code
§ “What is directly visible to software”
¢ Microarchitecture: Implementation of the architecture
§ CSE 352
¢ Programmer-Visible State
§ PC: Program counter
§Address of next instruction
§ Called “EIP” (IA32) or “RIP” (x86-64) Stack
§ Register file
§ Heavily used program data ¢ Memory
§ Condition codes § Byte addressable array
§ Store status information about most § Code, user data, (some) OS data
recent arithmetic operation § Includes stack used to support
§ Used for conditional branching procedures (we’ll come back to that)
Autumn 2013 Machine Code & C 15
University of Washington
Object Code
Code for sum ¢ Assembler
0x401040 <sum>: § Translates .s into .o
0x55 § Binary encoding of each instruction
0x89 § Nearly-complete image of executable code
0xe5
0x8b § Missing links between code in different files
0x45 • Total of 13 bytes ¢ Linker
0x0c
0x03 • Each instruction § Resolves references between object files
0x45 1, 2, or 3 bytes and (re)locates their data
0x08 • Starts at address § Combines with static run-time libraries
0x89 0x401040
§ E.g., code for malloc, printf
0xec • Not at all obvious
0x5d § Some libraries are dynamically linked
where each instruction
0xc3 starts and ends § Linking occurs when program begins
execution
¢ Disassembler
objdump -d p
§ Useful tool for examining object code (man 1 objdump)
§ Analyzes bit pattern of series of instructions (delineates instructions)
§ Produces near-exact rendition of assembly code
§ Can be run on either p (complete executable) or p1.o / p2.o file
Autumn 2013 Machine Code & C 20
University of Washington
Alternate Disassembly
Object Disassembled
0x401040: 0x401040 <sum>: push %ebp
0x55 0x401041 <sum+1>: mov %esp,%ebp
0x89 0x401043 <sum+3>: mov 0xc(%ebp),%eax
0xe5 0x401046 <sum+6>: add 0x8(%ebp),%eax
0x8b 0x401049 <sum+9>: mov %ebp,%esp
0x45 0x40104b <sum+11>: pop %ebp
0x0c 0x40104c <sum+12>: ret
0x03
0x45
0x08 ¢ Within gdb debugger
0x89 gdb p
0xec
disassemble sum
0x5d
0xc3 (disassemble function)
x/13b sum
(examine the 13 bytes starting at sum)
No symbols in "WINWORD.EXE".
Disassembly of section .text:
30001000 <.text>:
30001000: 55 push %ebp
30001001: 8b ec mov %esp,%ebp
30001003: 6a ff push $0xffffffff
30001005: 68 90 10 00 30 push $0x30001090
3000100a: 68 91 dc 4c 30 push $0x304cdc91