【汇编语言】子程序实现递归求和
编程一个求1+2+…+N的子程序,要求用递归实现。设和不会溢出
完整代码
;This is the structure of a main module using complete segment directives
DATA SEGMENT
;......Place data declarations here
N DB ?
ANS DB ?
;......
DATA ENDS
STACK SEGMENT STACK
DB 100 DUP(0)
STACK ENDS
CODE SEGMENT 'CODE'
ASSUME CS:CODE,DS:DATA,SS:STACK
ACCUMULATE PROC NEAR
.IF N == 0
RET ; 设置结束递归条件
.ENDIF
ADD ANS, AL
INC AL
DEC N
CALL ACCUMULATE
RET ; 没有这个会IP指针跳转错误
ACCUMULATE ENDP
START:
MOV AX,DATA
MOV DS,AX
;......Place instructions here
MOV AH, 01H
INT 21H
SUB AL, '0'
MOV N, AL
INC N
MOV AX, 0H
CALL ACCUMULATE
;......
MOV AH,4CH ;exit
INT 21H
CODE ENDS
END START