Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

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 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

3 Ch2a- 3 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Binary review Binary representations of numbers consist of only 1’s and 0’s 0101 2 = 5 10 1000 2 = 8 10 1111111 2 = 127 10 Unsigned (always positive) binary numbers 2 10 = 1024 = 1K  1,000 2 20 = 1,048,576 = 1M  1,000,000 2 30 = 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,000 2 20 = 1,048,576 = 1M  1,000,000 2 30 = 1,073,741,824 = 1G  1,000,000,000 Being fluent in binary is highly underrated on the dating scene Binary Facts:

4 Ch2a- 4 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Converting between binary and hex Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Group bits together in groups of 4 Assign the appropriate hex digit to each group Done 1000110010110010000111 2 --> 10 0011 0010 1100 1000 0111 2 10 0011 0010 1100 1000 0111 2 --> 2 3 2 C 8 7 = 232C87 16 Hexadecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F

5 Ch2a- 5 EE/CS/CPE 3760 - Computer Organization  Seattle 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 Assemblers translate from one language to another.

6 Ch2a- 6 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University MIPS Instructions addA, B, C Operation Destination Sources A = B + C subD, A, B D = A - B In MIPS, All register-to-register arithmetic instructions have three operands. In MIPS, All register-to-register arithmetic instructions have three operands.

7 Ch2a- 7 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Operands addA, 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

8 Ch2a- 8 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Registering MIPS has 32 general-purpose registers $0 through $31 47 $2 Each register holds 32 bits 0 to 2 32 -1 (4 billion) if unsigned -2 31 to +2 31 -1 (-2 billion to +2 billion) if signed Most registers can hold any value, for any purpose Exception: $0 is always zero!

9 Ch2a- 9 EE/CS/CPE 3760 - Computer Organization  Seattle 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 #NamePurpose $0$zeroConstant zero $1$atReserved for assembler $2$v0Function return value $3$v1 $4$a0Function parameter $5$a1 $6$a2 $7$a3 $8$t0Temporary – Caller-saved $9$t1 $10$t2 $11$t3 $12$t4 $13$t5 $14$t6 $15$t7 #NamePurpose $16$s0Temporary – Callee-saved $17$s1 $18$s2 $19$s3 $20$s4 $21$s5 $22$s6 $23$s7 $24$t8Temporary – Caller-saved $25$t9 $26$k0Reserved for OS $27$k1 $28$gpGlobal pointer $29$spStack pointer $30$fpFrame pointer $31$raFunction return address

10 Ch2a- 10 EE/CS/CPE 3760 - Computer Organization  Seattle 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

11 Ch2a- 11 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Complicated arithmetic F = (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 compiler assigns variables to registers Note: Typically, the compiler assigns variables to registers $12 = ($8 + $9) - ($10 + $11) We don’t have a 5-operand add/subtract instruction! Use temporary variables to solve the problem. add$13, $8, $9# $13 <-- A + B add$14, $10, $11 # $14 <-- C + D sub$12, $13, $14# F <-- (A+B) - (C+D) $13 and $14 are temporary variables

12 Ch2a- 12 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Getting to the bits of it all We’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 bits5 bits 6 bits 0 = Add$8$9$13032=Add 32 bits, total R-Type Instruction 000000 01000 01001 01101 00000 100000

13 Ch2a- 13 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Staying Regular R-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 key to high-performance Opcode RS RT RD ShAmt Function 6 bits5 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

14 Ch2a- 14 EE/CS/CPE 3760 - Computer Organization  Seattle 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 bits5 bits 16 bits 8 10 12 4 001000 01010 01100 0000 0000 0000 0100 I-Type Instruction add immediate Instructions with constant data in them are called immediate instructions addi $12, $10, 4 # Reg. 12 <-- Reg. 10 + 4

15 Ch2a- 15 EE/CS/CPE 3760 - Computer Organization  Seattle 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

16 Ch2a- 16 EE/CS/CPE 3760 - Computer Organization  Seattle 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

17 Ch2a- 17 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Going places Consider 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 and GoTo location are all we need if (condition) GoTo location and GoTo location are all we need

18 Ch2a- 18 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Branching out if ($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 bits5 bits 16 bits I-Type Instruction More details on specifying the branch target in immediate data later

19 Ch2a- 19 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University Unconditional branches - Jumps GoTo Exit; jExit j - Jump (unconditionally) Opcode Immediate Data 6 bits26 bits J-Type Instruction Jumps 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

20 Ch2a- 20 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University IF-Then Structures if $x == $y then S1 S2 S1 should be executed if $x == $y is True If $x != $y, or after S1 is executed, S2 is executed bne $x, $y, False # if $x != $y, skip S1 S1# $x == $y, execute S1 False:S2# either way we get here, execute S2 beq $x, $y, True # if $x == $y, then execute S1 jFalse# $x != $y, so exit True:S1# $x == $y, execute S1 False:S2# either way we get here, execute S2 If you can’t express the condition as a negative, try this:

21 Ch2a- 21 EE/CS/CPE 3760 - Computer Organization  Seattle 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 S2 S2 # $x != $y, execute S2 jFinish# now execute S3 IF:S1 # $x == $y, so execute S1 Finish:S3# either way, we do S3 afterwards

22 Ch2a- 22 EE/CS/CPE 3760 - Computer Organization  Seattle 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 False S1# execute body of loop jRepeat# do it all over again Exit:# end of the loop Repeat:S1# execute body of loop beq$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:

23 Ch2a- 23 EE/CS/CPE 3760 - Computer Organization  Seattle Pacific University For Loops for 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 S1 addi$t0, $t0, 1# increment count jLoop# go again done:S2 Use temporary, $t0 to hold i Note: bgt doesn’t really exist - more on that next...

24 Ch2a- 24 EE/CS/CPE 3760 - Computer Organization  Seattle 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 on addi$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 on add$4, $2, $0# ($12 >= $13), so $4 = $2 GoOn: Example: $7=4, $8=9 $1 = 1 ( $7 < $8) $1  0, Don’t branch Set $15 to 3 Example: $7=4, $8=2 $1 = 0 ( $7 > $8) $1 == 0, Branch GoOn ($15 not changed) Example: $12=4, $3=2 $1 = 0 ( $12 > $3) $1 == 0, Don’t branch Set $4 to 2


Download ppt "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."

Similar presentations


Ads by Google