Search code examples
loopsx86iterationmasmfibonacci

How to make assembly program call new line after 5 outputs of fibonacci


So my assignment was to have the user enter a number of fibonacci terms to print, and the program would print those out in order. However it also calls for there to be a new line after five terms are printed. The other requirement is that the masm "loop" instruction has to be used in the loop that calculates the fibonacci numbers. How would I implement this without an infinite loop? This is what it I have right now. It calculates the fibonacci fine but not when I try to add a new line:

.data
fib_count    DWORD    ?
num1         DWORD    0
num2         DWORD    1
sum          DWORD    0
counter      DWORD    0
spaces       BYTE     "     ",0
.code

main PROC

;Display fibonacci
mov     ecx, fib_count ;fib_count is the number of terms the user entered
;printing first 1
mov     eax, 1
call    WriteDec
mov     edx, OFFSET spaces
call    WriteString
dec     ecx

print_loop:
mov     eax, num1
add     eax, num2
mov     sum, eax
mov     eax, num2
mov     num1,eax
mov     eax, sum
mov     num2,eax
mov     eax, sum
call    WriteDec
inc     counter
mov     eax, counter
cmp     eax, 5
je      new_line
mov     edx, OFFSET spaces
call    WriteString
loop    print_loop

new_line:
call    crlf
loop    print_loop

Solution

  • You need a jump instruction just before new_line to go around the new_line code:

    ;       ...
            call    WriteString
            loop    print_loop
            jmp     short done
    
    new_line:
            call    crlf
            loop    print_loop
    done:
    ;       ...