04-Machineprog 16sp
04-Machineprog 16sp
Computer
system:
1
Machine Code & C Spring 2016
2
Machine Code & C Spring 2016
Translation
User
program C Assembler Hardware
in C compiler
3
Machine Code & C Spring 2016
Intel Pentium 4
C Language
Intel Core 2
Program
A
GCC x86-64 Intel Core i7
AMD Opteron
Program
B
AMD Athlon
Clang
Your
program
ARMv8 ARM Cortex-A53
(AArch64/A64)
Apple A7
4
Machine Code & C Spring 2016
CPU
PC
Memory
Registers
5
Machine Code & C Spring 2016
¢ Registers
§ How many registers are there?
§ How wide are they?
¢ Memory
§ How do you specify a memory location?
6
Machine Code & C Spring 2016
X86 ISA
¢ 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
7
Machine Code & C Spring 2016
¢ Added Features
§ Instructions to support multimedia operations
§ Parallel operations on 1, 2, and 4-byte data (“SIMD”)
§ Instructions to enable more efficient conditional operations
§ Hardware support for virtualization (virtual machines)
§ More cores!
9
Machine Code & C Spring 2016
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
10
Machine Code & C Spring 2016
11
Machine Code & C Spring 2016
12
Machine Code & C Spring 2016
13
Machine Code & C Spring 2016
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/EE 469, 470
¢ Number of registers?
¢ How about CPU frequency?
¢ Cache size? Memory size?
14
Machine Code & C Spring 2016
¢ Programmer-Visible State
§ PC: Program counter
§ Address of next instruction
§ Called “RIP” (x86-64)
§ Named registers
§ Heavily used program data
§ Memory
§ Together, called “register file”
§ Byte addressable array
§ Condition codes § Code and user data
§ Store status information about most § Includes Stack (for supporting
recent arithmetic operation procedures, we’ll come back to that)
§ Used for conditional branching 15
Machine Code & C Spring 2016
Produced by command:
gcc –Og –S sum.c
Generates file: sum.s
Warning: You may get different results with other versions of gcc and
different compiler settings.
https://godbolt.org/g/5hQk8a 17
Machine Code & C Spring 2016
18
Machine Code & C Spring 2016
Object Code
Code for sumstore ¢ Assembler
§ Translates .s into .o
0x00400536 <sumstore>:
§ Binary encoding of each instruction
0x48
0x01 § Nearly-complete image of executable code
0xfe § Missing linkages between code in different
0x48 files
0x89
0x32 ¢ Linker
0xc3 § Resolves references between files
§ Combines with static run-time libraries
• Total of 7 bytes § E.g., code for malloc, printf
• Each instruction § Some libraries are dynamically linked
here 1 or 3 bytes § Linking occurs when program begins
execution
• Starts at address
0x00400536
19
Machine Code & C Spring 2016
¢ Disassembler
objdump –d sum
§ Useful tool for examining object code (Try man 1 objdump)
§ Analyzes bit pattern of series of instructions
§ Produces approximate rendition of assembly code
§ Can be run on either a.out (complete executable) or .o file
20
Machine Code & C Spring 2016
No symbols in "WINWORD.EXE".
Disassembly of section .text:
30001000 <.text>:
30001000: 55 push %ebp
30001001: 8b ecReverse engineering
mov forbidden by
%esp,%ebp
30001003: 6a ff MicrosoftpushEnd$0xffffffff
User License
30001005: 68 90 10 00 30 push $0x30001090
Agreement
3000100a: 68 91 dc 4c 30 push $0x304cdc91