Author Topic: .while .endwhile  (Read 9302 times)

mark allyn

  • Guest
.while .endwhile
« on: August 13, 2009, 03:53:13 PM »
Hi everyone -

I am trying to code a windows application and ti would be very helpful to have  a macro or "quasi directive" that could do the equivalent of masm32's .while .endw capability to do the message loop.  I was hoping someone would be willing to share code that could perform this feat.

Thanks for your consideration,
Mark Allyn

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: .while .endwhile
« Reply #1 on: August 13, 2009, 08:20:01 PM »
Hi Mark,

I'm sure there are many macro sets for Nasm that include while/endwhile. One I know about is Nasm64developers's "control_flow.zip", available from the "files" page here. I was going to cut-and-paste his while/endwhile, but I thought "No, the forum software will screw up the formatting. Give him a specific link to download it, instead." But the "files" page seems to be down for repairs right now, so back to cut-and-paste. :)


;---------------------------------------
; WHILE...ENDWHILE
;---------------------------------------

%imacro WHILE 1.nolist
 ; allow nesting: omit %if(n)ctx while
 %push while
 %define %$cc %+1
 jmp %$eval
 %$beg:
%endmacro

%imacro ENDWHILE 0.nolist
 %ifnctx while
  %error ENDWHILE: must use WHILE first
 %else
  %$eval: j%$cc %$beg
  %$end:
  %pop
 %endif
%endmacro

;---------------------------------------
; CONTINUE and BREAK
;---------------------------------------

%imacro CONTINUE 0.nolist
 %ifctx repeat while
  jmp %$eval
 %else
  %error CONTINUE: must use REPEAT or WHILE first
 %endif
%endmacro

%imacro BREAK 0.nolist
 %ifctx repeat while switch
  jmp %$end
 %else
  %error BREAK: must use REPEAT or WHILE or SWITCH first
 %endif
%endmacro

As I read this, it isn't really equivalent to Masm's ".while". The parameter has to be a condition code - "WHILE z", etc. You can't do ".while eax != ebx" or so, as I think you can in Masm. Looks like "nagoa+.inc" would accept "while eax, ne, ebx", which is a little closer... http://www.visual-assembler.pt.vu/

Compared to Masm's "built in macros", the best part of Nasm's system is that anyone can write their own macros. The worst part is that everybody does! :)

Best,
Frank