News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

FOR-NEXT loops

Started by Vortex, May 19, 2021, 07:28:41 PM

Previous topic - Next topic

Vortex

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.


hutch--

Thanks Erol, looks like interesting stuff.

stevenxie

HI,VOTEX,you are very good.