Author Topic: FOR-NEXT loops  (Read 212 times)

Vortex

  • Member
  • *****
  • Posts: 2559
FOR-NEXT loops
« 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.

Code: [Select]
FOR_ MACRO Iterator,StartValue,EndValue,Increment
The last parameter Increment is optional and the default value is equal to 1.

Code: [Select]
ContFor
This macro jumps to the NEXT statement associated with the current FOR loop.

Code: [Select]
BreakFor
This macro terminates immediately the ongoing FOR-NEXT loop.

An example :

Code: [Select]
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--

  • Administrator
  • Member
  • ******
  • Posts: 8354
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: FOR-NEXT loops
« Reply #1 on: May 20, 2021, 01:08:55 AM »
Thanks Erol, looks like interesting stuff.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

stevenxie

  • Regular Member
  • *
  • Posts: 47
Re: FOR-NEXT loops
« Reply #2 on: May 21, 2021, 01:01:22 AM »
HI,VOTEX,you are very good.