Download Machine-Level Programming I Basics, Lecture Slide - Computer Science and more Slides Computer System Design and Architecture in PDF only on Docsity!
Machine-ĀāLevel Programming I: Basics
15-Āā213/18-Āā243: Introduc3on to Computer Systems 4 th Lecture, Sep. 2, 2010 Instructors: Randy Bryant and Dave OāHallaron
Today: Machine Programming I: Basics
ļ¢ History of Intel processors and architectures
ļ¢ C, assembly, machine code
ļ¢ Assembly Basics: Registers, operands, move
ļ¢ Intro to x86-Āā
Intel x86 EvoluJon: Milestones
Name Date Transistors MHz
ļ¢ 8086 1978 29K 5-Āā
ļ§ First 16-Āābit processor. Basis for IBM PC & DOS
ļ§ 1MB address space
ļ¢ 386 1985 275K 16-Āā
ļ§ First 32 bit processor , referred to as IA
ļ§ Added āflat addressingā
ļ§ Capable of running Unix
ļ§ 32-Āābit Linux/gcc uses no instruc3ons introduced in later models
ļ¢ PenJum 4F 2004 125M 2800-Āā
ļ§ First 64-Āābit processor, referred to as x86-Āā
ļ¢ Core i7 2008 731M 2667-Āā
ļ§ Our shark machines
Intel x86 Processors: Overview
X86-Āā64 / EM64t
X86-Āā32/IA
X86-Āā16 8086
286 386 486 PenJum PenJum MMX PenJum III PenJum 4 PenJum 4E PenJum 4F Core 2 Duo Core i
IA: o[en redefined as latest Intel architecture
Jme
Architectures Processors
MMX SSE SSE SSE SSE
More InformaJon
ļ¢ Intel processors (Wikipedia)
ļ¢ Intel microarchitectures
New Species: ia64, then IPF, then Itanium,ā¦
Name Date Transistors
ļ¢ Itanium 2001 10M
ļ§ First shot at 64-Āābit architecture: first called IA
ļ§ Radically new instruc3on set designed for high performance
ļ§ Can run exis3ng IA32 programs
ļ§ On-Āāboard āx86 engineā
ļ§ Joint project with Hewlef-ĀāPackard
ļ¢ Itanium 2 2002 221M
ļ§ Big performance boost
ļ¢ Itanium 2 Dual-ĀāCore 2006 1.7B
ļ¢ Itanium has not taken off in marketplace
ļ§ Lack of backward compa3bility, no good compiler support, Pen3um
4 got too good
Intelās 64-ĀāBit
ļ¢ Intel Adempted Radical Shi[ from IA32 to IA
ļ§ Totally different architecture (Itanium)
ļ§ Executes IA32 code only as legacy
ļ§ Performance disappoin3ng
ļ¢ AMD Stepped in with EvoluJonary SoluJon
ļ§ x86-Āā64 (now called āAMD64ā)
ļ¢ Intel Felt Obligated to Focus on IA
ļ§ Hard to admit mistake or that AMD is befer
ļ¢ 2004: Intel Announces EM64T extension to IA
ļ§ Extended Memory 64-Āābit Technology
ļ§ Almost iden3cal to x86-Āā64!
ļ¢ All but low-Āāend x86 processors support x86-Āā
ļ§ But, lots of code s3ll runs in 32-Āābit mode
Our Coverage
ļ¢ IA
ļ§ The tradi3onal x
ļ¢ x86-Āā64/EM64T
ļ§ The emerging standard
ļ¢ PresentaJon
ļ§ Book presents IA32 in Sec3ons 3.1ā3.
ļ§ Covers x86-Āā64 in 3.
ļ§ We will cover both simultaneously
ļ§ Some labs will be based on x86-Āā64, others on IA
DefiniJons
ļ¢ Architecture: (also instrucJon set architecture: ISA) The
parts of a processor design that one needs to understand
to write assembly code.
ļ§ Examples: instruc3on set specifica3on, registers.
ļ¢ Microarchitecture: ImplementaJon of the architecture.
ļ§ Examples: cache sizes and core frequency.
ļ¢ Example ISAs (Intel): x86, IA, IPF
CPU
Assembly Programmerās View
ļ¢ Programmer-ĀāVisible State ļ§ PC: Program counter ļ§ Address of next instruc3on ļ§ Called āEIPā (IA32) or āRIPā (x86-Āā64) ļ§ Register file ļ§ Heavily used program data ļ§ Condi3on codes ļ§ Store status informa3on about most recent arithme3c opera3on ļ§ Used for condi3onal branching
PC
Registers
Memory
Object Code Program Data OS Data Addresses Data Instruc3ons
Stack
CondiJon
Codes
ļ§ Memory
ļ§ Byte addressable array ļ§ Code, user data, (some) OS data ļ§ Includes stack used to support procedures
Compiling Into Assembly
C Code
int sum(int x, int y) { int t = x+y; return t; }
Generated IA32 Assembly
sum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax popl %ebp ret
Obtain with command
/usr/local/bin/gcc āO1 -S code.c
Produces file code.s
Some compilers use
instrucJon āleaveā
Assembly CharacterisJcs: Data Types
ļ¢ āIntegerā data of 1, 2, or 4 bytes
ļ§ Data values
ļ§ Addresses (untyped pointers)
ļ¢ FloaJng point data of 4, 8, or 10 bytes
ļ¢ No aggregate types such as arrays or structures
ļ§ Just con3guously allocated bytes in memory
Code for sum
0x401040 : 0x 0x 0xe 0x8b 0x 0x0c 0x 0x 0x 0x5d 0xc
Object Code
ļ¢ Assembler
ļ§ Translates .s into .o
ļ§ Binary encoding of each instruc3on
ļ§ Nearly-Āācomplete image of executable code
ļ§ Missing linkages between code in different
files
ļ¢ Linker
ļ§ Resolves references between files
ļ§ Combines with sta3c run-Āā3me libraries
ļ§ E.g., code for malloc, printf
ļ§ Some libraries are dynamically linked
ļ§ Linking occurs when program begins execu3on
- Total of 11 bytes
- Each instrucJon 1, 2, or 3 bytes
- Starts at address 0x
Machine InstrucJon Example
ļ¢ C Code
ļ§ Add two signed integers
ļ¢ Assembly
ļ§ Add 2 4-Āābyte integers
ļ§ āLongā words in GCC parlance ļ§ Same instruc3on whether signed or unsigned
ļ§ Operands:
x: Register %eax y: Memory M[%ebp+8] t: Register %eax
- Return func3on value in %eax
ļ¢ Object Code
ļ§ 3-Āābyte instruc3on
ļ§ Stored at address 0x80483ca
*int t = x+y; addl 8(%ebp),%eax 0x80483ca: 03 45 08 Similar to expression: x += y More precisely: int eax; int ebp; eax += ebp[2]