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.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
CDA 3100 Recitation Week 8. Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A li $a1, 17 li $a2, 10 jal funct li $v0,
Lecture 9: MIPS Instruction Set
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
MIPS Assembly Language
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
Assembly Language II CPSC 321 Andreas Klappenecker.
1 Warning! Unlike the previous lessons, for today's lesson you will have to listen, think and even understand (for the exam, of course). Individuals with.
CS 536 Spring Code generation I Lecture 20.
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
1 Answers to Test 2, Question 1 The functions receives the radius and height of a cylinder (גליל) and returns its volume (נפח). const float PI=3.14 float.
CHAPTER 2 ISA Instructions (logical + procedure call)
HW #1 Problems & Industrial Strength Assembly CSCI 136.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
MIPS I/O and Interrupt. .data val1:.float 0.6 val2:.float 0.8 msg_done:.asciiz "done\n".text.globl main main: li.s $f0, mfc1 $a0, $f0 jal calsqrt.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
CSCI 136 Lab 1: 135 Review.
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Simple Procedure Calls jal: performs the control transfer (unconditional jump) to the starting address of procedure, while also saving the return.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Lecture 4: MIPS Instruction Set
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
Subroutines, parameters and the stack Bryan Duggan.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Pushing the Return Address To return to the caller a subroutine must have the correct return address in $ra when the jr instruction is performed. But this.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Lab Find Live CMPUT 229.
Instructions - Type and Format
MISP Assembly.
Addressing in Jumps jump j Label go to Label op address 2 address
Solutions Chapter 2.
Logical and Decision Operations
ECE232: Hardware Organization and Design
Instruction encoding The ISA defines Format = Encoding
Logical and Decision Operations
Review.
Computer Organization and Design Assembly & Compilation
10/4: Lecture Topics Overflow and underflow Logical operations
MIPS function continued
Flow of Control -- Conditional branch instructions
3.
Program and memory layout
Systems Architecture I
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Program and memory layout
Flow of Control -- Conditional branch instructions
MIPS assembly.
Program and memory layout
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

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? 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 <= 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:

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:

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 code What 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) # 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 Stack: For all paths through your code Leave 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) # 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 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 going to get the value of high back?

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!

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!

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

"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 Input We go bit by bit left to right through word Goals: Better Understanding of Masking Bit Manipulation, Shifting

Today's Lab Project: Implementing a CRC Input We go bit by bit left to right through word If you use Ethernet(802.3) (which you probably do...) You are using an error detection mechanism known as cyclic redundancy check

Input bits are processed one by one before going to Network 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.

Input in $a0 We go bit by bit left to right through word 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

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? Input in $a0 We go bit by bit left to right through word Bits of Output Register $v0

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 Input in $a0 We go bit by bit left to right through word 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 bits 4. if the xor is 0 then feedback line is 0... we should if the results is 1 then we should Input in $a0 We go bit by bit left to right through word

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 Input in $a0 We go bit by bit left to right through word