Top Banner
CPSC355 Week 3 Hoang Dang
36

CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

CPSC355 Week 3

Hoang Dang

Page 2: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Week 3

• Assignment 1• Do/while loop• Assignment 2• Binary • Bitwise operation

Page 3: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Assignment 1

• Overall very good• Problem area:– Conforming to specification– Documenting/Commenting– Removing NOP

Page 4: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Conforming to Specification

• Many missed some parts of the specification– Put maximum in %L0– Print x, y, and max on each iteration– Print registers at key points

Page 5: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Suggestion

• Read spec carefully• Highlight key tasks• Check against marking guide

Page 6: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Documentation

• What does the program do?• Who coded this?• When was this done?

Page 7: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Commenting

• Why, not What– In general, tell me why you are doing it. Not what

the code is doing– We assume the reader knows the language to

determine what the statements do

Page 8: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Commenting…

mov 10, %x !move 10 to xmov %x, %o0 !move x to %o0mov 5, %o1 !move 5 to %o1call .mul !call multiply

mov 10, %x !initialize xmov %x, %o0 !passing x as argument to multiplymov 5, %o1 !passing 5 as argument to multiply(x*5)call .mul

Page 9: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

NOP

• Many have problems removing the nop– SPARC instruction processing:• F – Fetch the instruction• E – Execute the instruction• M – Load/Store data to memory• W – Write to register

• SPARC is a pipelined architecture

Page 10: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

NOP…

• Linear execution:

• Pipelined execution:

F E M W F E M W F

4 cycle, 1 instruction

F E M W F E M W F E

F E M W F E M W F

F E M W F E M W

F E M W F E M

1 cycle, 1 instruction

Page 11: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Call and Branch

F E M W F E M W F E

F E M W F E M W F

F E M W F E M W

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5Bge elseMov 3,%l0Ba nextElse: mov 6,%l0Next:

Page 12: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Call and Branch

F E M W F E M W F E

F E M W F E M W F

F E M W F E M W

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5Bge elseMov 3,%l0Ba nextElse: mov 6,%l0Next:

cmp

Page 13: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Call and Branch

F E M W F E M W F E

F E M W F E M W F

F E M W F E M W

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5Bge elseMov 3,%l0Ba nextElse: mov 6,%l0Next:

cmp

bge

Page 14: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Call and Branch

F E M W F E M W F E

F E M W F E M W F

F E M W F E M W

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5Bge elseMov 3,%l0Ba nextElse: mov 6,%l0Next:

cmp

bge

Mov

Doesn’t move to else until end of execution

But we’ve already fetched the move instruction belonging to the if, not else!

Mov 3

Page 15: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Call and Branch

F E M W F E M W F E

F E M W F E M W F

F E M W F E M W

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5Bge elseMov 3,%l0Ba nextElse: mov 6,%l0Next:

cmp

bge

Mov 3

Mov 6

Page 16: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Call and Branch

F E M W F E M W F E

F E M W F E M W F

NOP

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5bge elsenopmov 3,%l0ba nextnopelse: mov 6,%l0next:

Page 17: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Call and Branch

F E M W F E M W F E

F E M W F E M W F

NOP

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5bge elsenopmov 3,%l0ba nextnopelse: mov 6,%l0next:

cmpbge

Page 18: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Call and Branch

F E M W F E M W F E

F E M W F E M W F

NOP

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5bge elsenopmov 3,%l0ba nextnopelse: mov 6,%l0next:

cmpbge

Page 19: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Call and Branch

F E M W F E M W F E

F E M W F E M W F

NOP

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5bge elsenopmov 3,%l0ba nextnopelse: mov 6,%l0next:

cmpbge

mov 6,%l0

The nop allows us to delay fetching of the next instruction

Page 20: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Removing NOP

Assembly

cmp %l0,5bge elsenopmov 3,%l0ba nextnopelse: mov 6,%l0next:

• General rule:Replace NOP with next instruction after the branch/call.

Next Instruction

Assembly

cmp %l0,5bge elsenopmov 3,%l0ba nextnopelse: mov 6,%l0next:

Assembly

cmp %l0,5bge elsemov 6,%l0mov 3,%l0ba nextnopelse: next:

Page 21: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Removing NOP

F E M W F E M W F E

F E M W F E M W F

F E M W F E M W

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5bge elsemov 6,%l0 mov 3,%l0ba nextnopelse: next:

Page 22: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Removing NOP

F E M W F E M W F E

F E M W F E M W F

F E M W F E M W

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5bge elsemov 6,%l0 mov 3,%l0ba nextnopelse: next:

cmp %l0,5

bge else

Page 23: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Removing NOP

F E M W F E M W F E

F E M W F E M W F

F E M W F E M W

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5bge elsemov 6,%l0 mov 3,%l0ba nextnopelse: next:

cmp %l0,5

bge else

mov 6,%l0

Page 24: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Removing NOP

F E M W F E M W F E

F E M W F E M W F

F E M W F E M W

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5bge elsemov 6,%l0 mov 3,%l0ba nextnopelse: next:

cmp %l0,5

bge else

mov 6,%l0

We have a problem if x <5. We are always going to execute move 6,%l0!

Page 25: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Removing NOP

F E M W F E M W F E

F E M W F E M W F

F E M W F E M W

F E M W F E M

C code

If(x<5) x=3;Else x = 6;

Assembly

cmp %l0,5Bge,a elsemov 6,%l0 mov 3,%l0ba nextnopelse: next:

cmp %l0,5

bge else

mov 3,%l0

We have a problem if x <5. We are always going to execute mov 6,%l0!

We can annul it by placing an a after the branch.

Page 26: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Removing NOP

• How would we remove the following NOP

set out_string,%o0mov 6, %o1call printf,0nopmov 1, %g1ta 0

Page 27: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Removing NOP

• How would we remove the following NOP

set out_string,%o0mov 6, %o1call printf,0nopmov 1, %g1ta 0

Next instruction

Page 28: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Removing NOP

• How would we remove the following NOP

set out_string,%o0mov 6, %o1call printf,0nopmov 1, %g1ta 0

Next instruction

set out_string,%o0mov 6, %o1call printf,0mov 1, %g1ta 0

Page 29: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Do/While Loop

• Do first then check loop conditionmain(){

int i=2;do{ i--;}while(i>-2);

}• What is equivalent of this in SPARC?

Page 30: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Do/While Loop

• In SPARC:Mov 2,%iDo: sub i%,1,%iTest: cmp %i,-2 bg Do nopNext:

• NOP Removed:Mov 2,%isub i%,1,%1Test: cmp %i,-2 bg,a Test sub i%,1,%iNext:

Page 31: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Assignment 2

• 2 Parts:– CRC Checksum– Multiplication

Page 32: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Binary

• Base 2 [0,1]• 100001111000000

• Convert binary to decimal– 1011 = (1*2^0)+(1*2^1)+(0*2^2)+(1*2^3)

• Signed bit is used to tell negative numbers– 1000011

Low order bitsHigh order bits

Page 33: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Bitwise operation

• NOT – reverses the bits NOT 1001 = 0110

• OR

• AND

• XOR

1 0 1

0 0 1

1 0 1

1 0 1

0 0 1

0 0 1

1 0 1

0 0 1

1 0 0

Page 34: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Bitwise operation

C:NOT ~5And 5 & 5OR 5 | 5XOR 5^5

SPARC:NOT not %r,%sAnd and %r,%s,%rdOR or %r,%s,%rdXOR xor %r,%s,%rd

Page 35: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Bit shifting

• We can shift bits left or right0 1 0 1 0 1 1

1 0 1 0 1 1 0

0 1 0 1 0 1 1

0 0 1 0 1 0 1

Shift left by 1

Shift right by 1

CX << 1

SPARCsll %r, 1, %rd

CX >> 1

SPARC srl %r, 1, %rd

Page 36: CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.

Bit shifting

• There are two types of shift– Arithmetic – Keeps the signed bit intact – Logical – ignores the signed bit

• In SPARC– sll , srl are logical shifts– sra is the arithmetic shift