The function F is defined as F(1) = F(2) = F(3) = 1 and for n 3,
F(n + 1) = F(n) (F(n - 1) + F(n - 2))
i.e., the (n + 1)th value is given by the product of the nth value and
the sum of the (n - 1)th and (n - 2)th values.
Write an assembly program for computing the kth value F(k),
where k is an integer bigger than 3 read from a memory location
M, and storing F(k) at memory location M.
这是我针对上述问题的代码。但是我面临有关输出的问题。对于1,2,3,4,5,6它显示正确的输出 但是对于7输出必须为72 对于8应该是1152 对于9应该是96768。 Click here to see the image of output
DATA SEGMENT
NUM1 DB 1
NUM2 DB 1
NUM3 DB 1
k DB ?
RESULT DB ?
MSG1 DB 10,13,"ENTER VALUE OF k: $"
MSG3 DB 10,13,"RESULT F(k) = $"
ENDS
CODE SEGMENT
ASSUME DS:DATA, CS:CODE
START:
MOV AX,DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,9
INT 21H
MOV AH,1
INT 21H
SUB AL,30H
MOV k,AL ; Input value of k stored in k
CMP AL,4
JL SETONE ; if input is less than 4 than set result to 1
MOV BL,3 ; Counter BL = 4
LOOPSTART:
MOV CL,NUM2 ; 2nd Last Value stored in CX
MOV DL,NUM1 ; 3rd Last Value stored in DX
ADD CL,DL ; CX= CX+DX
MOV AL,NUM3 ; Last Value stored in AX
MUL CL ; AX= AX*CX
;ADD AX,30H
AAM
MOV DH,NUM2
MOV NUM1,DH
MOV DH,NUM3
MOV NUM2,DH
MOV NUM3,AL
INC BL
MOV CL,k
CMP BL,CL
JNE LOOPSTART
JMP SHOWRESULT
SETONE: ; jump here if k is less than 4
MOV AL,1 ; set result = 1
MOV RESULT,AL
SHOWRESULT:
ADD AH,30H
ADD AL,30H
MOV BX,AX
LEA DX,MSG3
MOV AH,9
INT 21H
MOV AH,2
MOV DL,BH
INT 21H
MOV AH,2
MOV DL,BL
INT 21H
MOV AH,4CH
INT 21H
ENDS
END START
其他:答案1中也提供了解决此问题的算法。 我的作业说将结果存储在内存中。但是我想展示结果以供学习预期。
答案 0 :(得分:0)
算法:比我想的要容易。我开发的一种简单算法:
让t1=1,t2=1,t3=1
存储最后三个值,让R
存储结果。
-- Input n
IF(n<=3){
R=1
} else {
limit=n;
counter=4;
While(counter!=n){
R=t3*(t2+t1);
t1=t2;
t2=t3;
t3=R;
counter++;
}
}
Print R;