24

Ch2a- 2 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Embed Size (px)

DESCRIPTION

Ch2a- 3 EE/CS/CPE Computer Organization  Seattle Pacific University Binary review Binary representations of numbers consist of only 1’s and 0’s = = = Unsigned (always positive) binary numbers 2 10 = 1024 = 1K  1, = 1,048,576 = 1M  1,000, = 1,073,741,824 = 1G  1,000,000,000 Being fluent in binary is highly underrated on the dating scene 2 10 = 1024 = 1K  1, = 1,048,576 = 1M  1,000, = 1,073,741,824 = 1G  1,000,000,000 Being fluent in binary is highly underrated on the dating scene Binary Facts:

Citation preview

Page 1: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what
Page 2: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 2EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Taking orders• A computer does what you tell it to do

• Not necessarily what you want it to do...

• We give computers orders by means of instructions• Instructions tell the computer what it should be doing,

right now• Arithmetic• Logic• Data movement• Control

Page 3: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 3EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Binary review

Binary representations of numbers consist of only 1’s and 0’s

01012 = 510

10002 = 810

11111112 = 12710

Unsigned (always positive)binary numbers 210 = 1024 = 1K 1,000

220 = 1,048,576 = 1M 1,000,000 230 = 1,073,741,824 = 1G 1,000,000,000 Being fluent in binary is highly underrated on the dating scene

Binary Facts:

Page 4: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 4EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Converting between binary and hexBinary0000000100100011010001010110011110001001101010111100110111101111

Group bits together in groups of 4

Assign the appropriate hex digit to each group

Done

10001100101100100001112

--> 10 0011 0010 1100 1000 01112

10 0011 0010 1100 1000 01112

--> 2 3 2 C 8 7

= 232C8716

Hexadecimal0123456789ABCDEF

Page 5: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 5EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

The Translation Process

Computers speak in binary. We don’t.(Exception: Geeks)

A = B + C

add $1, $2, $3

000000 00010 00011 00001 00000 100000

Compiler

Assembler

High-level language

Assembly language

Machine language

Compilers and Assemblerstranslate from one languageto another.

Page 6: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 6EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

MIPS Instructions

add A, B, C

Operation Destination Sources

A = B + C

sub D, A, B

D = A - B

In MIPS,All register-to-registerarithmetic instructionshave three operands.

Page 7: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 7EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Operands

add A, B, C

What are A, B, and C?

The operands of arithmetic instructions are always registers

add $17, $18, $19

Add contents of registers 18 and 19 and put result in register 17

sub $19, $19, $18

Subtract $19 - $18 and put the result back in $19

Page 8: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 8EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Registering• MIPS has 32 general-purpose registers

• $0 through $31

47$2

• Each register holds 32 bits• 0 to 232

-1 (4 billion) if unsigned

• -231 to +231-1 (-2 billion to +2 billion) if signed

• Most registers can hold any value, for any purpose• Exception: $0 is always zero!

Page 9: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 9EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Register Naming and Conventions• In MIPS, all registers (except $0) can be used for any purpose

desired• However, there are standard use conventions that make it

easier to write software# Name Purpose$0 $zero Constant zero$1 $at Reserved for assembler$2 $v0 Function return value$3 $v1$4 $a0 Function parameter$5 $a1$6 $a2$7 $a3$8 $t0 Temporary – Caller-saved$9 $t1$10 $t2$11 $t3$12 $t4$13 $t5$14 $t6$15 $t7

# Name Purpose$16 $s0 Temporary – Callee-saved$17 $s1$18 $s2$19 $s3$20 $s4$21 $s5$22 $s6$23 $s7$24 $t8 Temporary – Caller-saved$25 $t9$26 $k0 Reserved for OS$27 $k1$28 $gp Global pointer$29 $sp Stack pointer$30 $fp Frame pointer$31 $ra Function return address

Page 10: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 10EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Reflections on Registers• Registers are just “special” memory locations

• A small number of registers, as opposed to a huge number of memory locations

• Because there are a small number of registers, accessing them is fast

• Principle: Smaller is usually faster.

• Trade-offs• More registers --> More data in fast memory -->

Faster execution• Fewer registers --> Registers are faster --> Faster

execution• Compromise: 16 to 32 registers works well

Page 11: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 11EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Complicated arithmeticF = (A + B) - (C + D)

Assume: A is in $8 B is in $9 C is in $10 D is in $11 F is in $12

Note: Typically, the compilerassigns variables toregisters

$12 = ($8 + $9) - ($10 + $11)

We don’t have a 5-operandadd/subtract instruction!

Use temporary variables tosolve the problem.

add $13, $8, $9 # $13 <-- A + Badd $14, $10, $11 # $14 <-- C + Dsub $12, $13, $14 # F <-- (A+B) - (C+D)

$13 and $14 aretemporary variables

Page 12: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 12EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Getting to the bits of it allWe’ve looked at assembly language (briefly)

The CPU wants bits.

add $13, $8, $9

Assembler

0 8 9 13 0 32

Opcode RS RT RD ShAmt Function

6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

0 = Add $8 $9 $13 0 32=Add

32 bits, total

R-Type Instruction

000000 01000 01001 01101 00000 100000

Page 13: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 13EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Staying RegularR-Type instructions all have the same format:

Add has an opcode of ‘0’, a function of ‘32’

The instructions differ only in one bit!

Regularity:

Similar functions should be similar in format.Regularity is a keyto high-performance

Opcode RS RT RD ShAmt Function

6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

Subtract has an opcode of ‘0’, a function of ‘34’

add $13,$8,$9: 000000 01000 01001 01101 00000 100000

sub $13,$8,$9: 000000 01000 01001 01101 00000 100010

Page 14: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 14EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Constants

Many times, an instruction needs to use a constant value• Multiply by 4• Add 3

I-Type instructions all have the same format:

Opcode RS RT Immediate Data

6 bits 5 bits 5 bits 16 bits

8 10 12 4

001000 01010 01100 0000 0000 0000 0100

I-Type Instructionaddimmediate

Instructions with constant data in them are called immediate instructions• addi $12, $10, 4 # Reg. 12 <-- Reg. 10 + 4

Page 15: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 15EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Doing Double Duty• You desire to copy the value in register $8 to $10

• Called a “move” in computer terms• move $10, $8 #copy register $8 to $10

• Doesn’t exist in MIPS assembly language!

• add $10, $8, $0 # adds zero and $8, result in $10• Does the same thing as a move• Allows the add instruction to serve double duty!

• Many instructions have double/triple functions• sub $8, $0, $8 # negate $8• addi $12, $0, 4 # load register $12 with value 4

Page 16: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 16EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Boooooring• Straight-line code is nice,

but boring• Just arithmetic and

loads/stores based on a predetermined sequence

A = B+C

D = B+F

M[18]=D

D = B+F

D>23?

M[22] = D

C = B+A

Y

N

• Decision-making elements add some spice to the equation• Control allows programs to

make decisions based on their current state

• The most common control structure is the branch

Page 17: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 17EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Going placesConsider the lowly GoTo

if (x == y) q = 13;

if (x != y) GoTo Next;q = 13;

Next: ...

while (y < 2) y = y+1;

Loop: if (y >=2) GoTo End;y = y+1;

GoTo Loop;End: ...

if (p > q) r = 3; else r=2;

if (p>q) GoTo R3;r = 2;GoTo Next;

R3: r = 3;Next: ...

if (condition) GoTo location andGoTo location are all we need

Page 18: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 18EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Branching outif ($9 == $10) GoTo Label;

beq $9, $10, Label

if ($7 != $13) GoTo Next;

bne $7, $13, Next

beq - Branch if EQual

bne - Branch if Not Equal

Branches need: Opcode Two registers to compare Location to go to

Opcode RS RT Immediate Data

6 bits 5 bits 5 bits 16 bits

I-Type Instruction

More details on specifying the branch target in immediate data later

Page 19: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 19EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Unconditional branches - JumpsGoTo Exit;

j Exit j - Jump (unconditionally)

Opcode Immediate Data6 bits 26 bits

J-Type InstructionJumps need only an opcode and data -There is a lot more room for the data...

More details on specifying the jump target in immediate data later

Page 20: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 20EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

IF-Then Structuresif $x == $y then S1 S2

S1 should be executed if $x == $y is TrueIf $x != $y, or after S1 is executed, S2 is executed

bne $x, $y, False # if $x != $y, skip S1S1 # $x == $y, execute S1

False: S2 # either way we get here, execute S2

beq $x, $y, True # if $x == $y, then execute S1j False # $x != $y, so exit

True: S1 # $x == $y, execute S1False: S2 # either way we get here, execute S2

If you can’t express the condition as a negative, try this:

Page 21: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 21EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

IF-Then-Else Structures

if $x == $y then S1 else S2 S3

S1 should be executed if $x == $y S2 should be executed if $x != $y After executing S1 or S2, execute S3

beq $x, $y, IF # if $x == $y, goto S1, skip S2S2 # $x != $y, execute S2j Finish # now execute S3

IF: S1 # $x == $y, so execute S1Finish: S3 # either way, we do S3 afterwards

Page 22: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 22EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

While Loops

while $x == $y do S1

Execute S1 repeatedly as long as $x == $y is true

Repeat: bne $x,$y, Exit # exit if $x != $y is FalseS1 # execute body of loopj Repeat # do it all over again

Exit: # end of the loop

Repeat: S1 # execute body of loopbeq $x,$y, Repeat # do it again if $x == $y

Exit: # end of the loop

Warning: The following loop always executes at least once, no matter what $x and $y are:

Page 23: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 23EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

For Loopsfor i = $start to $finish {S1}S2

Execute S1 for all values from $start to $finish (step of 1)

add $t0, $start, $0 # copy start to i ($t0)Loop: bgt $t0, $finish, done # if i > finish, then we’re done - do S2

S1 # execute S1addi $t0, $t0, 1 # increment countj Loop # go again

done: S2

Use temporary, $t0 to hold i

Note: bgt doesn’t really exist - more on that next...

Page 24: Ch2a- 2 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what

Ch2a- 24EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University

Other conditions

slt $4, $10, $11

Set $4 = 1 if ($10 < $11), otherwise $4 = 0

if ($7 < $8) then $15 = 3; slt $1, $7, $8 # $1 <-- ($7 < $8)beq $1, $0, GoOn # If not less than, go onaddi $15, $0, 3 # ($7 < $8), so $15 <-- 3

GoOn:

Set on Less Than

if ($12 >= $3) then $4 = $2; slt $1, $12, $3 # $1 <-- ($12 < $3)bne $1, $0, GoOn # If less than, go onadd $4, $2, $0 # ($12 >= $13), so $4 = $2

GoOn:

Example: $7=4, $8=9$1 = 1 ( $7 < $8)

$1 0, Don’t branchSet $15 to 3

Example: $7=4, $8=2$1 = 0 ( $7 > $8)$1 == 0, BranchGoOn ($15 not changed)

Example: $12=4, $3=2$1 = 0 ( $12 > $3)$1 == 0, Don’t branchSet $4 to 2