CS230 System Programming

Download Report

Transcript CS230 System Programming

CS230
SYSTEM PROGRAMMING
Junehwa Song
1
Organization of Computers
A Programmer’s View
2
Contents
Simple View of Computers
C/Assembly
Intel Architecture
3
Simple View of Computers
I/O
CPU
Memory
CPU
MEMORY
I/O
4
Components of Computer System
Address Bus
Data Bus
MEMORY
CPU
Control and Status Bus
5
Memory Addresses
00000
101101100110
01110
11111 = 2k - 1
Memory is a place to store
bits
A word is a fixed number of
bits (eg, 32) at an address
 also fixed no. of bits
Addresses are naturally
represented as unsigned
numbers
What can be stored?
 numbers
 addresses
 representations of things
6
What else to Represent?
Numbers, Characters, logicals, ...
Addresses
Commands (operations)




0
1
2
3
=>
=>
=>
=>
add
subtract
compare
multiply
7
Machine Code Representation
Some bits for the operation
Some bits for operands



the address of each operand
the address of the result
constants
N-1
0
operation result addr
add d x y
op1 addr
op2 addr
;; d = x + y;
8
The Stored Program Computer
0
1
2
3
4
5
6
7
8
9
0745
1873
0989
0970
0061
0017
0003
0000
0000
0000
Memory holds instructions and data as bits
Instructions are fetched from memory and executed
 operands fetched, manipulated, and stored
Example 4-digit Instruction
 operation: 0 => add, 1 => sub
 result address
 op1 address
 op2 address
Example Data
 4 digit unsigned value
What’s in memory after executing 0,1,2?
9
The Stored Program Computer
(cont’d)
We can write a program that will translate strings of
‘characters’ into ‘computer instructions’
 called a compiler or an assembler
We can load these particular bits into the computer
and execute them.
 may manipulate numbers, characters, pixels...
(application)
 may translate strings to instructions
(compiler)
 may load and run more programs (operating
system)
10
Central Processing Unit
Program
Counter
Memory Address
Register
Instruction
Register
Memory Data
Register
Instruction Decoder
General Purpose
Registers
Address Bus
Data Bus
Control & Status
Bus
Arithmetic & Logic
Unit
11
ALU Data Path
ALU
Memory
Data Bus
Accumulator
12
Execution Steps
Fetch and Execution Cycle
1.
2.
3.
4.
5.
memory
1.
Instruction
fetch
registers
CPU
control
logic
Mem Addr Bus  PC
Start Read
Increment PC
Wait Read Completion
IR  Mem Data Bus
2. I decode
4. data fetch
3. control signals
5. execution
6. result store
13
Assembly and C
14
Compilation
C compilers take C and convert it into an architecture specific
machine code
 Unlike Java which converts to “architecture independent code”
 Unlike most Scheme environments which interpret the code.
But how is it architecture specific?
 Maps to machine language for particular class of machines
 Each machine has a unique set of instructions
 Instruction Set Architecture !
15
Pros and Cons
Advantages of C-style compilation:
 Great run-time performance: generally much faster
than Scheme or Java for comparable code
 Simplicity / Predictability: you know what you get
Disadvantages of C-style compilation:
 Portability (lack thereof…)
 All compiled files (including the executable) are
architecture specific, depending on both the CPU
type and the operating system.
 Executable must be rebuilt on each new system.
16
Assembly Operators
Syntax of Assembly Statement
 1) operation by name
 2) operand getting result
 3) 1st operand for operation
 4) 2nd operand for operation
Ex. add b to c and put the result in a:
add a, b, c
 Called an (assembly language) Instruction
Equivalent assignment statement in C:
a = b + c;
17
Assembly Operators/Instructions
MIPS Assembly Syntax is rigid:
1 operation, 3 variables
 Why? Keep Hardware simple via regularity
How to do following C statement?
a = b + c + d - e;
Break into multiple instructions
add a, b, c
# a = sum of b & c
add a, a, d
# a = sum of b,c,d
sub a, a, e
# a = b+c+d-e
To right of sharp (#) is a comment terminated by end of the line
C comments have format /* comment */ , and can span
many lines
Unlike C (and most other HLL), each line of assembly contains
at most 1 instruction

18
Compilation
How to turn notation programmers prefer
into notation computer understands?
Program to translate C statements into
Assembly Language instructions; called a
compiler
Example: compile by hand this C code:
a = b + c;
d = a - e;
Easy: add a, b, c
sub d, a, e
19
Compilation 2
Example: compile by hand this C code:
f = (g + h) - (i + j);
First sum of g and h. Where put result?
add f, g, h # f contains g+h
Now sum of i and j. Where put result?
 Cannot use f !
 Compiler creates temporary variable to hold sum:
t1
add t1, i, j
# t1 contains i+j
Finally produce difference
sub f, f, t1 # f=(g+h)-(i+j)
20
Compilation 2 Summary
C statement (5 operands, 3 operators):
f = (g + h) - (i + j);
Becomes 3 assembly instructions
(6 unique operands, 3 operators):
add f, g, h
# f contains g+h
add t1, i, j
# t1 contains i+j
sub f, f, t1
# f=(g+h)-(i+j)
In general, each line of C produces many assembly instructions
 One reason why people program in C vs. Assembly;
 fewer lines of code
 Other reasons?
21
ASM operands
So far we have pretended that operands
were ‘variables’ as in a HLL
ASM operands are more restricted


registers in the machine (small fast
storage in the processor)
memory locations (specified by address)
r0
Execution
Unit
r31
22
Intel Architecture
23
Intel Architecture
Instruction Format

Zero-, one-, two-operands



HLT (halt)
INC EAX
MOV AX, 125
Operands:

Constant, register, memory, I/O port
24
Intel Architecture Registers
General Purpose Registers:

Accumulator (AX), Base Register (BX), Count Register (CX), Data
Register (DX), Stack Pointer (SP), Base Pointer (BP), Source
Index (SI), Destination Index (DI)
Flags Register (Flags)
Instruction Pointer (IP)
Segment Registers

Code Segment (CS), Data Segment (DS), Stack Segment (SS),
Extra Segment (ES)
25
Real Mode Architecture
Segmented Addressing:
16 bit address space, too restricted
 extend to 20 bit address space
using segmented addressing
16-bit segment
0000
16-bit offset
16-bit address
20-bit physical address
26
Addressing Modes
Direct Addressing Mode
Register Indirect
Based
Indexed
Based-Indexed
see figures in pp 82-83
27
Intel Protected Architecture
28
Data Manipulation Instructions
29
Compilation using Registers
Compile by hand using registers:
f = (g + h) - (i + j);

f: $s0, g: $s1, h: $s2,
i: $s3, j: $s4
MIPS Instructions:
add $s0,$s1,$s2
add $t1,$s3,$s4
sub $s0,$s0,$t1
# $s0 = g+h
# $t1 = i+j
# f=(g+h)-(i+j)
30
What does the instruction look
like?
op
6 bits
rs
5 bits
rt
5 bits
rd
5 bits
funct
5 bits
6 bits
32 bit instruction word divided into fields
 op: basic operation of instruction, “opcode”
 rs: 1st register source operand
 rt: 2nd register source operand
 rd: register destination operand, gets the result
 funct: function; selects the specific variant of the
operation in the op field; sometimes called the
function code
31
Instruction as a Number
C code:
i = j + k;
/* i-k:$s0-$s2 */
Assembly: add $s0,$s1,$s2 #s0=s1+s2
Decimal representation:
0
17
18
16
0
32
Binary representation:
000000
6 bits
10001
5 bits
10010
5 bits
10000
5 bits
00000
5 bits
100000
6 bits
Called Machine Language Instruction
 Layout called Instruction Format
 All MIPS instructions 32 bits (word): simple!

32
Things to Remember
Compilers convert C into an architecture specific machine
code
MIPS Assembly Language syntax
 OP RES ARG1 ARG2
# RES = ARG1 OP ARG2
Mapping of arithmetic expressions to sequence of MIPS
arithmetic operations
Registers as Operands
 Memory next time!
Instructions encoded as numeric fields
Big Idea: compiler translates notation from one level of
abstraction (C) to lower level (ASM)
 assembly language is symbolic form of binary machine
language
33