30
1 Digital Computer Systems EE 344 Lecture 5: Assembly Language, an Overview Where are we? Very close to the hardware… Link between HLL, ISA, and microarchitecture One HLL instruction may be implemented with several assembly language instructions Symbolic presentation of the instruction set architecture (ISA) level Programs written with different HLL can be transformed to the same assembly language

Lecture 5 Digital Computing Systems

  • Upload
    sra893

  • View
    6

  • Download
    0

Embed Size (px)

DESCRIPTION

Lecture 5 Digital Computing Systems

Citation preview

Page 1: Lecture 5 Digital Computing Systems

1

Digital Computer SystemsEE 344

Lecture 5: Assembly Language, an Overview

Where are we?• Very close to the hardware…• Link between HLL, ISA, and

microarchitecture• One HLL instruction may be implemented with

several assembly language instructions• Symbolic presentation of the instruction set

architecture (ISA) level• Programs written with different HLL can be

transformed to the same assembly language

Page 2: Lecture 5 Digital Computing Systems

2

Not Easy!• Limited storage resources (memory and

registers)• No notion of types, it is the programmer

responsibility to use the correct instructions• Only “go to” control instructions• Amount of work done by an instruction is

smaller than a HLL instructions• Much longer to debug and much harder to

maintain• Difficult to think at that low level• Not portable

Why bother, then?• Performance (speed and cost)

– Much smaller than HLL– Much faster than HLL

• Complete access to the hardware features – Device drivers– Interrupts– Some real-time embedded systems– etc...

Page 3: Lecture 5 Digital Computing Systems

3

Assembly-Language

The view/model(registers and memory)

Data Types

Instruction types

The assembly programmersees memory and registers

the key difference between mediocre and excellent programmers is whether or not they know assembly language.

The Memory

• Large number of locations• Each location has a fixed size width

(usually the width is 1 byte)• Collection of all locations is called

memory address space• In assembly we use labels • Memory address space is used to store

instructions and data

Page 4: Lecture 5 Digital Computing Systems

4

Typical Memory OrganizationMemory Address Space

Text

Addresses0x000000000x00000001

.

.

.

Direction of growthAt run-time

Data

Stack

•Hold the instructions•Usually read-only fromProgrammer point of view

•OS can still write to it

To store the data (localand global) during run-time

To store data required during Subroutine activation, includingLocal variables

Register Model FAQ• Where are the registers?

– Inside the processor chip• What is their function

– Holding data needed frequently• How many registers do we have?

– Usually few • Why?

– Decoder used in selection is much smaller than the one used for memory

– Limited space on-chip– Few bits to specify a register– Technology

Page 5: Lecture 5 Digital Computing Systems

5

Commonly Used Registers• PC: program counter• ACC: Accumulator• SP: Stack Pointer• Link register• General purpose registers (GPRs)• Flag register

– N---negative result– Z--- zero result– V--- overflow– C--- carry– other

What about Data Types?

• There is no notion of variables• Type is specified based on the

instruction used• Instructions deal with few data types:

– Signed/unsigned integers– Floating point– Decimal (BCD)– characters

Page 6: Lecture 5 Digital Computing Systems

6

Instructions• Each assembly-level architecture has its

own group of instructions -> instruction set

• Instructions can be grouped into four categories:– Data Transfer– Data Manipulation– Program Sequencing and Control– Trap/system call

Instructions: Data Transfer

Page 7: Lecture 5 Digital Computing Systems

7

Instructions: Data Manipulation

Instructions: Program Sequencing and Control

Page 8: Lecture 5 Digital Computing Systems

8

Welcome to MIPS Assembly Language

Why MIPS?• Patterson and Hennessy use MIPS assembly language. Given the

overwhelming acceptance of their textbook as the standard, this is already a strong argument.

• RISC architectures actually are dominant. • It's best to teach a subject by going from the simple to the

complex. The MIPS R2000 instruction set architecture is probably the simplest in existence for a real processor with a significant market. In contrast, the Intel architecture, is extremely complex.

• Electrical and software engineers do, in fact, need to write assembly-language programs for embedded processors. (e.g. HP 4000 laser printer, Nintendo, Sony playstation, etc.)

• Other system integrators, such as router and telephone-switch manufacturers, make use of embedded processors.

• In all cases, these RISC architectures are extremely similar to the MIPS R2000 architecture that Hennessy and Patterson use throughout their text. A student who has learned the MIPS R2000 instruction set is in a good position to write programs for any embedded RISC processor.

Page 9: Lecture 5 Digital Computing Systems

9

You really should do…• http://www.cs.unibo.it/~solmi/teaching/arch_2002-

2003/AssemblyLanguageProgDoc.pdf(MIPS assembly Language Programmer’s Guide -> download it!)

http://logos.cs.uic.edu/366/notes/MIPS%20Quick%20Tutorial.htm

(A quick start on MIPS assembly -> look at it!)

Get Some books:

MIPS Assembly Language Programmingby Robert Britton. Published by Prentice Hall.

See MIPS Runby Dominic Sweetman. Publisher: Morgan Kaufmann

Processor

Computer

Control(“brain”)

Datapath

Memory

(where programs, data live whenrunning)

Devices

Input

Output

Keyboard, Mouse

Display, Printer

Disk(where programs, data live whennot running)

Computer Structure

Page 10: Lecture 5 Digital Computing Systems

10

MIPS arithmetic

•All instructions have 3 operands and order is fixed (destination first)

Example:

C code: a = b + c

MIPS ‘code’: add a, b, c

“The natural number of operands for an operation like addition is three…requiring every instruction to have exactly three operands, no more and no less, conforms to the philosophy of keeping the hardware simple”

MIPS arithmetic

•Design Principle: simplicity favors regularity. •Of course this complicates some things...

(what are these things?)

C code: a = b + c + d;

MIPS code: add a, b, cadd a, a, d

•Operands must be registers, •Only 32 registers provided•Each register contains 32 bits•Design Principle: smaller is faster.

What about programs with lots of variables?

Page 11: Lecture 5 Digital Computing Systems

11

Page 12: Lecture 5 Digital Computing Systems

12

Page 13: Lecture 5 Digital Computing Systems

13

Page 14: Lecture 5 Digital Computing Systems

14

Page 15: Lecture 5 Digital Computing Systems

15

Let $s3 = …1001 0100

Meaning:$t0 = Memory[$s3+24]

Page 16: Lecture 5 Digital Computing Systems

16

Page 17: Lecture 5 Digital Computing Systems

17

Memory Organization

•Viewed as a large, single-dimension array, with an address.•A memory address is an index into the array•"Byte addressing" means that the index points to a byte of

memory.

0

1

2

3

4

5

6

...

8 bits of data

8 bits of data

8 bits of data

8 bits of data

8 bits of data

8 bits of data

8 bits of data

Page 18: Lecture 5 Digital Computing Systems

18

Memory Organization

•Bytes are nice, but most data items use larger "words"•For MIPS, a word is 32 bits or 4 bytes.•Registers hold 32 bits of data•232 bytes with byte addresses from 0 to 232-1•230 words with byte addresses 0, 4, 8, ... 232-4

Instructions•Load and store instructions•Example:

C code: A[12] = h + A[8];

MIPS code: lw $t0, 32($s3)add $t0, $s2, $t0sw $t0, 48($s3)

•Can refer to registers by name (e.g., $s2, $t2) instead of number•Store word has destination last•Remember arithmetic operands are registers, not memory!

Can’t write: add 48($s3), $s2, 32($s3)•Byte Addressing: 4x8=32•$s3 is called Base Register•Data Transfer Instruction: A[8] is offset

Page 19: Lecture 5 Digital Computing Systems

19

Summary

•MIPS— loading words but addressing bytes— arithmetic on registers only

•Instruction Meaning

add $s1, $s2, $s3 $s1 = $s2 + $s3sub $s1, $s2, $s3 $s1 = $s2 – $s3lw $s1, 100($s2) $s1 = Memory[$s2+100] sw $s1, 100($s2) Memory[$s2+100] = $s1

Page 20: Lecture 5 Digital Computing Systems

20

ori = OR immediate

Page 21: Lecture 5 Digital Computing Systems

21

Page 22: Lecture 5 Digital Computing Systems

22

Page 23: Lecture 5 Digital Computing Systems

23

Examples of sll &srlSll $t2, $s0, 2 Multiply by 4

….0010…00100 Shift left once (* by 2)

…001000 Shift left twice(* by 4)srl $t2, $s0, 2 Divide by 4001…0001… Shift right once (div by 2)00001…Shift right twice(div by 4)

Page 24: Lecture 5 Digital Computing Systems

24

Page 25: Lecture 5 Digital Computing Systems

25

MIPS Instruction Classes

Page 26: Lecture 5 Digital Computing Systems

26

Page 27: Lecture 5 Digital Computing Systems

27

Page 28: Lecture 5 Digital Computing Systems

28

Page 29: Lecture 5 Digital Computing Systems

29

(Section 2.8)

Page 30: Lecture 5 Digital Computing Systems

30

See you next time….