Search code examples
assemblyx86masmirvine32

Add new line every 5 loops MASM


I'm trying to properly format output in an assembly program I'm writing but I'm finding it very difficult to use the ECX register to keep track of which loop I'm on and take an action based on that value.

I want to add a new line every 5 times the loop runs, and I've tried using a modulus with the ECX register to do this, but haven't had any luck. I'm using the Irvine32 library.

I'm trying to do something like this:

mov    ecx, someNumber

the_loop:
    cmp    0, ecx mod 5
    je     fifth_loop
    jmp    continue

    fifth_loop:
        call    CrLf

    continue:
        loop    the_loop

This obviously doesn't run properly. Although I think the logic is sound, I don't know syntactically how to use the modulus alongside the ECX register. I know I could use DIV along with the EAX and EDX registers, but I'm already using the EAX, and EBX registers to calculate the Fibonacci sequence (that's the goal of my program.)


Solution

  • There are several options. If you're running short on registers, you can store their value on stack or in memory and restore the original values after not needing the registers anymore.

    Another option is to count till 5, do the CrLf and reset the counter after that:

        mov    ecx, someNumber
        mov    edx, 1
    the_loop:
        cmp    edx, 5
        je     fifth_loop
        inc    edx
        jmp    continue
    fifth_loop:
        call    CrLf
        mov     edx, 1
    continue:
        loop    the_loop
    

    Here you only need edx in addition to the rest of registers.