Assembly 06 Recursion.

Slides:



Advertisements
Similar presentations
Chapter 7 Sorting Part II. 7.3 QUICK SORT Example left right pivot i j 5 > pivot and should go to the other side. 2 < pivot and should go to.
Advertisements

COMP 2003: Assembly Language and Digital Logic
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
CS2422 Assembly Language & System Programming October 26, 2006.
Flow Control Instructions
Quicksort.
Kip Irvine: Assembly Language for Intel-Based Computers
S: Application of quicksort on an array of ints: partitioning.
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6)
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
COSC 456 Lesson 8 Cool Codes ADD AL,SIAL AL + SI ADD AL,[SI]AL AL + [SI] INC BXBX BX + 1 INC [BX]Ambiguity error INC BYTE PTR [BX][BX] [BX] + 1 INC WORD.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Strings, Procedures and Macros
Arithmetic Flags and Instructions
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Assembly 08 Interrupts. Introduction Interrupts are similar to procedures –They are used to alter a program’s control flow –The interrupt service is also.
Control Structure vs. Assembly Language NASM. If-then-else If conditional then then_actions jump to endif else else_actions endif.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Assembly Language Addressing Modes. Introduction CISC processors usually supports more addressing modes than RISC processors. –RISC processors use the.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Lecture 10 CS Sorting Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms. First, sorting.
Recursion Riley Chapter 14 Sections 14.1 – 14.5 (14.6 optional)
Stack Operations Dr. Hadi AL Saadi.
Reading Condition Codes (Cont.)
Computer Architecture CST 250
Presentation on Real Mode Memory Addressing
Assembly Lab 3.
Data Transfers, Addressing, and Arithmetic
Lecture No.45 Data Structures Dr. Sohail Aslam.
Sorting Why? Displaying in order Faster Searching Categories Internal
Homework Reading Labs PAL, pp
Aaron Miller David Cohen Spring 2011
Assembly Language for x86 Processors 6th Edition
Programming in Java: lecture 10
Lecture 4 Control Flow Structures (LOOPS)
Assembly Language Programming Part 2
QuickSort QuickSort is often called Partition Sort.
(The Stack and Procedures)
Data Structures and Algorithms
Quicksort 1.
Y86 Processor State Program Registers
Computer Organization and Assembly Language
Advanced Sorting Methods: Shellsort
Microprocessor and Assembly Language
Practical Session 4.
(The Stack and Procedures)
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Multi-modules programming
Searching.
Miscellaneous Topics.
X86 Assembly Review.
MIPS function continued
UNIT-II Assembly Language Programs Involving Logical
Quicksort.
Main() { int fact; fact = Factorial(4); } main fact.
(The Stack and Procedures)
ICS51 Introductory Computer Organization
Computer Architecture and System Programming Laboratory
Advanced Sorting Methods: Shellsort
Procedures and Macros.
Presentation transcript:

Assembly 06 Recursion

A recursive procedure calls itself Directly : the procedure calls itself directly Indirectly : the procedure P makes a call to procedure Q, which in turn calls procedure P. Example: n! = n(n-1)! 1 ; n = 0 factorial(i) = n * factorial(n − 1); n > 0 int fact(int n) { if (n == 0) return(1); return(n * fact(n-1)); }

factorial

factorial %include "io.mac" .DATA prompt_msg db "Please enter a positive integer: ",0 output_msg db "The factorial is: ",0 error_msg db "Sorry! Not a positive number. Try again.",0 .CODE .STARTUP PutStr prompt_msg ; get n GetInt BX ; read number into BX call fact PutStr output_msg ; output n! PutInt AX .EXIT Example

factorial ; It receives a positive integer n in BX register. ; It returns n! in AX register. .CODE fact: cmp BL,1 ; if N > 1, recourse jg one_up mov AX,1 ; return 1 for N < 2 ret ; terminate recursion one_up: dec BL ; recurse with (N-1) call fact inc BL mul BL ; AX = AL * BL ret

QuickSort

Quick Sort public void quickSort(int array[], int start, int end) { int i = start; int k = end; if (end - start >= 1) { // more than on element int pivot = array[start]; // set the pivot as the first element while (k > i) { while (array[i] <= pivot && i <= end && k > i) // left to right scan i++; while (array[k] > pivot && k >= start && k >= i) // right to left scan k--; // element not greater than the pivot if (k > i) swap(array, i, k); // the right index, swap the corresponding elements } swap(array, start, k); // after cross, swap the last element in quickSort(array, start, k - 1); // quicksort the left partition quickSort(array, k + 1, end); // quicksort the right partition else { // one element return;

Quick Sort QuckSort: pushad cmp EDI, ESI jle qsort_done ; end recursion if hi <= lo ; save hi and lo for later use mov ECX, ESI mov EDX, EDI mov AX,[EBX+EDI*2] ; AX = xsep lo_loop: cmp [EBX+ESI*2],AX ; jge lo_loop_done ; LO while loop inc ESI jmp lo_loop lo_loop_done: dec EDI ; hi = hi-1 hi_loop: jle sep_done cmp [EBX+EDI*2],AX ; HI while loop jle hi_loop_done ; dec EDI ; jmp hi_loop ;

Quick Sort hi_loop_done: xchg AX, [EBX+ESI*2] xchg AX, [EBX+EDI*2] ; x[i] <=> x[j] jmp lo_loop sep_done: xchg AX, [EBX+EDX*2] ; x[i] <=> x[hi] dec ESI mov EDI, ESI ; hi = i-1 ; ESI value will be modified in the next statement, as the original ESI value is in EDI, ; EDI value is used to get i+1 value for the second qsort call. mov ESI,ECX call qsort inc EDI ;EDI has the i value inc EDI mov ESI,EDI ; lo = i+1 mov EDI,EDX qsort_done: popad ret