The MASM Forum

Microsoft 64 bit MASM => MASM64 SDK => Topic started by: Vortex on May 19, 2021, 07:28:41 PM

Title: FOR-NEXT loops
Post by: Vortex on May 19, 2021, 07:28:41 PM
Hello,

Here are some simple macros to emulate the traditional FOR\NEXT construct of the Basic language.

FOR_ MACRO Iterator,StartValue,EndValue,Increment

The last parameter Increment is optional and the default value is equal to 1.

ContFor

This macro jumps to the NEXT statement associated with the current FOR loop.

BreakFor

This macro terminates immediately the ongoing FOR-NEXT loop.

An example :

include \masm32\include64\masm64rt.inc
include ForLoop.inc

.code

start PROC

    call    main
    invoke  ExitProcess,0   

start ENDP


main PROC USES rbx

LOCAL i:QWORD
LOCAL j:QWORD

    FOR_ i,3,16,6

        .if i == 15
            BreakFor           
        .endif 



        FOR_ j,20,23

            .if j == 22
                  ContFor
            .endif



            FOR_ rbx,7,3,-2

                .if rbx == 5
                    ContFor
                .endif

                invoke  vc_printf,\
                        chr$("i=%d j=%d rbx=%d",13,10,),\
                        i,j,rbx   
             
            NEXT ebx
           
        NEXT j
       
    NEXT i


    FOR_ i,1,5

        invoke  vc_printf,\
                chr$("This is a test.",13,10)

    NEXT i

    ret

main ENDP

END


The same macros in the file ForLoop.inc can be used with 32-bit code.

Title: Re: FOR-NEXT loops
Post by: hutch-- on May 20, 2021, 01:08:55 AM
Thanks Erol, looks like interesting stuff.
Title: Re: FOR-NEXT loops
Post by: stevenxie on May 21, 2021, 01:01:22 AM
HI,VOTEX,you are very good.