Lecture02 assembly language

Preview:

Citation preview

COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

Lecture 2

1

“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

“Numbers” in Memory3

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

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

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

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

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.

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

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.

10

11

12

13

14

Addresses and Contents shown in Hex

15

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

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

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

Base Conversion, cont’d19

From binary to decimal

From decimal to binary

From binary to hexadecimal

From hexadecimal to binary

From decimal to hexadecimal

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

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)

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

Program Performance23

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

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

Recommended