Author Topic: condition codes in NASM macros  (Read 16373 times)

mark allyn

  • Guest
condition codes in NASM macros
« on: August 23, 2009, 06:45:14 PM »
Hello everyone-

I borrowed the REPEAT/UNTIL macro written by nasm64developer.  My code reads like this:

mov ecx, 0
   REPEAT
   inc ecx
   UNTIL ecx,>,10

When NASM hits the UNTIL keyword it throws up its hands and says that it expects an instruction.  Using the -e switch, and NAMS exe, it looks to me like the preprocessor is simply ignoring the ecx,1>,0 condition.  Obviously, I am coding the code wrong, but I have experiemented with various codings and looked at the asm file for the macro and can't figger out what the right code would be.

Somebody must have hit this brick wall way long before I did.

Thanks,
Mark Allyn

mark allyn

  • Guest
Re: condition codes in NASM macros
« Reply #1 on: August 23, 2009, 07:17:28 PM »
Hi everyone --

....oops!  Please disregard the wacky "greater than" sign in the last line of the snippet.  Should have been "<".  Either way, the UNTIL macro doesn't run.

Thanks,
Mark

mark allyn

  • Guest
Re: condition codes in NASM macros
« Reply #2 on: August 23, 2009, 07:21:12 PM »
Hi everyone -

Looked at the code again....oh boy...I was right the first time.  As I have been inclined to say of late:  Must be Hurricane Bill.

Mark

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: condition codes in NASM macros
« Reply #3 on: August 23, 2009, 08:25:49 PM »
Hi Mark,

Hey, thanks for making me feel like a youngster! I'm not (quite) 65 yet!

As I read Nasm64developer's macro, UNTIL takes only one parameter - the condition-code to be pasted after "j". I'm looking at "j%+1" - I'm thinking that should be "j%1" or perhaps "j%+%1"... seems to work, as is. Anyway, there's no "cmp" being done that I can see. I think your code is going to have to look like:

%include "/home/fbk/control_flow.asm"

global _start

section .text
_start:
    xor ecx, ecx
    REPEAT
    inc ecx
    cmp ecx, 1000000000 ; delay long enough to detect :)
    UNTIL a

mov eax, 1
    int 80h

Well... you won't need the Linux cruft, of course... just wanted to post something "known working"...

I think that to handle "UNTIL ecx, >, 10", you're going to need macro code to do something like "%ifidni %2, '>'", generate the code, "%elifidni %2, '<'", generate the code, etc... "%endif". "%1" and "%3" will have to be valid operands to "cmp", or the "user" of the macro will get a "cryptic error". To handle "UNTIL ecx, &, 80h", you'll have to generate "test" instead of "cmp"... ad nauseum...

Something to think about... "ja" would be correct for an unsigned comparison, but "jg" if your operands are to be taken as signed. How will you distinguish between an unsigned '>' and a signed '>'? I understand Masm has a way to do this, but I don't know how. "s>" vs "u>"? If you're just going to accept 'a' and such condition codes, it's up to the programmer to chose the right one (as it should be, in asm!).

Funny how those hurricanes make us all confused. The effect could last for *months*, too! :)

Best,
Frank