Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
In this lecture, students will be able to understand: Advanced Procedures; What is recursion? recursively calculating a sum; calculating a factorial; string primitive instructions; MOVSB, MOVSW, and MOVSD; CMPSB, CMPSW, and CMPSD; SCASB, SCASW, and SCASD; STOSB, STOSW, and STOSD; LODSB, LODSW, and LODSD. | CSC 221 Computer Organization and Assembly Language Lecture 26: Recursion and String Operations Lecture 25: Review Assembly Implementation of: Stack Parameters INVOKE Directive PROC Directive PROTO Directive Passing by Value or by Reference Example: Exchanging Two Integers Lecture 25: Review Assembly Implementation of: Stack Frames Explicit Access to Stack Parameters Passing Arguments by Reference (cont.) Lecture Outline Advanced Procedures: Recursion What is recursion? Recursively Calculating a Sum Calculating a Factorial String Primitive Instructions MOVSB, MOVSW, and MOVSD CMPSB, CMPSW, and CMPSD SCASB, SCASW, and SCASD STOSB, STOSW, and STOSD LODSB, LODSW, and LODSD Lecture Outline Selected String Procedures Str_length Procedure Str_copy Procedure Str_trim Procedure Str_ucase Procedure Recursion What is recursion? Recursively Calculating a Sum Calculating a Factorial What is Recursion? The process created when . . . A procedure calls itself Procedure A calls procedure B, which in | CSC 221 Computer Organization and Assembly Language Lecture 26: Recursion and String Operations Lecture 25: Review Assembly Implementation of: Stack Parameters INVOKE Directive PROC Directive PROTO Directive Passing by Value or by Reference Example: Exchanging Two Integers Lecture 25: Review Assembly Implementation of: Stack Frames Explicit Access to Stack Parameters Passing Arguments by Reference (cont.) Lecture Outline Advanced Procedures: Recursion What is recursion? Recursively Calculating a Sum Calculating a Factorial String Primitive Instructions MOVSB, MOVSW, and MOVSD CMPSB, CMPSW, and CMPSD SCASB, SCASW, and SCASD STOSB, STOSW, and STOSD LODSB, LODSW, and LODSD Lecture Outline Selected String Procedures Str_length Procedure Str_copy Procedure Str_trim Procedure Str_ucase Procedure Recursion What is recursion? Recursively Calculating a Sum Calculating a Factorial What is Recursion? The process created when . . . A procedure calls itself Procedure A calls procedure B, which in turn calls procedure A Using a graph in which each node is a procedure and each edge is a procedure call, recursion forms a cycle: Recursively Calculating a Sum CalcSum PROC cmp ecx,0 ; check counter value jz L2 ; quit if zero add eax,ecx ; otherwise, add to sum dec ecx ; decrement counter call CalcSum ; recursive call L2: ret CalcSum ENDP The CalcSum procedure recursively calculates the sum of an array of integers. Receives: ECX = count. Returns: EAX = sum Stack frame: Recursively Calculating a Sum .code main PROC mov ecx,10 ; count = 10 mov eax,0 ; holds the sum call CalcSum ; calculate sum L1: invoke dwtoa, eax, addr disp1 ; display eax invoke StdOut, addr disp1 invoke ExitProcess,0 main ENDP ;------------------------------------------ CalcSum PROC ; Calculates the sum of a list of integers ; Receives: ECX = count | Returns: EAX = sum ;------------------------------------------ cmp ecx,0 ; check counter value jz L2 ; quit if zero add eax,ecx ; otherwise, add to sum dec ecx ; .