Author Topic: Example needed for conditional programming, please  (Read 129 times)

Offline RuudB

  • Jr. Member
  • *
  • Posts: 35
  • Country: nl
    • Ruud's Commodore Site
Example needed for conditional programming, please
« on: August 27, 2019, 01:21:58 PM »
Hello

I did a RTFM and found the use of %if: %if <condition>. So far I haven't found out what syntax of <condition> is. I want to use it inside a macro, something like:

Code: [Select]
CharSym equ 1
WordSym equ 2

%macro WriteLn 2
  %if &1=CharSym
   ....
  %elif &1=WordSym
   ....
  %endif
%endmacro

I tried the above but then I don't get an error message regarding the macro but one saying there is something wrong in the line that calls the macro. Both are in a different file. The moment I delete the whole %if construction and just keep one of the .... parts things assemble and run fine.

I hope someone can help, tahnk you very much in advance!

Kind regards, Ruud Baltissen

« Last Edit: August 27, 2019, 08:50:39 PM by Frank Kotler »
With kind regards / met vriendelijke groet, Ruud Baltissen

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2376
  • Country: us
Re: Example needed for conditional programming, please
« Reply #1 on: August 27, 2019, 10:20:27 PM »
Hi Ruud.

Square brackets around "code tags", not angle brackets.

Beyond that, I'm not getting anywhere. Can you show us more and tell us what message you're getting?

Best,
Frank


Offline RuudB

  • Jr. Member
  • *
  • Posts: 35
  • Country: nl
    • Ruud's Commodore Site
Re: Example needed for conditional programming, please
« Reply #2 on: August 28, 2019, 09:37:57 AM »
Hallo Frank,

The error message I got was: "TMP.asm:18: error: parser: instruction expected". But I found out later that that must have been due to a stupid typo I overlooked all the time. This morning I was experimenting a bit again and didn't see it any more until I made another typo.

The experiments brought me further but not with the wanted result. I'm writing a Pascal compiler in Pascal and it generates a source code that does not contain instructions but only macros. An include file should contain the macros with the instructions for the wanted processor. My self written assembler should turn the whole in an executable. So far things go fine with creating programs for example for the Commodore 64.
But I also wanted to be able to create programs that should run on a 8088 machine running my own OS. So I started to add a unit to my assembler that should handle 8088 instructions. But to be able to test the generated code, I wrote a little program that translates various directives into NASM ones. That worked out fine except for my own ".if" directive.

The code of the main program after the translation:
Code: [Select]
%include 't8088.inc'
CPU 8086
org $0100
;
macJump zFirstLine
 
; === Constants ===
CONST0000:
 db $10,'Hello!'

; === Subroutines needed by macros ===
mDisplayAL
mDisplayString

zFirstLine:

;    3 begin
macWrite StringSym,CONST0000,0,0
macWriteLn
;    4   writeln('Hello!');
;    5 end.
macEndOfProgram

; === Variables ===


The part of t8088.inc that is relevant for the above:
Code: [Select]
CharSym equ 2
StringSym equ 9
WordSize equ 2
True equ 0FFh
False equ 0

%macro macWrite 4 ; type, address of variable/char, length number(s)
%if True         ; works
;%if StringSym=&1 ; doesn't work
 mov si,&2
 call cDisplayString
%endif
 
%if WordSize=CharSym ; works
;%if (&1=CharSym) ; doesn't work
;%if &1=CharSym ; doesn't work
;%if False ; works
; mov al,[&2]
 mov si,&2
 mov al,[si]
 call cDisplayAL
%endif
%endmacro

So far I have found out that the %if statements using True and False work fine and the one whith "WordSize=CharSym". But all the ones using &1 in one or another way are not accepted by NASM. Even the one where I expected that "StringSym=&1" would be translated into "StringSym=StringSym" and thus should have worked. But alas, no.

Another question: "mov si,&2" does work, "mov al,[&2]" generates an error. Why?

I hope you can help!

Kind regards, Ruud Baltissen

With kind regards / met vriendelijke groet, Ruud Baltissen

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2376
  • Country: us
Re: Example needed for conditional programming, please
« Reply #3 on: August 29, 2019, 12:30:27 AM »
Hi Ruud.

One error we have been making... we have been calling macro parameters " $1" etc. Should be "%1" etc. See if that helps you any...

Best,
Frank



Offline RuudB

  • Jr. Member
  • *
  • Posts: 35
  • Country: nl
    • Ruud's Commodore Site
Re: Example needed for conditional programming, please
« Reply #4 on: August 29, 2019, 12:47:09 PM »
Hello Frank,

Things work now fine!

And I do feel very stupid. If I had a better look at the LST file I would have seen that the generated values for where I used the "&" based parameters were also wrong. My only excuse is that I was focused on the error ones.

Thank you very much!
With kind regards / met vriendelijke groet, Ruud Baltissen