Transcript Ch. I-1

CS2422
Basic Concepts
Department of Computer Science
National Tsing Hua University
Assembly Language for IntelBased Computers, 5th Edition
Kip Irvine
Chapter 1: Basic Concepts
Slides prepared by the author, with modification
Revision date: June 3, 2006
(c) Pearson Education, 2006-2007. All rights reserved. You may modify and copy this slide show for your personal use,
or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
當你在寫C程式時,
心中想像的電腦是長什麼樣子?
CPU, memory, I/O,
Operations on variables,
…
A Model of Computer for C
Program
Counter
i = i + j;
xfloat = 1.0;
if (A[0]==0)
…
Memory
i, j, k;
&&
CPU
if

+
for
xfloat, yfloat;
A[0], A[1], …
Typed storage
3
這個model和你實際跑程式的
電腦有很大的差別!
為什麼你用這個C的model
所寫的程式能在你的電腦上執行?
顯然有人為你做了轉換!
我們已經知道...
C program
x = (a+b) * b
Assembly program
C compiler
MOV
ADD
MUL
MOV
AX, a
AX, b
c
x, AX
5
可見得組合語言程式更靠近
實際的電腦
組合語言程看到的電腦
是長什麼樣子?
A Model of Computer for ASM
MOV AX, a
ADD AX, b
MOV x, AX
…
CPU
PC
AX
BX
ALU
Register
Take the data stored in memory
address a, and move it to register AX
Take the data stored in memory
address b, and add it to register AX
Take the dataMemory
stored in register AX,
and move it to memory address x
a
010100110010101
b
110010110001010
x
000000000010010
...
+ -
7
組合語言程式/機器碼
看到的電腦,
離實際電腦還是有一段差距
e.g. 多核心、hyperthreading
當然仍需要再一或多層的轉換!
Computer Model of a Lower Layer
PCSrc
ID/EX
0
M
u
x
1
WB
Control
IF/ID
EX/MEM
M
WB
EX
M
MEM/WB
WB
Add
Add
Add result
Instruction
memory
ALUSrc
Read
register 1
Read
data 1
Read
register 2
Registers Read
Write
data 2
register
Write
data
MemtoReg
Address
Branch
Shift
left 2
MemWrite
PC
Instruction
RegWrite
4
Zero
ALU
0
M
u
x
1
ALU
result
Address
Data
memory
Read
data
Write
data
Instruction 16
[15– 0]
Instruction
[20– 16]
Instruction
[15– 11]
Sign
extend
32
6
ALU
control
0
M
u
x
1
MemRead
ALUOp
RegDst
(from Computer Architecture textbook)
1
M
u
x
0
A Layered View of Computer
i = i + j;
if (A[0]==0)
…
Program
Counter
&&
CPU

if
i = i + j;
xfloat = 1.0;
if (A[0]==0)
…
i, j, k;
xfloat, yfloat;
+
A[0], A[1], …
for
Memory
ADD AX, b
MOV x, AX
…
MOV AX, a
ADD AX, b
MOV x, AX
…
a 010100110010101
b 110010110001010
AX
PC BX ...
JX
CPU
Memory
x 000000000010010
+ PCSrc
ID/EX
0
M
u
x
1
WB
Control
IF/ID
EX/MEM
M
WB
EX
M
MEM/WB
WB
Add
Add
Add result
Instruction
memory
Branch
Shift
left 2
ALUSrc
Read
register 1
Read
data 1
Read
register 2
Registers
Read
Write
data 2
register
Write
data
MemtoReg
Address
MemWrite
PC
Instruction
RegWrite
4
Zero
ALU
0
M
u
x
1
ALU
result
Address
Data
memory
Read
data
Write
data
Instruction
[15– 0]
Instruction
[20– 16]
Instruction
[15– 11]
16
Sign
extend
32
6
ALU
control
0
M
u
x
1
ALUOp
RegDst
MemRead
1
M
u
x
0
xxxxxx
xxxxx
10
What’s Next?


Virtual machine concept (Sec. 1-2)
Data representation (Sec. 1-3)
11
Virtual Machine Concept

Purpose of this section:


Understand the role of assembly language in a
computer system
Side product:

The principle of layered abstraction for combating
complexities, e.g. OSI 7-layer protocol
12
Virtual Machine Concept



A layered abstraction of
computers proposed by
A. Tanenbaum
Each layer provides an
abstract computer, or
virtual machine, to its
upper layer
Virtual machine:

A hypothetical computer
that can be constructed
of either HW or SW
What is a computer?
High-Level Language
Level 5
Assembly Language
Level 4
Operating System
Level 3
Instruction Set
Architecture
Level 2
Microarchitecture
Level 1
Digital Logic
Level 0
13
Simplest Model of Computers
Instructions
Program
Input data
Compute
engine
Memory
Output data
c.f., y = f(x)
Layered abstraction: A computer consists of layers of
such virtual machine abstractions
14
Why Layered Abstraction?

Big idea: layered abstraction to combat
complexities





A strategy of divide-and-conquer
Decompose a complex system into layers with
well-defined interfaces
Each layer is easier to manage and handle
Only need to focus on a particular layer, e.g. to
make it the best
Also, it makes interaction clear

Particularly if one layer is realized in hardware and
the other in software
15
Layered Abstraction of Computer

Each layer as a hypothetical computer, or virtual
machine, that runs a programming language

Can be programmed with the programming
language to process inputs and outputs
Instructions
Compute
engine

Program written in Li can
be mapped to that Li-1 by:


Program
Input data
Memory
Output data
Interpretation: Li-1 program interprets and executes
Li instructions one by one
Translation: Li program is completely translated
into Li-1 program, and runs on Li-1 machine
16
Layered Abstraction of Computer
Program
Counter
&&
CPU

if
Memory
i = i + j;
xfloat = 1.0;
if (A[0]==0)
…
i, j, k;
xfloat, yfloat;
+
A[0], A[1], …
for
Memory
ADD AX, b
MOV x, AX
…
b 110010110001010
x 000000000010010
+ -
Li
MOV AX, a
ADD AX, b
MOV x, AX
…
a 010100110010101
AX
PC BX ...
JX
CPU
Virtual Machine
i = i + j;
if (A[0]==0)
…
Li-1
PCSrc
ID/EX
0
M
u
x
1
WB
Control
IF/ID
EX/MEM
M
WB
EX
M
MEM/WB
WB
Add
Add
Add result
Instruction
memory
Branch
Shift
left 2
ALUSrc
Read
register 1
Read
data 1
Read
register 2
Registers
Read
Write
data 2
register
Write
data
MemtoReg
Address
MemWrite
PC
Instruction
RegWrite
4
Zero
ALU
0
M
u
x
1
ALU
result
Address
Data
memory
Read
data
Write
data
Instruction
[15– 0]
Instruction
[20– 16]
Instruction
[15– 11]
16
Sign
extend
32
6
ALU
control
0
M
u
x
1
ALUOp
RegDst
MemRead
1
M
u
x
0
xxxxxx
xxxxx
17
Languages of Different Layers
English: Display the sum of A times B plus C.
C++: cout << (A * B + C);
Assembly Language:
mov eax,A
mul B
add eax,C
call WriteInt
Intel Machine Language:
A1 00000000
F7 25 00000004
03 05 00000008
E8 00500000
18
High-Level Language
Level 5
 Application-oriented languages, e.g., C, C++,
Java, Perl
 Written with certain programming model in mind



Variables in storage
Operators for operations
Programs compiled into assembly language
(Level 4) or interpreted by interpreters
What kind of computer does C see?
19
Assembly Language
Level 4
 Instruction mnemonics that have a one-to-one
correspondence to machine language
 Based on a view of machine: register
organization, addressing, operand types and
locations, functional units, …
High-Level Language
 Calls functions written at the
Assembly Language
OS level (Level 3)
 Programs are translated into
Operating System
machine language (Level 2)
Instruction Set
What kind of computer does it see?
Level 5
Level 4
Level 3
Architecture
Level 2
Microarchitecture
Level 1
Digital Logic
Level 0
20
Operating System
Level 3
 Provides services to Level 4 programs as if it
were a computer


How about VMware?
How about JVM?
21
Instruction Set Architecture
Level 2
 Known as conventional machine language
 Attributes of a computer as seen by assembly
programmer, i.e. conceptual structure and
functional behavior





Organization of programmable storage
Data types and data structures
Instruction set and formats
Addressing modes and data accessing
Executed by Level 1 program (microarchitecture)
22
Microarchitecture
Level 1
 Can be described by register transfer language
(RTL)
 Interprets conventional machine instructions
(Level 2)
 Executed by digital hardware (Level 0)
Register
Control Signals
Controller
clock
Memory
N
Z
IR
ALU
PC
23
Digital Logic
Level 0
 CPU, constructed from digital logic gates
 System bus
 Memory
24
What’s Next?


Virtual machine concept (Sec. 1-2)
Data representation (Sec. 1-3)
25
Data Representation

Purpose of this section







Assembly program often needs to process data,
and manage data storage and memory locations
 need to know data representation and storage
Binary numbers: translating between binary and
decimal
Binary addition
Integer storage sizes
Hexadecimal integers: translating between
decimal and hex.; hex. subtraction
Signed integers: binary subtraction
Character storage
26
Integer Storage Sizes
Standard sizes:
byte
word
doubleword
8
16
32
quadword
64
Why unsigned
numbers?
What is the largest unsigned integer that may be stored in 20 bits?
27
Signed Integers

The highest bit indicates the sign
1 = negative, 0 = positive
sign bit
1
1
1
1
0
1
1
0
0
0
0
0
1
0
1
0
Negative
Positive
If the highest digit of a hexadecimal integer is > 7, the
value is negative. Examples: 8A, C5, A2, 9D
28
Forming Two's Complement

Negative numbers are stored in two's
complement notation


Complement (reverse) each bit
Add 1
Why?
Note that 00000001 + 11111111 = 00000000
29
Binary Subtraction


When subtracting A – B, convert B to its two's
complement
Add A to (–B)
1100
– 0011
1100
+ 1101
1001
Practice: Subtract 0101 from 1001
30
Ranges of Signed Integers


The highest bit is reserved for the sign
This limits the range:
What is the largest positive value that may be stored in 20 bits?
31
Character Storage

Character sets

Standard ASCII (0 – 127)
‒ American Standard Code for Information
Interchange


Extended ASCII (0 – 255)
ANSI (0 – 255)
‒ American National Standard Institute


Null-terminated string


Unicode (0 – 65,535): UTF-8. UTF-16, UTF-32
Array of characters followed by a null byte
Using the ASCII table

back inside cover of book
32
Chapter Summary



Assembly language helps you learn how
software is constructed at the lowest levels
Assembly language has a one-to-one
relationship with machine language
Each layer in a computer's architecture is an
abstraction of a machine


layers can be hardware or software
Assembly programs and assemblers often need
to manage data storage in memory, and it is
important to understand data representation and
size of different data
33