Hello! I just started trying to get the grasp of Assembly a few days ago, so I decided to write a program to print the Fibonacci numbers up to the 25th one. I'm using NASM, and it's for the x86 processor. I came up with this code, but it'll loop infinitely, not just 25 times (which I set ECX to). It's probably a simple mistake, but thank you to anyone who looks through it for me!

    fmt: DB '%d',10,0
    GLOBAL  _main
    EXTERN  _printf
    MOV     EBX,1
    MOV     EAX,0
    MOV     ECX,25     ;set loop counter to 25, right?
    PUSH    EBX
    ADD     EBX,EAX
    POP     EAX
    PUSH    EAX
    PUSH    fmt
    CALL    _printf
    ADD     ESP,8
    LOOP    fib       ;maybe put in the wrong spot?
    MOV     EAX,0

EDIT: I just realized that _printf is probably messing with EAX, but how can I save it before and restore it after? A PUSH and POP before and after the _printf block still kept the infinite loop and messed with the signs on the number? I think...

I've had problems getting the LOOP instruction to work with NASM before. You might want to try replacing your loop instruction with this code:

JG fib

Nope, no luck. It still loops infinitely...

I finally got it. The fib code is a bit off anyways, but to fix the loop, I just needed to PUSH ECX right before my _printf block and POP it back after; It was getting messed with by the C function. Phew :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.