1 / 36

CPSC355 Week 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. Conforming to Specification.

kedem
Télécharger la présentation

CPSC355 Week 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPSC355 Week 3 Hoang Dang

  2. Week 3 • Assignment 1 • Do/while loop • Assignment 2 • Binary • Bitwise operation

  3. Assignment 1 • Overall very good • Problem area: • Conforming to specification • Documenting/Commenting • Removing NOP

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

  5. Suggestion • Read spec carefully • Highlight key tasks • Check against marking guide

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

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

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

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

  10. NOP… • Linear execution: • Pipelined execution: 4 cycle, 1 instruction 1 cycle, 1 instruction

  11. 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;

  12. 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;

  13. 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;

  14. 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;

  15. 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;

  16. 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;

  17. 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;

  18. 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;

  19. 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;

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

  21. 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;

  22. 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;

  23. 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;

  24. 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!

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

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

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

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

  29. Do/While Loop • Do first then check loop condition main(){ inti=2; do { i--; }while(i>-2);} • What is equivalent of this in SPARC?

  30. 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:

  31. Assignment 2 • 2 Parts: • CRC Checksum • Multiplication

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

  33. Bitwise operation • NOT – reverses the bits NOT 1001 = 0110 • OR • AND • XOR

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

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

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

More Related