lecture02 assembly language

23
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE Lecture 2 1

Upload: programming-passion

Post on 11-Jul-2015

54 views

Category:

Engineering


6 download

TRANSCRIPT

Page 1: Lecture02 assembly language

COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

Lecture 2

1

Page 2: Lecture02 assembly language

“C Program” Down to “Numbers”2

swap:muli $2, $5, 4add $2, $4, $2lw $15, 0($2)lw $16, 4($2)sw $16, 0($2)sw $15, 4($2)jr $31

void swap(int v[], int k){

int temp;temp = v[k];v[k] = v[k+1];v[k+1] = temp;

}

00000000101000010…00000000000110000…10001100011000100…10001100111100100…10101100111100100…10101100011000100…00000011111000000…

compiler

assembler

Page 3: Lecture02 assembly language

“Numbers” in Memory3

00000000101000010…00000000000110000…10001100011000100…10001100111100100…10101100111100100…10101100011000100…00000011111000000…

Page 4: Lecture02 assembly language

A List of ISA4

IA32 (MASM supports this architecture) IA64 (32 and 64 bit Intel Architecture) Sun Sparc DEC Alpha MIPS R2000/R300 ARM Some of these are actually families of ISAs.

That is, they are a collection of similar ISAs

Page 5: Lecture02 assembly language

Stored Program Concept5

processor

main memory hard disk

program A

program Bprogram C

program A

program B

data A

data B

program fetchdata load/store

disk I/Oprogramcounter

Page 6: Lecture02 assembly language

Fetch, Execute, Decode Cycle6

1. IF (Instruction Fetch) The instruction is fetched from RAM Address of register called PC The instruction is copied from memory to IR

1. D (Decode the instruction. Fetch Operands) For example, if you have an add instruction that adds the

contents of register 1 and 2, and places the result in register, then the values of register 1 and 2 need to be fetched to perform the addition.

1. ALU (Perform the operation) In the add example, addition is performed. This is carried out

by a circuit called the ALU

Page 7: Lecture02 assembly language

Fetch, Execute, Decode Cycle7

4. MEM (Memory access) In MIPS, the only memory access occurs

during load and store instructions. For other instructions, this step doesn't do anything. The memory being accessed is the data memory (cache)

4. WB (Write Back) The result of the operation being performed in step 3 (ALU), is

written to the appropriate register in the register file.4. PC Update (PC Update)

Update the value of the PC to next instruction. However, on branch and jump instructions, PC can be updated to other addresses.

Page 8: Lecture02 assembly language

Stored Program Concept8

Programs (instructions) are stored in memory as a stream of bits (numbers) Indistinguishable from data More than one program can reside in memory at

the same time Programs can be modified by the processor or I/O

just as data can be modified Instructions are fetched by the processor and

decoded; they determine processor actions Program Counter determines which instruction

is fetched next

Page 9: Lecture02 assembly language

Stored Program Concept9

In fact, one of the great ideas in computer science is the idea that programs could be stored just as data is stored.

Before that, people envisioned the hardware running a fixed program, and data being stored in memory.

Page 10: Lecture02 assembly language

10

Page 11: Lecture02 assembly language

11

Page 12: Lecture02 assembly language

12

Page 13: Lecture02 assembly language

13

Page 14: Lecture02 assembly language

14

Page 15: Lecture02 assembly language

Addresses and Contents shown in Hex

15

Page 16: Lecture02 assembly language

Number Systems16

Actual machine code is in binary O, 1 are high and low signals to hardware

Hex (base 16) is often used by humans (c o d e , s im ula to r, m a nua ls , … ) because:

16 is a power of 2 (while 10 is not); mapping between hex and binary is easy

It’s more compact than binary We can write, e.g., 0x90000008 in programs

rather than 10010000000000000000000000001000

Page 17: Lecture02 assembly language

Base 10 (Decimal)17

Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (10 of them)

Example: 3217 = (3×103) + (2×102) + (1×101) + (7×100) A shorthand form we’ll also use:

103 102 101 100

3 2 1 7

Page 18: Lecture02 assembly language

Example: Base 2 (Binary)18

Digits: 0, 1 (2 of them) “Binary digit” = “Bit”

Example: 11010two = (1×24) + (1×23) + (0×22) + (1×21) + (0×20)

= 16 + 8 + 0 + 2 + 0 = 26ten

Choice for machine implementation! 1 = on, 0 = off

Page 19: Lecture02 assembly language

Base Conversion, cont’d19

From binary to decimal

From decimal to binary

From binary to hexadecimal

From hexadecimal to binary

From decimal to hexadecimal

Page 20: Lecture02 assembly language

Base Conversion, cont’d20

Binary to hex (base 16), or hex to binary base conversion: Take 4 bits in binary and convert them into one hex digit and vice

versa For binary hex: 4-bit groups, starting from the right For hex binary: translate each hex digit into 4 bits, starting

from the right

Since binary notation tends to be long, hex notation is frequently used in assembly language (and in C programs).

More on binary number representation will be discussed when we study arithmetic

Page 21: Lecture02 assembly language

Program Performance21

Program performance is measured in terms of time!

Program execution time depends on: Number of instructions executed to complete a

job How many clock cycles are needed to execute

a single instruction The length of the clock cycle (clock cycle time)

Page 22: Lecture02 assembly language

Clock, Clock Cycle Time22

Circuits in computers are “clocked” At each clock rising (or falling) edge, some specified actions are

done, usually within the next rising (or falling) edge Instructions typically require more than one cycle to execute

Function block(made of circuits)

clock

clock cycle time

Page 23: Lecture02 assembly language

Program Performance23

time = (# of clock cycles) × (clock cycle time)

# of clock cycles = (# of instructions executed) × (average cycles per instruction)