Author Topic: Processing Strings in Macros  (Read 16125 times)

nobody

  • Guest
Processing Strings in Macros
« on: April 10, 2008, 02:10:28 PM »
Hi everyone! Here's the problem:

%macro getlength   1
   %strlen len %1
%endmacro

if you now call this with "getlength hello" you'd end up with "%strlen requires string as second parameter". Naturally one could use quotes, but is there any other way? I think it's quite possible in TASM or MASM.

What is more, you could look up NASM documentation section 4.10.2, about reading environment variables, where in fact the same problem is covered, saying 'a workaround can be created using a multi-line macro'. I only I could see that macro...

nobody

  • Guest
Re: Processing Strings in Macros
« Reply #1 on: April 10, 2008, 03:09:30 PM »
Hi,
Here's one way. This depends on trapping for the string delimiter.
I used zero but it can be whatever your text uses. It uses more
code than you wanted (I think) but it works.
When the smoke clears, the count is returned in ECX.


%macro getlength 2       ; First param is adr of string, in this
                         ;  example it's macrotest
                         ; Second param is the delimiter to look for

push ebx
    mov ecx,0            ; Reset the counter
    mov ebx,dword %1     ; adr of string

here:
    cmp byte [ebx],%2    ; Check for the delimiter
    jz there             ; Found the delimiter
    inc ebx              ; Not yet - move to next char
    inc ecx              ; Inc count
    jmp here             ; Next char
there:

pop ebx

%endmacro

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

.data

macrotest db "macrotest",0    ;

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

.code

getlength macrotest, 0

This will return ECX = 09h



;mcamember

nobody

  • Guest
Re: Processing Strings in Macros
« Reply #2 on: April 10, 2008, 05:02:17 PM »
Yeah, thanks, that's a great piece. I would use it if I needed to get the length at run-time. [And it Definitely uses more code than I wanted :)) ] That's not exactly what I actually want to do, though.

I'm writing a DOS app just for fun. I'm upgrading from TASM to NASM and I want to make the procedure call interface like in TASM. For example, I want to be able to write stuff like

proc hex2ascii HexNumber:dword,pOutputBuffer:word
       mov di,pOutputBuffer
       ..... and so on ....
endp hex2ascii

So we need 'proc' and 'endp' macros. To distinguish between word and dword params, I need to analyze the arguments passed to 'proc' at compile-time. There are macros %substr and %strlen, but they seem to require quotes. I thought it would be silly to write

proc hex2ascii "hexNumber:dword","pOutputBuffer:word"
      ....
endp hex2ascii

,wouldn't it? So what should I do?

By the way, how would you implement the following TASM macro:
irpc      character,
    db        ’&character&’,0Fh
endm
It's taken from a book and is intended to prepare strings to copy directly into video-memory at B8000h.

Thanks in advance.

nobody

  • Guest
Re: Processing Strings in Macros
« Reply #3 on: April 10, 2008, 07:02:56 PM »
Hi,

>>So we need 'proc' and 'endp' macros.

From this comment I'm guessing that you can't code PROC/ENDPROC
in NASM without an assembler error. (I noticed that you spell
the direcive ENDP not ENDPROC. My implementation of NASM requires
ENDPROC.)

So one thing at a time ;-)

PROC/ENDPROC macros can be found in the file NASMX.INC
This file is part of the NASM installation at :

http://www.asmcommunity.net/projects/nasmx/

It's probably here at sourceforge also but I happened to get my
installation from http://www.asmcommunity.

If this is NOT your problem then you might be having a syntax
problem but I want to be sure that you are able to code a
PROC/ENDPROC directive without error before going off-track
on something else.

As for the TASM code I don't have a clue on TASM syntax. ( Actually,
my knowledge of macros in NASM is pretty much limited to what I wrote in the last post.)

>>>copy directly into video-memory at B8000h.
Wow ! Does Bill & Co. still allow us do that (heh-heh)

;mcamember