cs 230: computer organization and assembly language

33
C C M M L L C C M M L L CS 230: Computer CS 230: Computer Organization and Organization and Assembly Language Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics Arizona State University Slides courtesy: Prof. Yann Hang Lee, ASU, Prof. Mary Jane Irwin, PSU, Ande Carle, UCB

Upload: havard

Post on 09-Jan-2016

18 views

Category:

Documents


0 download

DESCRIPTION

CS 230: Computer Organization and Assembly Language. Aviral Shrivastava. Department of Computer Science and Engineering School of Computing and Informatics Arizona State University. Slides courtesy: Prof. Yann Hang Lee, ASU, Prof. Mary Jane Irwin, PSU, Ande Carle, UCB. Announcements. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

CS 230: Computer CS 230: Computer Organization and Organization and

Assembly LanguageAssembly LanguageAviral

ShrivastavaDepartment of Computer Science and

EngineeringSchool of Computing and Informatics

Arizona State University

Slides courtesy: Prof. Yann Hang Lee, ASU, Prof. Mary Jane Irwin, PSU, Ande Carle, UCB

Page 2: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

AnnouncementsAnnouncements

Page 3: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Computer OrganizationComputer Organization

• We have leaned the ISA of the processor till now– Given an algorithm, express it in terms of the processor ISA

Instruction Set Architecture

software

hardware

Page 4: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

High-level language program (in C) swap (int v[], int k) . . .

Assembly language program (for MIPS) swap: sll $2, $5, 2 add $2, $4, $2 lw $15, 0($2) lw $16, 4($2) sw $16, 0($2) sw $15, 4($2) jr $31

Machine (object) code (for MIPS) 000000 00000 00101 0001000010000000 000000 00100 00010 0001000000100000 100011 00010 01111 0000000000000000 100011 00010 10000 0000000000000100 101011 00010 10000 0000000000000000 101011 00010 01111 0000000000000100 000000 11111 00000 0000000000001000

Below the ProgramBelow the Program

C - Compiler

Assembler

Page 5: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Working of ComputerWorking of Computer

Processor

Control

Datapath

Memory

Devices

Input

Output

Page 6: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Input Device Inputs Input Device Inputs Object CodeObject Code

Processor

Control

Datapath

Memory

Devices

Input

Output

000000 00000 00101 0001000010000000 000000 00100 00010 0001000000100000 100011 00010 01111 0000000000000000 100011 00010 10000 0000000000000100 101011 00010 10000 0000000000000000 101011 00010 01111 0000000000000100 000000 11111 00000 0000000000001000

Page 7: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Object Code Stored in Object Code Stored in MemoryMemory

Processor

Control

Datapath

MemoryDevices

Input

Output

000000 00000 00101 0001000010000000000000 00100 00010 0001000000100000100011 00010 01111 0000000000000000100011 00010 10000 0000000000000100101011 00010 10000 0000000000000000101011 00010 01111 0000000000000100000000 11111 00000 0000000000001000

Page 8: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Processor Fetches an Processor Fetches an InstructionInstruction

Processor

Control

Datapath

MemoryDevices

Input

Output

000000 00000 00101 0001000010000000000000 00100 00010 0001000000100000100011 00010 01111 0000000000000000100011 00010 10000 0000000000000100101011 00010 10000 0000000000000000101011 00010 01111 0000000000000100000000 11111 00000 0000000000001000

Processor fetches an instruction from memory

Page 9: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Control000000 00100 00010 0001000000100000

Control Decodes the Control Decodes the InstructionInstruction

Processor

Datapath

Memory

Devices

Input

Output

Control decodes the instruction to determine what to execute

Page 10: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Datapath Executes the Datapath Executes the InstructionInstruction

Processor

Control

Datapath

Memory

Devices

Input

Outputcontents Reg #4 ADD contents Reg #2results put in Reg #2

Datapath executes the instruction as directed by control

000000 00100 00010 0001000000100000

Page 11: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Update the PC, and Update the PC, and continuecontinue

Processor

Control

Datapath

MemoryDevices

Input

Output

000000 00000 00101 0001000010000000000000 00100 00010 0001000000100000100011 00010 01111 0000000000000000100011 00010 10000 0000000000000100101011 00010 10000 0000000000000000101011 00010 01111 0000000000000100000000 11111 00000 0000000000001000

Fetch

DecodeExecute

Page 12: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Output Data Stored in Output Data Stored in MemoryMemory

Processor

Control

Datapath

MemoryDevices

Input

Output000001000101000000000000000000000000000001001111000000000000010000000011111000000000000000001000

At program completion the data to be output resides in memory

Page 13: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Output Device Outputs Output Device Outputs DataData

Processor

Control

Datapath

Memory

Devices

Input

Output

000001000101000000000000000000000000000001001111000000000000010000000011111000000000000000001000

Page 14: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

MIPS ISA to ImplementMIPS ISA to Implement

• Arithmetic Instructions– Add, sub, AND, OR, and slt (R-

type)

• Memory access instructions– LW, and SW (I-type)

• Branch instructions– Beq (I-type)

• Jump instructions– J (J-type)

Page 15: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

MIPS Machine MIPS Machine

ReadAddress

Instr[31-0]

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

Read Data 1

Read Data 2

ALU

ovf

zero

RegWrite

DataMemory

Address

Write Data

Read Data

MemWrite

MemRead

SignExtend16 32

MemtoReg

ALUSrc

Shiftleft 2

Add

PCSrc

RegDst

ALUcontrol

1

1

1

00

0

0

1

ALUOp

Instr[5-0]

Instr[15-0]

Instr[25-21]

Instr[20-16]

Instr[15 -11]

ControlUnit

Instr[31-26]

Branch

Shiftleft 2

0

1

Jump

32Instr[25-0]

26PC+4[31-28]

28

Page 16: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Build a MIPS ProcessorBuild a MIPS Processor• Today – Build the ALU

– Support all the Arithmetic/Logic operations

32

32

32

ALU control lines

result

a

b

ALU

ALU Control Lines

Function

0000 AND

0001 OR

0010 add

0110 subtract

0111 Set on less than

1100 NOR

Page 17: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

ALUOp and ALU control bitsALUOp and ALU control bits

Instruction Opcode

ALUOp

Instruction operation

Funct field

Desired ALU action

ALU control input

LW 00 Load word XXXXXX

add 0010

SW 00 Store word XXXXXX

Add 0010

Beq 01 Branch equal XXXXXX

Subtract 0110

R-type 10 Add 100000 Add 0010

R-type 10 Subtract 100010 Subtract 0110

R-type 10 AND 100100 AND 0000

R-type 10 OR 100101 OR 0001

R-type 10 Set on less than

101010 Set on less than

0111

Page 18: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

ImplementationImplementation

Truth table for 4 ALU control bits

Page 19: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Building a 1-bit Binary Building a 1-bit Binary AdderAdder

S = A xor B xor carry_in carry_out = (A and B) or (A and carry_in) or (B and carry_in)

1 bit Full Adder

A

BS

carry_in

carry_out

• How can we use it to build a 32-bit adder?

• How can we modify it easily to build an adder/subtractor?

A B carry_in

carry_out

S

0 0 0 0 0

0 0 1 0 1

0 1 0 0 1

0 1 1 1 0

1 0 0 0 1

1 0 1 1 0

1 1 0 1 0

1 1 1 1 1

Page 20: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Building 32-bit AdderBuilding 32-bit Adder

• Just connect the carry-out of the least significant bit FA to the carry-in of the next least significant bit and connect . . .

• Ripple Carry Adder (RCA)– advantage: simple logic, so

small (low cost)– disadvantage: slow and lots

of glitching (so lots of energy consumption)

1-bit FA

A0

B0

S0

c0=carry_in

c1

1-bit FA

A1

B1

S1

c2

1-bit FA

A2

B2

S2

c3

c32=carry_out

1-bit FA

A31

B31

S31

c31

. .

.

Page 21: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Building 32-bit Building 32-bit Adder/SubtractorAdder/Subtractor

• Remember 2’s complement is just– complement all the bits

– add a 1 in the least significant bit

A 0111 0111B - 0110 + 1010

1-bit FA S0

c0=carry_in

c1

1-bit FA S1

c2

1-bit FA S2

c3

c32=carry_out

1-bit FA S31

c31

. .

.

A0

A1

A2

A31

B0

B1

B2

B31

add/subt

B0

control(0=add,1=subt) B0 if control =

0, !B0 if control = 1

Page 22: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Logic OperationsLogic Operations

• Logic operations operate on individual bits of the operand.$t2 = 0…0 0000 1101 0000

$t1 = 0…0 0011 1100 0000

and $t0, $t1, $t2 $t0 =

or $t0, $t1, $t2 $t0 =

xor $t0, $t1, $t2 $t0 =

nor $t0, $t1, $t2 $t0 =

• How do we expand our FA design to handle the logic operations - and, or, xor, nor ?

0…0 0000 1100 0000

0…0 0011 1101 0000

0…0 0011 0001 0000

1…1 1100 0010 1111

Page 23: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

A Simple ALU CellA Simple ALU Cell

1-bit FA

carry_in

carry_out

A

B

add/subt

add/subt

result

op

Page 24: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Tailoring the ALU to the Tailoring the ALU to the MIPS ISAMIPS ISA

• Need to support the set-on-less-than instruction

(slt)

– remember: slt is an arithmetic instruction

– produces a 1 if rs < rt and 0 otherwise

– use subtraction: (a - b) < 0 implies a < b

• Need to support test for equality (beq)

– use subtraction: (a - b) = 0 implies a = b

• Need to add the overflow detection hardware

Page 25: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Modifying the ALU Cell for Modifying the ALU Cell for sltslt

1-bit FA

A

B

result

carry_in

carry_out

add/subt op

add/subt

less

Page 26: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

ALU for sltALU for slt

0

0set

First perform a subtraction

Make the result 1 if the subtraction yields a negative result

Make the result 0 if the subtraction yields a positive result

A1

B1

A0

B0

A31

B31

+

result1

less

+

result0

less

+

result31

less

. . .

tie the most significant sum bit (sign bit) to the low order less input

Page 27: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

ALU for ZeroALU for Zero

+

A1

B1

result1

less

+

A0

B0

result0

less

+

A31

B31

result31

less

. . .

0

0

set

First perform subtraction

Insert additional logic to detect when all result bits are zero

zero

. . .

add/subtop

Note zero is a 1 when result is all zeros

Page 28: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Overflow DetectionOverflow Detection

• Overflow: the result is too large to represent in the number of bits allocated

• On your own: Prove you can detect overflow by:– Carry into MSB xor Carry out of MSB

1

1

1 10

1

0

1

1

0

0 1 1 1

0 0 1 1+

7

3

0

1

– 6

1 1 0 0

1 0 1 1+

–4

– 5

71

0

Page 29: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Modifying the ALU for Modifying the ALU for OverflowOverflow

+

A1

B1

result1

less

+

A0

B0

result0

less

+

A31

B31

result31

less

. . .

0

0

set

Modify the most significant cell to determine overflow output setting

Disable overflow bit setting for unsigned arithmetic

zero

. . .

add/subtop

overflow

Page 30: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Shift OperationsShift Operations

• Also need operations to pack and unpack 8-bit characters into 32-bit words

• Shifts move all the bits in a word left or right sll $t2, $s0, 8 #$t2 = $s0 << 8 bits

srl $t2, $s0, 8 #$t2 = $s0 >> 8 bits

• Such shifts are logical because they fill with zeros

op rs rt rd shamt funct

000000 00000 10000 01010 01000 000000

000000 00000 10000 01010 01000 000010

Page 31: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Shift OperationsShift Operations

• An arithmetic shift (sra) maintain the arithmetic correctness of the shifted value (i.e., a number shifted right one bit should be ½ of its original value; a number shifted left should be 2 times its original value)– so sra uses the most significant bit (sign bit) as the bit

shifted in– note that there is no need for a sla when using two’s

complement number representation

sra $t2, $s0, 8 #$t2 = $s0 >> 8 bits

• The shift operation is implemented by hardware (usually a barrel shifter) outside the ALU

000000 00000 10000 01010 01000 000011

Page 32: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

MIPS MIPS Machine Machine

ReadAddress

Instr[31-0]

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

Read Data 1

Read Data 2

ALU

ovf

zero

RegWrite

DataMemory

Address

Write Data

Read Data

MemWrite

MemRead

SignExtend16 32

MemtoReg

ALUSrc

Shiftleft 2

Add

PCSrc

RegDst

ALUcontrol

1

1

1

00

0

0

1

ALUOp

Instr[5-0]

Instr[15-0]

Instr[25-21]

Instr[20-16]

Instr[15 -11]

ControlUnit

Instr[31-26]

Branch

Shiftleft 2

0

1

Jump

32Instr[25-0]

26PC+4[31-28]

28

Page 33: CS 230: Computer Organization and Assembly Language

CCMMLLCCMMLL

Yoda says…Yoda says…

Use your feelings, Obi-Wan, and find him you will