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

36
CPSC355 Week 3 Hoang Dang
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    1

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

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

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

Suggestion

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

Documentation

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

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

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

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

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

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:

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

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

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

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

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:

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

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

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

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:

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:

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

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

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!

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.

Removing NOP

• How would we remove the following NOP

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

Removing NOP

• How would we remove the following NOP

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

Next instruction

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

Do/While Loop

• Do first then check loop conditionmain(){

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

}• What is equivalent of this in SPARC?

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:

Assignment 2

• 2 Parts:– CRC Checksum– Multiplication

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

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

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

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

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