+ All Categories
Home > Documents > CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

Date post: 12-Jan-2016
Category:
Upload: prudence-alexander
View: 222 times
Download: 0 times
Share this document with a friend
34
CS2422 Basic Concepts Department of Computer Science National Tsing Hua University
Transcript
Page 1: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

CS2422 Basic Concepts

Department of Computer ScienceNational Tsing Hua University

Page 2: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

Assembly Language for Intel-

Based Computers, 5th Edition

Chapter 1: Basic Concepts

(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.

Slides prepared by the author, with modification

Revision date: June 3, 2006

Kip Irvine

Page 3: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

當你在寫 C 程式時,心中想像的電腦是長什麼樣子?

CPU, memory, I/O,Operations on variables,

Page 4: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

4

A Model of Computer for C

+

if for

&&

CPU

Memory

i, j, k;

xfloat, yfloat;

A[0], A[1], …

i = i + j;xfloat = 1.0;if (A[0]==0)…

ProgramCounter

Typed storage

Page 5: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

這個model和你實際跑程式的電腦有很大的差別!

為什麼你用這個 C 的 model所寫的程式能在你的電腦上執行?

顯然有人為你做了轉換!

Page 6: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

6

MOV AX, aADD AX, bMUL cMOV x, AX

MOV AX, aADD AX, bMUL cMOV x, AX

x = (a+b) * bx = (a+b) * b

我們已經知道 ...

C compilerC compiler

Assembly program

C program

Page 7: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

可見得組合語言程式更靠近實際的電腦

組合語言程看到的電腦是長什麼樣子 ?

Page 8: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

8

A Model of Computer for ASM

CPU

MemoryMOV AX, aADD AX, bMOV x, AX…

010100110010101a

110010110001010b

000000000010010x

AXBX

...

+ -

PC

Register

ALU

Take the data stored in memory address a, and move it to register AXTake the data stored in memory address b, and add it to register AXTake the data stored in register AX, and move it to memory address x

Page 9: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

組合語言程式 /機器碼看到的電腦,

離實際電腦還是有一段差距e.g. 多核

心、 hyperthreading

當然仍需要再一或多層的轉換!

Page 10: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

Computer Model of a Lower Layer

PC

Instructionmemory

Inst

ruct

ion

Add

Instruction[20– 16]

Mem

toR

eg

ALUOp

Branch

RegDst

ALUSrc

4

16 32Instruction[15– 0]

0

0

Mux

0

1

Add Addresult

RegistersWriteregister

Writedata

Readdata 1

Readdata 2

Readregister 1

Readregister 2

Signextend

Mux

1

ALUresult

Zero

Writedata

Readdata

Mux

1

ALUcontrol

Shiftleft 2

RegW

rite

MemRead

Control

ALU

Instruction[15– 11]

6

EX

M

WB

M

WB

WBIF/ID

PCSrc

ID/EX

EX/MEM

MEM/WB

Mux

0

1

Mem

Write

AddressData

memory

Address

(from Computer Architecture textbook)

Page 11: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

11

A Layered View of Computer

+

if for

&&CPU

Memory

i, j, k;xfloat, yfloat;

A[0], A[1], …

i = i + j;if (A[0]==0)…

ProgramCounter

CPU

MemoryADD AX, bMOV x, AX… 010100110010101a

110010110001010b

000000000010010x

AXBXJX

...

+ -

PC

PC

Instructionmemory

Instruction

Add

Instruction[20– 16]

MemtoReg

ALUOp

Branch

RegDst

ALUSrc

4

16 32Instruction[15– 0]

0

0

Mux

0

1

Add Addresult

RegistersWriteregister

Writedata

Readdata 1

Readdata 2

Readregister 1

Readregister 2

Signextend

Mux

1

ALUresult

Zero

Writedata

Readdata

Mux

1

ALUcontrol

Shiftleft 2

RegW

rite

MemRead

Control

ALU

Instruction[15– 11]

6

EX

M

WB

M

WB

WBIF/ID

PCSrc

ID/EX

EX/MEM

MEM/WB

Mux

0

1

MemWrite

AddressData

memory

Address

i = i + j;xfloat = 1.0;if (A[0]==0)…

MOV AX, aADD AX, bMOV x, AX…

xxxxxxxxxxx

Page 12: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

12

What’s Next?

Virtual machine concept (Sec. 1-2) Data representation (Sec. 1-3)

Page 13: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

13

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

Page 14: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

14

Virtual Machine Concept

A layered abstraction ofcomputers proposed byA. Tanenbaum

Each layer provides anabstract computer, orvirtual machine, to itsupper layer

Virtual machine: A hypothetical computer

that can be constructedof either HW or SW

What is a computer?

Page 15: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

15

Simplest Model of Computers

Computeengine

Memory

Input data

Output data

ProgramInstructions

c.f., y = f(x)

Layered abstraction: A computer consists of layers of such virtual machine abstractions

Page 16: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

16

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

Page 17: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

17

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

Program written in Li canbe mapped to that Li-1 by: 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

Computeengine

MemoryInput data

Output data

ProgramInstructions

Page 18: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

18

Layered Abstraction of Computer

+

if for

&&CPU

Memory

i, j, k;xfloat, yfloat;

A[0], A[1], …

i = i + j;if (A[0]==0)…

ProgramCounter

CPU

MemoryADD AX, bMOV x, AX… 010100110010101a

110010110001010b

000000000010010x

AXBXJX

...

+ -

PC

PC

Instructionmemory

Instruction

Add

Instruction[20– 16]

MemtoReg

ALUOp

Branch

RegDst

ALUSrc

4

16 32Instruction[15– 0]

0

0

Mux

0

1

Add Addresult

RegistersWriteregister

Writedata

Readdata 1

Readdata 2

Readregister 1

Readregister 2

Signextend

Mux

1

ALUresult

Zero

Writedata

Readdata

Mux

1

ALUcontrol

Shiftleft 2

RegW

rite

MemRead

Control

ALU

Instruction[15– 11]

6

EX

M

WB

M

WB

WBIF/ID

PCSrc

ID/EX

EX/MEM

MEM/WB

Mux

0

1

MemWrite

AddressData

memory

Address

i = i + j;xfloat = 1.0;if (A[0]==0)…

MOV AX, aADD AX, bMOV x, AX…

xxxxxxxxxxx

Li

Li-1

Virtual Machine

Page 19: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

19

Languages of Different Layers

English: Display the sum of A times B plus C.

C++: cout << (A * B + C);

Assembly Language:

mov eax,Amul Badd eax,Ccall WriteInt

Intel Machine Language:

A1 00000000

F7 25 00000004

03 05 00000008

E8 00500000

Page 20: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

20

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?

Page 21: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

21

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, …

Calls functions written at theOS level (Level 3)

Programs are translated intomachine language (Level 2)

What kind of computer does it see?

Page 22: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

22

Operating System

Level 3 Provides services to Level 4 programs as if it

were a computer

How about VMware? How about JVM?

Page 23: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

23

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)

Page 24: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

24

Microarchitecture

Level 1 Can be described by register transfer language

(RTL) Interprets conventional machine instructions

(Level 2) Executed by digital hardware (Level 0)

ZN

Register

Memory

PCIR

Controller ALU

clock

Control Signals

Page 25: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

25

Digital Logic

Level 0 CPU, constructed from digital logic gates System bus Memory

Page 26: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

26

What’s Next?

Virtual machine concept (Sec. 1-2) Data representation (Sec. 1-3)

Page 27: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

27

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

Page 28: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

28

Integer Storage Sizes

What is the largest unsigned integer that may be stored in 20 bits?

Standard sizes:

Why unsignednumbers?

Why unsignednumbers?

Page 29: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

29

Signed Integers

The highest bit indicates the sign1 = negative, 0 = positive

If the highest digit of a hexadecimal integer is > 7, the value is negative. Examples: 8A, C5, A2, 9D

Page 30: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

30

Forming Two's Complement

Negative numbers are stored in two's complement notation Complement (reverse) each bit Add 1

Note that 00000001 + 11111111 = 00000000

Why?Why?

Page 31: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

31

Binary Subtraction

When subtracting A – B, convert B to its two's complement

Add A to (–B)

1 1 0 0 1 1 0 0– 0 0 1 1 + 1 1 0 1

1 0 0 1

Practice: Subtract 0101 from 1001

Page 32: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

32

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?

Page 33: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

33

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 Unicode (0 – 65,535): UTF-8. UTF-16, UTF-32

Null-terminated string Array of characters followed by a null byte

Using the ASCII table back inside cover of book

Page 34: CS2422 Basic Concepts Department of Computer Science National Tsing Hua University.

34

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


Recommended