斐波那契:汇编语言

时间:2013-02-09 15:47:21

标签: assembly x86 dos fibonacci

我正在尝试模拟斐波纳契序列,直到用户给出的数字。我得到了这个代码,但它只询问用户的一位数输入。它只接受3到9的输入。

.MODEL SMALL

.STACK 64

.DATA
        VAL1    DB      01H
        VAL2    DB      01H
        LP      DB      00H
        V1      DB      00H
        V2      DB      00H
        NL      DB      0DH,0AH,'$'

.CODE

MAIN PROC
        MOV AX,@DATA
        MOV DS,AX

        MOV AH,01H
        INT 21H
        MOV CL,AL
        SUB CL,30H
        SUB CL,2

        MOV AH,02H
        MOV DL,VAL1
        ADD DL,30H
        INT 21H

        MOV AH,09H
        LEA DX,NL
        INT 21H

        MOV AH,02H
        MOV DL,VAL2
        ADD DL,30H
        INT 21H

        MOV AH,09H
        LEA DX,NL
        INT 21H


DISP:
        MOV BL,VAL1
        ADD BL,VAL2

        MOV AH,00H
        MOV AL,BL
        MOV LP,CL
        MOV CL,10
        DIV CL
        MOV CL,LP

        MOV V1,AL
        MOV V2,AH

        MOV DL,V1
        ADD DL,30H
        MOV AH,02H
        INT 21H

        MOV DL,V2
        ADD DL,30H
        MOV AH,02H
        INT 21H

        MOV DL,VAL2
        MOV VAL1,DL
        MOV VAL2,BL

        MOV AH,09H
        LEA DX,NL
        INT 21H


        LOOP DISP

        MOV AH,4CH
        INT 21H

MAIN ENDP
END MAIN

我试图对它进行修改,以便它接受两位数的输入但是它崩溃了。这是修改后的代码:

.MODEL SMALL

.STACK 64

.DATA
Input db 13, 10, "Input the number of sequence: $"
Output db 13, 10, "The sequence is $"
        VAL1    DB      01H
        VAL2    DB      01H
        LP      DB      00H
        V1      DB      00H
        V2      DB      00H
        NL      DB     ' $'
        INP1A   DB      ?
        INP1B   DB      ?
        inputNum DB     ?

.CODE

MAIN PROC
        MOV AX,@DATA
        MOV DS,AX

        ;display Input message
        LEA DX, Input
        MOV AH, 09h
        INT 21h

        ;gets the tens digit of sequence number
        MOV AH,01h
        INT 21h
        SUB AL,'0'
        MOV INP1B,AL

        ; gets the ones digit of sequence number
        MOV AH,01h
        INT 21h
        SUB AL,'0'
        MOV INP1A,AL

        ;makes the two digits of the sequence number a whole number
        MOV AL, INP1B
        MOV CH, 10
        MUL CH
        MOV BL, INP1A
        ADD BL, AL
        MOV inputNum, BL
        ADD inputNum, '0'

        ;input --> CL
        MOV CL, inputNum
        SUB CL,30H
        SUB CL,2

        ;display Output message
        LEA DX, Output
        MOV AH, 09H
        INT 21H

        ;display first Fib number : 1
        MOV AH,02H
        MOV DL,VAL1
        ADD DL,30H
        INT 21H

        ;display space
        MOV AH,09H
        LEA DX,NL
        INT 21H

        ;display second Fib number : 1
        MOV AH,02H
        MOV DL,VAL2
        ADD DL,30H
        INT 21H

        ;display space
        MOV AH,09H
        LEA DX,NL
        INT 21H


DISP:
        ;adds the two previous numbers
        MOV BL,VAL1
        ADD BL,VAL2     ; new added number


        MOV AH,00H
        MOV AL,BL       ; new number ---> AL

        MOV CL,10   
        MOV LP,CL       ; LP will have value of CL (inputNumber)
        DIV CL
        MOV CL,LP

        MOV V1,AL       ; V1 = new number (added)
        MOV V2,AH       ; V2 = null

        MOV DL,V1       ; DL = new number
        ADD DL,30H  ; DL -> string  
        MOV AH,02H      ; print value of V1
        INT 21H

        MOV DL,V2       ; DL = V2 (null)
        ADD DL,30H      ; DL -> string
        MOV AH,02H      ; print value of V2
        INT 21H

        MOV DL,VAL2     ; DL = new added number
        MOV VAL1,DL     ; VAL1 = DL = new added number
        MOV VAL2,BL     ; VAL2 = new added number = VAL2

        ; display space
        MOV AH,09H
        LEA DX,NL
        INT 21H

        LOOP DISP

        MOV AH,4CH
        INT 21H

MAIN ENDP
END MAIN

这有什么办法可以解决这个问题吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

存在一个问题:

;makes the two digits of the sequence number a whole number
MOV AL, INP1B
MOV CH, 10
MUL CH
MOV BL, INP1A
ADD BL, AL
MOV inputNum, BL
ADD inputNum, '0'

添加'0'用于将十六进制数字转换为其ASCII表示('0'为0x30,'1'为0x31,依此类推)。如果你有两个数字,那将是0到99之间的数字。如果它大于10,添加'0'会给出一个不正确的值。但是,这不应该使您的程序崩溃。