Assembly Code Example Selection Sort.

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

Lecture 5: MIPS Instruction Set
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.
Lecture 9: MIPS Instruction Set
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
Solution 2nd Exam.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
Computer Organization CS224
Computer Architecture CSCE 350
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
CS3350B Computer Architecture Winter 2015 Lecture 4
Lecture 8: MIPS Instruction Set
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
CSCE 212 Quiz 2 – 2/2/11 1.What is the purpose of the jal instruction? 2.What are the two conditional branching (if, goto; not the slt instruction) instructions.
1 Lecture 5: MIPS Examples Today’s topics:  the compilation process  full example – sort in C Reminder: 2 nd assignment will be posted later today.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
CHAPTER 2 ISA Instructions (logical + procedure call)
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
CPEG323 Homework Review I Long Chen October, 17 th, 2005.
Lecture 5: Procedure Calls
Computer Architecture & Operations I
Lecture 6: Assembly Programs
CS2100 Computer Organisation
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
CSCI206 - Computer Organization & Programming
Instructions - Type and Format
MIPS Procedures.
Addressing in Jumps jump j Label go to Label op address 2 address
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
MIPS Functions.
Lecture 7: Examples, MARS, Arithmetic
Logical and Decision Operations
MIPS Procedures.
Computer Organization and Design Assembly & Compilation
MIPS Coding.
MIPS Functions.
Lecture 6: Assembly Programs
10/6: Lecture Topics C Brainteaser More on Procedure Call
MIPS Functions.
Conditional Branching (beq)
Presentation transcript:

Assembly Code Example Selection Sort

Swap Function in C Swap(int & num1, int & num2) // post: values of num1 and num2 have // been swapped { int temp; temp = num1; num1 = num2; num2 = temp; }

Swap Function in Assembly Code Swap: lw $t0, 0($a0) # t0 gets num1 lw $t1, 0($a1) # t1 gets num2 sw $t0, 0($a1) # num2 gets t0 sw $t1, 0($a0) # num1 gets t1 jr $ra

Selection Sort function in C void SelSort(int v[], int length) { int indexMin; for(int s = 0; s < length-1; s++){ indexMin = s; for(int k = s+1; k < length; k++){ if(v[k] < v[indexMin]) indexMin = k; } swap(v[s], v[indexMin]);

Selection Sort in Assembly Code Do we make a subsidiary function call? Yes, must save state, use s registers What must go into s registers? Variables whose values must be retained across function calls v base, length, length-1, s Other variables can use t registers k, minIndex, temps for address/branch computations Construct code by units (if, for, for)

Selection Sort in Assembly Code # register assignments # v base in $s0 (move from $a0) # length in $s1 (move from $a1) # length-1 in $s2 (compute from n) # s in $s3 (initialize to 0) # minIndex in $t0 # k in $t1

Selection Sort in Assembly Code # if(v[k] < v[minIndex]) { minIndex = k } # sll $t3, $t1, 2 # t3 = 4 * k add $t3, $s0, $t3 # t3 = address of v[k] sll $t4, $t0, 2 # t4 = 4 * minIndex add $t4, $s0, $t4 # t4 = addr of v[minIndex] lw $t5, 0($t3) # t5 = v[k] lw $t6, 0($t4) # t6 = v[minIndex] slt $t2, $t5, $t6 # v[k] < v[minIndex]? beq $t2, $zero, endif # if not skip if part add $t0, $t1, $zero # minIndex = k endif:

Selection Sort in Assembly Code # for(k = s+1; k < length; k++) # { if …} # addi $t1, $s3, 1 # k = s + 1 fork: slt $t2, $t1, $s1 # k < length ? beq $t2, $zero, endfork # if not, exit # { if…} addi $t1, $t1, 1 # k++ j fork endfork:

Selection Sort in Assembly Code # for(s = 0; s < length-1; s++) # minIndex = s; for k …; swap(v[s], v[minIndex]); # add $s3, $zero, $zero # s = 0 fors: slt $t2, $s3, $s2 # s < length-1 ? beq $t2, $zero, endfors # if not, exit loop add $t0, $s3, $zero # minIndex = s # for k ... sll $t3, $s3, 2 # compute array addresses add $a0, $s0, $t3 # and store as parameters sll $t3, $t0, 2 add $a1, $s0, $t3 jal Swap # call swap function addi $s3, $s3, 1 # s++ j fors endfors:

Selection Sort in Assembly Code # save state # restore state addi $sp, $sp, -20 sw $ra, 16($sp) lw $ra, 16($sp) sw $s3, 12($sp) lw $s3, 12($sp) sw $s2, 8($sp) lw $s2, 8($sp) sw $s1, 4($sp) lw $s1, 4($sp) sw $s0, 0($sp) lw $s0, 0($sp) addi $sp, $sp, 20 # initialize add $s0, $a0, $zero # return add $s1, $a1, $zero jr $ra addi $s2, $s1, -1

Assemble / Link / Load / Run translates assembly code to object code (machine lang) pseudo instructions are replaced by actual machine instructions e.g. move $t0, $s1 becomes add $t0, $s1, $zero addresses are set relative to start of code segment relocation done by linker internal addresses which need to be resolved are kept in a symbol table external addresses which are needed are listed e.g. function calls not part of this segment

Assemble / Link / Load / Run Link puts together different object code segments resolves addresses with function calls across segments resolves final addresses of labels places data in data segment and resolves addresses Load place code into main memory Run start at label main