求n的阶乘,要求用汇编语言,迭代法和递归法
发布网友
发布时间:2023-07-21 03:57
我来回答
共2个回答
热心网友
时间:2024-11-30 15:31
递归阶乘:
CODES SEGMENT
ASSUME CS:CODES
START:
MOV BX,8
CALL FACT
CALL DISPAX
MOV AX,4C00H
INT 21H
FACT PROC NEAR
AND BX,BX
JZ FACT1
PUSH BX
DEC BX
CALL FACT ;递归调用
POP BX
MUL BX ;结果在ax中
RET
FACT1: MOV AX,1
RET
FACT ENDP
;==============================================
DISPAX PROC NEAR
PUSH BX
PUSH CX
PUSH DX
PUSH SI
PUSH DS
PUSH AX
MOV AH,2
MOV DL,' '
INT 21H
POP AX
PUSH CS
POP DS
MOV BYTE PTR @NZ,0
PUSH AX
LEA SI,@DIVARR
MOV CX,5
@@1:
POP AX
MOV DX,0
MOV BX,[SI]
DIV BX
PUSH DX
CMP AL,0
JNE @@2
CMP BYTE PTR @NZ,1
JE @@2
CMP CX,1
JE @@2
MOV DL,20H
JMP @@3
@@2:
ADD AL,30H
MOV DL,AL
MOV BYTE PTR @NZ,1
@@3:
MOV AH,2
INT 21H
INC SI
INC SI
LOOP @@1
POP DX
POP DS
POP SI
POP DX
POP CX
POP BX
RET
@DIVARR DW 10000,1000,100,10,1
@NZ DB 0
DISPAX ENDP
;===============================================
CODES ENDS
END START
递推阶乘自己求着试试。
热心网友
时间:2024-11-30 15:31
STACK SEGMENT PARA STACK 'STACK'
DW 64 DUP (?)
STACK ENDS
DATA SEGMENT
N DB 8
FUNCN DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,SS:STACK,DS:DATA
MAIN PROC FAR
START: PUSH DS
MOV AX,0
PUSH AX
MOV AX,DATA
MOV DS,AX
PUSH CX
MOV AH,0
MOV AL,N
CALL FACTOR
MOV FUNCN,AX
POP CX
RET
MAIN ENDP
FACTOR PROC NEAR
PUSH AX
SUB AX,1
JNE AGAIN
POP AX
JMP FIN
AGAIN: CALL FACTOR
POP CX
MUL CL
FIN: RET
FACTOR ENDP
CODE ENDS
END START