360 likes | 503 Views
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.
E N D
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 x mov %x, %o0 !move x to %o0 mov 5, %o1 !move 5 to %o1 call .mul !call multiply mov 10, %x !initialize x mov %x, %o0 !passing x as argument to multiply mov 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: 4 cycle, 1 instruction 1 cycle, 1 instruction
Call and Branch Assembly cmp %l0,5 Bge else Mov 3,%l0 Ba next Else: mov 6,%l0 Next: C code If(x<5) x=3; Else x = 6;
Call and Branch cmp Assembly cmp %l0,5 Bge else Mov 3,%l0 Ba next Else: mov 6,%l0 Next: C code If(x<5) x=3; Else x = 6;
Call and Branch cmp bge Assembly cmp %l0,5 Bge else Mov 3,%l0 Ba next Else: mov 6,%l0 Next: C code If(x<5) x=3; Else x = 6;
Call and Branch Doesn’t move to else until end of execution cmp bge Mov Mov 3 But we’ve already fetched the move instruction belonging to the if, not else! Assembly cmp %l0,5 Bge else Mov 3,%l0 Ba next Else: mov 6,%l0 Next: C code If(x<5) x=3; Else x = 6;
Call and Branch cmp bge Mov 3 Mov 6 Assembly cmp %l0,5 Bge else Mov 3,%l0 Ba next Else: mov 6,%l0 Next: C code If(x<5) x=3; Else x = 6;
Call and Branch Assembly cmp %l0,5 bge else nop mov 3,%l0 ba next nop else: mov 6,%l0 next: C code If(x<5) x=3; Else x = 6;
Call and Branch cmp bge Assembly cmp %l0,5 bge else nop mov 3,%l0 ba next nop else: mov 6,%l0 next: C code If(x<5) x=3; Else x = 6;
Call and Branch cmp bge Assembly cmp %l0,5 bge else nop mov 3,%l0 ba next nop else: mov 6,%l0 next: C code If(x<5) x=3; Else x = 6;
Call and Branch The nop allows us to delay fetching of the next instruction cmp bge mov 6,%l0 Assembly cmp %l0,5 bge else nop mov 3,%l0 ba next nop else: mov 6,%l0 next: C code If(x<5) x=3; Else x = 6;
Removing NOP • General rule: • Replace NOP with next instruction after the branch/call. Assembly cmp %l0,5 bge else nop mov 3,%l0 ba next nop else: mov 6,%l0 next: Assembly cmp %l0,5 bge else nop mov 3,%l0 ba next nop else: mov 6,%l0 next: Assembly cmp %l0,5 bge else mov 6,%l0 mov 3,%l0 ba next nop else: next: Next Instruction
Removing NOP Assembly cmp %l0,5 bge else mov 6,%l0 mov 3,%l0 ba next nop else: next: C code If(x<5) x=3; Else x = 6;
Removing NOP cmp %l0,5 bge else Assembly cmp %l0,5 bge else mov 6,%l0 mov 3,%l0 ba next nop else: next: C code If(x<5) x=3; Else x = 6;
Removing NOP cmp %l0,5 bge else mov 6,%l0 Assembly cmp %l0,5 bge else mov 6,%l0 mov 3,%l0 ba next nop else: next: C code If(x<5) x=3; Else x = 6;
Removing NOP cmp %l0,5 bge else mov 6,%l0 Assembly cmp %l0,5 bge else mov 6,%l0 mov 3,%l0 ba next nop else: next: C code If(x<5) x=3; Else x = 6; We have a problem if x <5. We are always going to execute move 6,%l0!
Removing NOP cmp %l0,5 bge else mov 3,%l0 Assembly cmp %l0,5 Bge,a else mov 6,%l0 mov 3,%l0 ba next nop else: next: C code If(x<5) x=3; Else x = 6; 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,%o0 • mov 6, %o1 • call printf,0 • nop • mov 1, %g1 • ta 0
Removing NOP • How would we remove the following NOP • set out_string,%o0 • mov 6, %o1 • call printf,0 • nop • mov 1, %g1 • ta 0 Next instruction
Removing NOP • How would we remove the following NOP • set out_string,%o0 • mov 6, %o1 • call printf,0 • nop • mov 1, %g1 • ta 0 • set out_string,%o0 • mov 6, %o1 • call printf,0 • mov 1, %g1 • ta 0 Next instruction
Do/While Loop • Do first then check loop condition main(){ inti=2; do { i--; }while(i>-2);} • What is equivalent of this in SPARC?
Do/While Loop • NOP Removed: Mov 2,%i • sub i%,1,%1 Test: cmp %i,-2 bg,a Test • sub i%,1,%i Next: • In SPARC: Mov 2,%i Do: sub i%,1,%i Test: cmp %i,-2 bg Do nop Next:
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 High order bits Low order bits
Bitwise operation • NOT – reverses the bits NOT 1001 = 0110 • OR • AND • XOR
Bitwise operation C: NOT ~5 And 5 & 5 OR 5 | 5 XOR 5^5 SPARC: NOT not %r,%s And and %r,%s,%rd OR or %r,%s,%rd XOR xor %r,%s,%rd
Bit shifting • We can shift bits left or right C X << 1 SPARC sll %r, 1, %rd Shift left by 1 C X >> 1 SPARC srl %r, 1, %rd Shift right by 1
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