1 / 31

More Better Assembly

More Better Assembly. CSCI 136 Lab 4. Soapbox Philosophy. SAD 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?

riva
Download Presentation

More Better Assembly

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. More Better Assembly CSCI 136 Lab 4

  2. Soapbox Philosophy • SAD 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?

  3. Can we write this a different way? • move $a2,$s1 #$a2 = middle • addi $a2,$a2,1 #$a2 = middle+1

  4. How about this? • add $a2, $t0, $0 # a2 = middle • addi $a2, 1 # a2 = middle + 1

  5. Know You Instructions!Or at least look them up... • ori $t1, $0, 1 # set $t1 to 1 • srl $a3, $t0, $t1 # temp / 2

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

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

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

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

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

  11. Keep your register assignments! • msfor1: • sle $t0, $t1, $a3 # count <= high • beqz $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 element • addi $t7, $t7, 4 • addi $t1, $t1, 1 # move count to the next element • #move the address of the next element up one word • addi $t4, $t4, 4 • j msfor1 # continue for loop • msfor1End:

  12. Uh. This isn’t going to work! • msfor1: • sle $t0, $t1, $a3 # count <= high • beqz $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 element • addi $t7, $t7, 4 • addi $t1, $t1, 1 # move count to the next element • #move the address of the next element up one word • addi $t4, $t4, 4 • j msfor1 # continue for loop • msfor1End:

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

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

  15. Stack: For all paths through your codeLeave It Like You Found It! Finish: lw $s0, 0($sp) # low lw $s1, 4($sp) # mid lw $s2, 8($sp) # high lw $s3, 12($ra) # ra addi $sp,$sp, 16 lw $s1,0($sp) lw $ra,4($sp) addi $sp,$sp,8 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 When this function exits... the stack pointer is 8 bytes above where it started the function at... TRANSLATION: THIS ISN'T GOING TO WORK

  16. What do these lines do? Finish: lw $s0, 0($sp) # low lw $s1, 4($sp) # mid lw $s2, 8($sp) # high lw $s3, 12($ra) # ra addi $sp,$sp, 16 lw $s1,0($sp) lw $ra,4($sp) addi $sp,$sp,8 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

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

  18. 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 going to get the value of high back?

  19. Don't Fight the Framework • The 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!

  20. Don't Fight the Framework • Lets 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!

  21. "I didn't like your algorithm... so I implemented another one" • This is perfectly acceptable as long as: • You explain your algorithm • Your algorithm is generally efficient • Your code is well documented and works...

  22. "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)

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

  24. Today's Lab Project: Implementing a CRC Goals: Better Understanding of Masking Bit Manipulation, Shifting ... ... 30 29 28 27 26 2 1 31 0 25..3 Input We go bit by bit left to right through word 00000000 00000000 10000000 00000000

  25. Today's Lab Project: Implementing a CRC If you use Ethernet(802.3) (which you probably do...) You are using an error detection mechanism known as cyclic redundancy check ... ... 30 29 28 27 26 2 1 31 0 25..3 Input We go bit by bit left to right through word 00000000 00000000 10000000 00000000

  26. 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. register holding 32 bit CRC ... ... 30 29 28 27 26 2 1 31 0 25..3 Input bits are processed one by one before going to Network 00000000 00000000 10000000 00000000

  27. Today's Lab Project: Implementing a CRC Input in $a0 is word to have CRC calculated over Initially $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 ... ... 30 29 28 27 26 2 1 31 0 25..3 Input in $a0 We go bit by bit left to right through word 00000000 00000000 10000000 00000000

  28. Today's Lab Project: Implementing a CRC Let'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? Bits of Output Register $v0 ... ... 30 29 28 27 26 2 1 31 0 25..3 Input in $a0 We go bit by bit left to right through word 00000000 00000000 10000000 00000000

  29. Today's Lab Project: Implementing a CRC Let'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... This is the Feedback Line ... ... 30 29 28 27 26 2 1 31 0 25..3 Input in $a0 We go bit by bit left to right through word 00000000 00000000 10000000 00000000

  30. 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 bits 4. if the xor is 0 then feedback line is 0... we should... 5. if the results is 1 then we should... ... ... 30 29 28 27 26 2 1 31 0 25..3 Input in $a0 We go bit by bit left to right through word 00000000 00000000 10000000 00000000

  31. 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 ... ... 30 29 28 27 26 2 1 31 0 25..3 Input in $a0 We go bit by bit left to right through word 00000000 00000000 10000000 00000000

More Related