Microprocessor and Assembly Language

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
IP high IP low IP high IP low BP high BP low IP high IP low BP high BP low FL high FL low CS high CS low IP high IP low _TEXTsegment byte public ‘CODE’
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
Semantics of Calls and Returns
Kip Irvine: Assembly Language for Intel-Based Computers
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
Factorial of a number data segment x1 db 4 fact dw ? data ends
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Runtime Environments Compiler Construction Chapter 7.
L12-Computing Factorial
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Strings, Procedures and Macros
Lecture 7 A closer look at procedures Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005.
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#9) By Dr. Syed Noman.
Writing and using procedures
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
ECE291 Computer Engineering II Lecture 8 Josh Potts University of Illinois at Urbana- Champaign.
ICS312 Set 12 Subroutines: Passing Arguments Using the Stack.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Procedures Dr. Hadi Al Saadi Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java.
General Computer Science for Engineers CISC 106 Lecture 09 Dr. John Cavazos Computer and Information Sciences 03/04/2009.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Computer Science 210 Computer Organization
Chapter 14 Functions.
Format of Assembly language
Microprocessor and Assembly Language
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
Microprocessor and Assembly Language
Assembly Language Programming Part 2
Microprocessor and Assembly Language
(The Stack and Procedures)
Microprocessor and Assembly Language
CS 301 Fall 2002 Control Structures
Programming 8086 – Part IV Stacks, Macros
Microprocessor and Assembly Language
Recursion Data Structures.
The University of Adelaide, School of Computer Science
(The Stack and Procedures)
Microprocessor Lab CSL1543 0:0:2
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Computer Science 210 Computer Organization
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Multi-modules programming
Miscellaneous Topics.
Microprocessor and Assembly Language
Systems Architecture I
High-level language structures
Microprocessor and Assembly Language
(The Stack and Procedures)
Computer Organization and Assembly Language
Microprocessor and Assembly Language
Procedures and Macros.
Presentation transcript:

Microprocessor and Assembly Language Lecture-15-Recursion Muhammad Hafeez Department of Computer Science GC University Lahore

Today’s Agenda Recursion Theory in Assembly Implementation in 8086 Environment

Recursion A process is recursive if its defined in terms of itself Main Characteristics of a recursive process The Main Problem is break down to simpler problem, each of these simpler problem are solved exactly the way, main problem is solved There must be an escape case One sub problem is solved, proceed to next step of solving main problem

Example: Factorial Non-recursive way Factorial (N) = N x (N-1) x (N-2)x…x3x2x1 Recursive Way: Factorial (N) = N x Factorial (N-1)

Algorithm for Factorial: PROCEDURE FACTORIAL (INPUT:N, OUTPUT: RESULT) IF N =1 THEN RESULT=1 ELSE CALL FACTORIAL(INPUT:N-1, OUTPUT:RESULT) RESULT = N X RESULT END_IF RETURN

Algorithm for Find Max in An Array: If N = 1 then largest entry is A[1], else largest entry is either A[N] or largest of A[1] …A [N-1] PROCEDURE FIND_MAX(INPUT:N, OUPUT:MAX) IF N = 1 THEN MAX = A[1] ELSE CALL FIND_MAX(N-1,MAX) IF A[N] > MAX THEN MAX = A[N] MAX = MAX END_IF RETURN

Revision-Passing Parameters to Procedure on the Stack Write a procedure that adds two words and return the result in AX Pass words to procedure using Stack

The Activation Record Each new call to a recursive procedure require the parameters and local variables to be reinitialized for that particular call When the call is completed the procedure resumes the previous call, on the point it left off. Hence, the procedure needs to ‘remember’ the point, the parameters and local values of the previous call, this is called ‘the activation record’

Example: Activation Record First Call Activation Record 2nd Call

Example: Activation Record First Call Suppose, 3rd call is the escape case, result of this call is computed and stored in a register or memory location to be available to 2nd call and so on..

Implementation of Factorial using Recursion: MODEL SMALL .STACK 100H .CODE MAIN PROC MOV AX,3 ;N = 3 PUSH AX ;N on stack CALL FACTORIAL ;AX has 3 factorial MOV AH,4CH INT 21H ;dos return MAIN ENDP

Implementation of Factorial using Recursion: FACTORIAL PROC NEAR ; computes N factorial ; input: stack on entry - ret. addr.(top), N ; output: AX PUSH BP ;save BP MOV BP,SP ;BP pts to stacktop ;if CMP WORD PTR[BP+4],1 ;N = 1? JG END_IF ;no , N>1 ;then MOV AX,1 ;result = 1 JMP RETURN ;to to return END_IF: MOV CX,[BP+4] ;get N DEC CX ;N-1 PUSH CX ;save on stack CALL FACTORIAL ;recursive call MUL WORD PTR[BP+4] ;RESULT = N*RESULT RETURN: POP BP ;restore BP RET 2 ;return and discard N FACTORIAL ENDP END MAIN