MASM&amp ;;的奇怪错误斐波那契

时间:2016-10-07 16:02:28

标签: assembly masm fibonacci irvine32

我现在很确定错误是在sub ecx,3 !!!我是正确的!我在前两次调用之后创建了一个cmp ecx函数,然后在fib循环之前查看ecx = 0以及它是否跳到最后,因为它已经打印了3个值

INCLUDE Irvine32.inc

.data
myName              BYTE    "Nathan Campbell ", 0
programTitle        BYTE    "Fibonacci Numbers by ", 0
prompt_1            BYTE    "What is your name? ", 0
prompt_2            BYTE    "Enter the number of Fibonacci terms you would like to see. Please enter a number between [1 - 46]", 0
ec_prompt           BYTE    "EC: Doing something awesome: Setting text color to teal-ish", 0
numFib              DWORD   ?
prev1               DWORD   ?
prev2               DWORD   ?
spaces              BYTE    "     ",0
goodbye             BYTE    "Goodbye,  ", 0
firstTwo            BYTE    "1     1     ", 0
firstOne            BYTE    "1", 0
temp                DWORD   ?
moduloFive          DWORD   5
UPPERLIMIT = 46
LOWERLIMIT = 1

;user's name
buffer              BYTE 21 DUP(0)
byteCount           DWORD   ?

;greet user
hi                  BYTE    "Hi, ",0

;validation
tooHighError        BYTE    "The number you entered is too high! It must be 46 or below. ", 0
tooLowError         BYTE    "The number you entered is too low! It must be 1 or above. ", 0

;EC -> Doing something awesome: Setting  Background Color and Text Color
val1 DWORD 11
val2 DWORD 16

.code
 main PROC

    ;EC: doing something awesome like setting the text color

    ; set text color to teal
        mov eax, val2
        imul eax, 16
        add eax, val1
        call setTextColor

    ; INTRODUCTION
        mov     edx, OFFSET programTitle
        call    WriteString
        mov     edx, OFFSET myName
        call    WriteString
        call    CrLf

        ; EC Prompt
        mov     edx, OFFSET ec_prompt
        call    WriteString
        call    CrLf


        mov     edx, OFFSET prompt_1
        call    WriteString
        call    CrLf


        ; get user's name
        mov     edx, OFFSET buffer  ;point to the buffer
        mov     ecx, SIZEOF buffer  ; specify max characters
        call    ReadString
        mov     byteCount, eax

        ; greet the user
        mov     edx, OFFSET hi
        call    WriteString
        mov     edx, OFFSET buffer
        call    WriteString
        call    CrLf

    ; USER INSTRUCTIONS
topPrompt:
            mov     edx, OFFSET prompt_2
            call    WriteString
            call    CrLf

    ; GET USER DATA
        call    ReadInt
        mov     numFib, eax

    ; Validate user data
        cmp     eax, UPPERLIMIT
        jg      TooHigh
        cmp     eax, LOWERLIMIT
        jl      TooLow
        je      JustOne
        cmp     eax, 2
        je      JustTwo

    ; DISPLAY FIBS

        ; prepare loop (post-test), do the first two manually

        mov     ecx, numFib
        sub     ecx, 3          ; we start at iteration 3, the first two are taken care of by JustOne and JustTwo
        mov     eax, 1
        call    WriteDec
        mov     edx, OFFSET spaces
        call    WriteString
        call    WriteDec
        mov     edx, OFFSET spaces
        call    WriteString
        mov     prev2, eax
        mov     eax, 2
        call    WriteDec
        mov     edx, OFFSET spaces
        call    WriteString
        mov     prev1, eax
        **cmp       ecx, 0          ; Compares to ecx to prevent an error causing it to loop infinitely when 3 is input!
        je      TheEnd**

        fib:
            ; add prev 2 to eax
            add     eax, prev2
            call    WriteDec

            mov     edx, OFFSET spaces
            call    WriteString

            mov     temp, eax
            mov     eax, prev1
            mov     prev2, eax
            mov     eax, temp
            mov     prev1, eax

            ;for spacing (first time it should be % 3, rest %5)
            mov     edx, ecx
            cdq
            div     moduloFive
            cmp     edx, 0
            jne     skip
            call    CrLf

        skip:
                ; restore what was on eax
                mov     eax, temp
                ; if ecx % 3 = 0 call CrLf
                loop    fib
        jmp     TheEnd

TooHigh:
            mov     edx, OFFSET tooHighError
            call    WriteString
            jmp     TopPrompt

TooLow:
            mov     edx, OFFSET tooLowError
            call    WriteString
            jmp     TopPrompt
JustOne:
            mov     edx, OFFSET firstOne
            call    WriteString
            jmp     TheEnd

JustTwo:
            mov     edx, OFFSET firstTwo
            call    WriteString
            jmp     TheEnd

    ; FAREWELL
TheEnd:
            call    CrLf
            mov     edx, OFFSET goodbye
            call    WriteString
            mov     edx, OFFSET buffer
            call    WriteString
            call    CrLf

    exit    ; exit to operating system
main ENDP

END main

我目前的问题是它与每个数字完美配合! (如果错误高或低,则按预期进行处理)除了3?

3使它尝试进行超级计算并基本崩溃。有人能帮忙吗?我的意思是它不是太激烈,我有1/46的机会在分配任务时弹出错误(我将以任何一种方式转向)但我真的很想知道到底是怎么回事用代码。

它调用整数溢出,我认为它发生在moduloFive上,是否导致了一个巨大的错误?

0 个答案:

没有答案