Download presentation
Presentation is loading. Please wait.
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: FEMWFEMWF 4 cycle, 1 instruction FEMWFEMWFE FEMWFEMWF FEMWFEMW FEMWFEM 1 cycle, 1 instruction
11
Call and Branch FEMWFEMWFE FEMWFEMWF FEMWFEMW FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 Bge else Mov 3,%l0 Ba next Else: mov 6,%l0 Next:
12
Call and Branch FEMWFEMWFE FEMWFEMWF FEMWFEMW FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 Bge else Mov 3,%l0 Ba next Else: mov 6,%l0 Next: cmp
13
Call and Branch FEMWFEMWFE FEMWFEMWF FEMWFEMW FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 Bge else Mov 3,%l0 Ba next Else: mov 6,%l0 Next: cmp bge
14
Call and Branch FEMWFEMWFE FEMWFEMWF FEMWFEMW FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 Bge else Mov 3,%l0 Ba next Else: mov 6,%l0 Next: 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
15
Call and Branch FEMWFEMWFE FEMWFEMWF FEMWFEMW FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 Bge else Mov 3,%l0 Ba next Else: mov 6,%l0 Next: cmp bge Mov 3 Mov 6
16
Call and Branch FEMWFEMWFE FEMWFEMWF NOP FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 bge else nop mov 3,%l0 ba next nop else: mov 6,%l0 next:
17
Call and Branch FEMWFEMWFE FEMWFEMWF NOP FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 bge else nop mov 3,%l0 ba next nop else: mov 6,%l0 next: cmp bge
18
Call and Branch FEMWFEMWFE FEMWFEMWF NOP FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 bge else nop mov 3,%l0 ba next nop else: mov 6,%l0 next: cmp bge
19
Call and Branch FEMWFEMWFE FEMWFEMWF NOP FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 bge else nop mov 3,%l0 ba next nop else: mov 6,%l0 next: cmp bge mov 6,%l0 The nop allows us to delay fetching of the next instruction
20
Removing NOP Assembly cmp %l0,5 bge else nop mov 3,%l0 ba next nop else: mov 6,%l0 next: General rule: Replace NOP with next instruction after the branch/call. Next Instruction 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:
21
Removing NOP FEMWFEMWFE FEMWFEMWF FEMWFEMW FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 bge else mov 6,%l0 mov 3,%l0 ba next nop else: next:
22
Removing NOP FEMWFEMWFE FEMWFEMWF FEMWFEMW FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 bge else mov 6,%l0 mov 3,%l0 ba next nop else: next: cmp %l0,5 bge else
23
Removing NOP FEMWFEMWFE FEMWFEMWF FEMWFEMW FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 bge else mov 6,%l0 mov 3,%l0 ba next nop else: next: cmp %l0,5 bge else mov 6,%l0
24
Removing NOP FEMWFEMWFE FEMWFEMWF FEMWFEMW FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 bge else mov 6,%l0 mov 3,%l0 ba next nop else: 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!
25
Removing NOP FEMWFEMWFE FEMWFEMWF FEMWFEMW FEMWFEM C code If(x<5) x=3; Else x = 6; Assembly cmp %l0,5 Bge,a else mov 6,%l0 mov 3,%l0 ba next nop else: 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.
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 Next instruction set out_string,%o0 mov 6, %o1 call printf,0 mov 1, %g1 ta 0
29
Do/While Loop Do first then check loop condition main() { int i=2; do { i--; }while(i>-2); } What is equivalent of this in SPARC?
30
Do/While Loop In SPARC: Mov 2,%i Do: sub i%,1,%i Test: cmp %i,-2 bg Do nop Next: NOP Removed: Mov 2,%i sub i%,1,%1 Test: cmp %i,-2 bg,a Test sub i%,1,%i 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 Low order bitsHigh order bits
33
Bitwise operation NOT – reverses the bits NOT 1001 = 0110 OR AND XOR 101 001 101 101 001 001 101 001 100
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 0101011 1010110 0101011 0010101 Shift left by 1 Shift right by 1 C X << 1 SPARC sll %r, 1, %rd C X >> 1 SPARC srl %r, 1, %rd
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.