Comparison of recursive problems with compilation and C
Owlbird's prior declaration is not intentionally stimulating everyone, never combating everyone to learn the high enthusiasm of assessment language, but because there are many feelings in this program, I feel that the use of assembly to solve mathematics, especially recursive problems, what is a painful thing, see The advantage of compilation is or is in the system recursive solution f (n) = f (n-1) f (n-2), f (1) = 1, f (2) = 1 If you use the assembly, you must draw a Sketch to analyze the situation of the stack when the child is called, that is to say that you must first have a rebuild process, in n = 3 (Note: a frame between the division) BP 1 IP 1 ----- ---- BP 2; IP 2 in the stack --------- BP 3 IP 3 BP 1 [BP-14] IP 1 ------- BP 1 [BP-6] IP At the time of the stack 2 -------- BP 2 ------> [BP-6] [BP-14] IP 3 below is the assembly source Messem macro mess; macro definition MOV AH, 09 Lea DX, Mess Int 21h Endm Data Segment Swapper DW 0; Used to make ruler Message DB 'please input the number', '$' Messerr DB 'please input the number between 0 ~ 9', '$' CRLF DB 13, 10 , '$' decdec dw 10d data ends stack_seg segment dw 128 dup (0) tos label word stack_seg ends code segment main proc far assume cs: code, ds: data, ss: stack_seg start: mov ax, stack_seg mov ss, ax mov SP, Offset TOS Push DS Sub AX, AX Push Ax Mov AX, Data MoV DS, AX MOV ES, AX MOV BX, 0 MESSEM Message; Call Macro Definition Messem CRLF; Call Macro Definition INPUT: MOV AH, 01 INT 21H CMP A L, 13 JZ RESULT1 CMP AL, '0' JB ERROR CMP AL, '9' JA Error Result: MOV AH, 0 SUB AL, 30H XCHG AX, BX MUL [DECDEC] Add BX, AX JMP INPUT ERROR: MESSEM MESSERR; Call macro definition MESSEM CRLF; call macro definition MOV BX, 0 JMP INPUT RESULT1: MESSEM CRLF; call macro definition MOV AX, BX PUSH BX Call Fact Pop BX MOV DI, AX; AX is used to save the result Messem message; call macro definition CALL Dos Main Endp Fact Proc Near; Reconstruction Subprint Push AX Push BP MOV BP, SP MOV AX, [BP 6] CMP AX, 2 JE DONE CMP AX, 1 JE EXIT FACT1: DEC AX PUSH AX CALL FACT This recursive program pays attention must have two recursive hands POP AX, [BP-6]; add the first two add AX, [BP-14] MOV [BP 2], AX JMP EXIT DONE: MOV AX, 1 MOV [BP 2], AX Push Ax Call Fact;