Author Topic: Example of .IF macro in loop design.  (Read 52 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5659
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Example of .IF macro in loop design.
« on: August 10, 2018, 05:43:03 PM »
While both the .repeat - .until macro and the .while - .endw macro work correctly, they both must use a register where the .IF macro will accept 2 memory operands which is often simpler in higher level code. For the price of nominating a normal label, the .IF macro is simpler to use and more flexible in how you can use it. Examples below.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include64\masm64rt.inc

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

 entry_point proc

    USING r12
    LOCAL var1  :QWORD
    LOCAL var2  :QWORD

    SaveRegs

  ; -------------------------------------

    mov var1, 1
    mov var2, 5

  lbl1:
    conout str$(var1),lf
    add var1, 1
    .IF var1 le var2 goto lbl1          ; eval last

    conout lf

  ; -------------------------------------

    mov var1, 1
    mov var2, 5

  lbl2:
    .IF var1 gt var2 goto lbl3          ; eval first
    conout str$(var1),lf
    add var1, 1
    jmp lbl2
  lbl3:

    conout lf

  ; -------------------------------------

    mov var1, 1
    mov var2, 5

    jmp lbl5                            ; eval first by jmp to .IF
  lbl4:
    conout str$(var1),lf
    add var1, 1
  lbl5:
    .IF var1 le var2 goto lbl4          ; eval at end

  ; -------------------------------------

    waitkey
    RestoreRegs
    .exit

 entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin: