Download - More Better Assembly
More Better Assembly
CSCI 136 Lab 4
Soapbox PhilosophySAD TRUTH: THE TRIAL AND ERROR
METHOD OF PROGRAMMING IS A WASTE OF TIME.
Plan your program before you sit down in front of the computer.
What are the parameters? The algorithm?What registers will I use for what?What loops do I have?What do I need to save on the stack?
Can we write this a different way?
move $a2,$s1 #$a2 = middle
addi $a2,$a2,1 #$a2 = middle+1
How about this?
add $a2, $t0, $0 # a2 = middle
addi $a2, 1 # a2 = middle + 1
Know You Instructions!Or at least look them up...
ori $t1, $0, 1 # set $t1 to 1
srl $a3, $t0, $t1 # temp / 2
sub $t1, $a3, $a2 #middle = high-low
div $t1, $t1, 2 #middle= (high-low)/2
add $t1, $t1, $a2 #$t1 =((high-low)/2)+ low
#recursive call with ourArray, tempArray, #low, middle need to save high, low, middle- #push onto stack
sub $sp, $sp,4 #adjust stack pointer
sw $a3, 0($sp) #push high onto stack
lw $a3, 0($t1) # last parameter = middle
No need to clear registers!xor $t0, $t0, $t0
xor $t1, $t1, $t1
add $t0, $a0, $t4 #ourArray[upperIndex]
add $t1, $a1, $t7 #tempArray[newIndex]
Sadly, this person cleared a very important register on accident.
Otherwise their program would have actually worked. :(
DeMorgan is Your Friend!NOT (A AND B) = (NOT A) OR (NOT B)
while(lowerIndex<=middle && upperIndex<=high)
bgt lowerIndex,middle,whileDone
bgt upperIndex,high,whileDone
This is just an example…
In many cases DeMorgan’s Law will make your life much, much easier.
Move the Stack Pointer once?addi $sp, -4 # move sp
sw $a2, 0($sp) # store low
addi $sp, -4 # move sp
sw $a3, 0($sp) # store high
addi $sp, -4 # move sp
sw $t0, 0($sp) # store middle
Move the Stack Pointer once!addi $sp, -4 # move sp
sw $a2, 0($sp) # store low
addi $sp, -4 # move sp
sw $a3, 0($sp) # store high
addi $sp, -4 # move sp
sw $t0, 0($sp) # store middle
addi $sp, $sp, -12 #move sp
sw $a2,8($sp) #store low
sw $a3,4($sp) #store high
sw $t0,0($sp) #store middle
Keep your register assignments!msfor1:
sle $t0, $t1, $a3 # count <= highbeqz $t0, msfor1End
#Set the address of ourArray[count]add $t2, $a0, $t4
#Set the address of tempArray[newIndex] add $t3, $a1, $t7 lw $t4, 0($t2) # get ourArray[count]
sw $t4, 0($t3) # tempArray[newIndex] =ourArray[count]addi $s1, $s1, 1 # move newIndex to the next elementaddi $t7, $t7, 4addi $t1, $t1, 1 # move count to the next element#move the address of the next element up one wordaddi $t4, $t4, 4j msfor1 # continue for loop
msfor1End:
Uh. This isn’t going to work!msfor1:
sle $t0, $t1, $a3 # count <= highbeqz $t0, msfor1End
#Set the address of ourArray[count]add $t2, $a0, $t4
#Set the address of tempArray[newIndex] add $t3, $a1, $t7 lw $t4, 0($t2) # get ourArray[count]
sw $t4, 0($t3) # tempArray[newIndex] =ourArray[count]addi $s1, $s1, 1 # move newIndex to the next elementaddi $t7, $t7, 4addi $t1, $t1, 1 # move count to the next element#move the address of the next element up one wordaddi $t4, $t4, 4j msfor1 # continue for loop
msfor1End:
What's wrong with this?Whileloop:
blt $t0, $t1, exitwhileloop
blt $a3, $t3, exitwhileloop
sll $t7, $t1, 2 #multiply by 4
add $t7, $t7, $a0
lw $t5, ($t7) #load lower value
sll $t7, $t3, 2
add $t7, $a0, $t7
lw $t5, ($t7) #load upper value
Stack: For all paths through your codeWhat goes on… Must come off!
MergeSort:
sub $sp, $sp,4 #|push
sw $ra, ($sp) #|$a2 and $a3
beq $a2, $a3, exit
exit:
lw $a2, 8($sp) #restore $a2
lw $ra, 12($sp)
#retrieve return instruction from stack
addi $sp, $sp, 16
jr $ra
MergeSort:
sub $sp,$sp,16
sw $s0, 0($sp) # low
sw $s1, 4($sp) # mid
sw $s2, 8($sp) # high
sw $s3, 12($ra) # ra
beq $a2,$a3,Finish
Finish:lw $s0, 0($sp) # lowlw $s1, 4($sp) # midlw $s2, 8($sp) # highlw $s3, 12($ra) # raaddi $sp,$sp, 16
lw $s1,0($sp) lw $ra,4($sp)addi $sp,$sp,8jr $ra
Stack: For all paths through your codeLeave It Like You Found It!
When this function exits... the stack pointer is 8 bytes above where it started the function at... TRANSLATION: THIS ISN'T GOING TO WORK
MergeSort:
sub $sp,$sp,16
sw $s0, 0($sp) # low
sw $s1, 4($sp) # mid
sw $s2, 8($sp) # high
sw $s3, 12($ra) # ra
beq $a2,$a3,Finish
Finish:lw $s0, 0($sp) # lowlw $s1, 4($sp) # midlw $s2, 8($sp) # highlw $s3, 12($ra) # raaddi $sp,$sp, 16
lw $s1,0($sp) lw $ra,4($sp)addi $sp,$sp,8jr $ra
What do these lines do?
Code Efficiency
#moves pointer to lower index
add $t0,$a0,$zero
add $t1,$s4,$s4 # t1 = lowerindex *2
add $t1,$t1,$t1 # t1 = lowerindex *4
add $t2,$t0,$t1 # t2 = adddress ourarray[lowerIndex]
Write this in 2 instructions.
If you need it later:You should probably save it!MergeSort:
...
beq $a2, $a3, exit #base case
sub $t0, $a3, $a2 #(high-low)
srl $t0,$t0,1 #(high-low)/2
add $t0, $t0, $a2 # + low
move $a3, $t0 How in the world am I goingto get the value of high back?
Don't Fight the FrameworkThe original call to MergeSort had low in $a2
and high in $a3
Recursive calls need to use the SAME registers for parameters ($a2 and $a3).
(Disclaimer: Occasionally you'll need to use a helper function as a wrapper.)
DO NOT USE s registers ($s0, $s1) to pass parameters for function calls!
Don't Fight the FrameworkLets try using $s2 for high and $s0 for
low
sub $t1,$s2,$s0
srl $t1,$t1,1 #divide by 2
#add low to int middle
add $s1,$t1,$s0
#making high = middle
add $s2,$s1,$zero
jal MergeSort # first call
GUESS WHAT... IT DOESN'T WORK!
"I didn't like your algorithm... so I implemented another one"
This is perfectly acceptable as long as:
You explain your algorithmYour algorithm is generally efficientYour code is well documented and works...
"I didn't like your algorithm... so I implemented another one"
It is generally not a good idea to "Freelance" if:
You don't know what you're doing...We provide guidance, hints and algorithms in
order to simplify the assignments...
e.g. Recursive MergeSort is much "cleaner" and faster than Iterative MergeSort.
These assignments are designed to give you experience with important aspects of language. (The Stack, Floating Point Types)
"I didn't like your algorithm... so I implemented another one"
To be honest...
Its much easier for us to grade and give partial credit for a non-working assignment that "followed the framework" .
In the real world- its much easier to debug and troubleshoot code which "followed the framework".
Today's Lab Project: Implementing a CRC
00000000 00000000 10000000 00000000
31 30 29 28 27 26 012
...
Input We go bit by bit left to right through word
...
25..3
Goals: Better Understanding of Masking Bit Manipulation, Shifting
Today's Lab Project: Implementing a CRC
00000000 00000000 10000000 00000000
31 30 29 28 27 26 012
...
Input We go bit by bit left to right through word
...
25..3
If you use Ethernet(802.3) (which you probably do...) You are using an error detection mechanism known as cyclic redundancy check
00000000 00000000 10000000 00000000
31 30 29 28 27 26 012
...
Input bits are processed one by one before going to Network
...
25..3
register holding 32 bit CRC
A copy of the contents of an Ethernet Packet are sent through a "machine" similar to one below before the packet is sent out on the Network (our model is simplified). A 32 bit "checksum" for the packet is calculated and is attached to the end of a packet.
00000000 00000000 10000000 00000000
31 30 29 28 27 26 012
...
Input in $a0 We go bit by bit left to right through word
...
25..3
Today's Lab Project: Implementing a CRCInput in $a0 is word to have CRC calculated overInitially $v0 should be set to all 1s i.e. 0xFFFFFFFF
Since we have 32 bits in our input... the "machine" will be cycled 32 times... (Sounds like a loop!)
Output Register $v0
Today's Lab Project: Implementing a CRCLet's Figure Out What's Going On:1. What do the black lines represent?2. What is happening in the big picture? 3. What function does this symbol represent?4. What is the truth table for the symbol's function?
00000000 00000000 10000000 00000000
31 30 29 28 27 26 012
...
Input in $a0 We go bit by bit left to right through word
...
25..3
Bits of Output Register $v0
Today's Lab Project: Implementing a CRCLet's Ask Questions:1. If the value on the Feedback line is 0... what operation occurs in register $v0?2. If value on feedback line is 1... what operations?Hint: examine xor operation...
00000000 00000000 10000000 00000000
31 30 29 28 27 26 012
...
Input in $a0 We go bit by bit left to right through word
...
25..3
This is the Feedback Line
Let's write some big picture pseudocode..For each of the 32 bits in $a0:1. get the left most bit of CRC register ($v0) (HOW?)2. get the current bit of the $a0 register that we're on (HOW?)3. xor these 2 bits4. if the xor is 0 then feedback line is 0... we should...5. if the results is 1 then we should...
00000000 00000000 10000000 00000000
31 30 29 28 27 26 012
...
Input in $a0 We go bit by bit left to right through word
...
25..3
Things to do next:1. Start thinking about subproblems... expand each of the earlier sections (1,2,3,4,5)2. What do you need registers for?- Masks- to hold our bits- loop index
00000000 00000000 10000000 00000000
31 30 29 28 27 26 012
...
Input in $a0 We go bit by bit left to right through word
...
25..3