flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Concatenate strings for printing, [args] common db args,0 ?

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
kake_zinger



Joined: 15 Jul 2004
Posts: 59
kake_zinger 10 Apr 2025, 13:24
My newbie brain hurts and wonders if this can be done, and what is the correct syntax:
Code:
macro PrintString [args]
{ 
    local ..startm , ..endm
    jmp ..endm
    ..startm:
    common db args,0
    ..endm:
    mov rax, ..startm
    mov rcx, ..endm - ..startm
}
    

Compile fails with:
Quote:

C:\fasm>fasm invoke-messagebox.fasm
flat assembler version 1.73.32 (1048576 kilobytes memory)
invoke-messagebox.fasm [451]:
PrintString "Big whoppers","of things!"
invoke-messagebox.fasm [146] PrintString [5]:
..endm:
processed: ..endm?T0,..endm?T2:
error: illegal instruction.

Supposed to use like this:
Code:
  PrintString "Big whoppers","of things!"  ; strings to be joined together
  invoke MessageBox, 0, rax, "Joined", MB_OK
    

or with WriteFile using length in rcx .
Can't find a single example fitting this. It's as if ..endm label somehow catches on to common? Is there a way to limit the scope of common? This is just too weird for my linear (smooth) brain.
Post 10 Apr 2025, 13:24
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1149
Location: Russia
macomics 10 Apr 2025, 13:54
Code:
invoke MessageBox, HWND_DESKTOP, <"First string", "Second string", "Third string">, "Test", MB_OK or MB_ICONINFORMATION    


Code:
EOLN fix 0xd,0xa

struc StringA txt& { label .
  db txt
  label .length at ( $ - . )
  db 0
  label .bytes at ( $ - . ) }
; ...
        invoke WriteFile, [hFile], addr [a], a.length, addr [nWriten], NULL
; ...
a StringA "First string", EOLN, "Second string", EOLN, "Third string"
    
Post 10 Apr 2025, 13:54
View user's profile Send private message Reply with quote
a



Joined: 10 Apr 2025
Posts: 17
Location: Ukraine
a 10 Apr 2025, 16:23
kake_zinger wrote:
My newbie brain hurts and wonders...

"Joined: 15 Jul 2004"
Post 10 Apr 2025, 16:23
View user's profile Send private message Reply with quote
kake_zinger



Joined: 15 Jul 2004
Posts: 59
kake_zinger 12 Apr 2025, 12:04
a wrote:
"Joined: 15 Jul 2004"

I only ever used Fasm as a compiler for my pc boot environment project (which was fun), never had a need to touch macros nor Windows programming, so a definite newbie at those now. Besides in nearly 20 years it seems it's possible to forget most of even 32bit asm Smile
macomics wrote:
<examples>

Thanks for those, maybe they were the secret sauce to get my brain geared towards what I'd really need:
A way to conveniently use throwaway strings within code without endless label inventing, or even keeping track of a serial#.

Something like:
Code:
jmp @@
@1: db 'Here lies a very deep, introspective string,'\
       "which may even continue on multiple lines.',13,10,0
@2: db $ - @1
@@:
  mov rcx, [@2]
  mov rdx, @1
[...call/invoke something...]
    

I notice Thomas made some macros which might fit the purpose at
https://board.flatassembler.net/topic.php?p=190642#190642
but for Fasmg only. Any chance those could be translated to plain Fasm?
Even @bN only would be great for simple string definitions, although some complicated loop constructs might like @fN as well.
Since there have been proposals for this, and the Fasmg macro, many AI's seem to think this is a reality already, so might as well make it a reality.

Writing code in a debugger one has unlimited anon labels in the form of memory addresses. A whole range of hex wouldn't be bad, @1-@f, but what about going base 36 and all the way from @0 to @z Wink
Post 12 Apr 2025, 12:04
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1149
Location: Russia
macomics 12 Apr 2025, 13:06
Code:
  jmp  @f
@1: db 'Here lies a very deep, introspective string,',\
       "which may even continue on multiple lines.",13,10,0
@2: dq $ - @1
@@:
  mov  rcx, [@2]
  lea  rdx, [@1]
[...call/invoke something...]    

https://board.flatassembler.net/topic.php?t=21545&postdays=0&postorder=asc&start=0
Post 12 Apr 2025, 13:06
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8394
Location: Kraków, Poland
Tomasz Grysztar 12 Apr 2025, 13:23
kake_zinger wrote:
I notice Thomas made some macros which might fit the purpose at
https://board.flatassembler.net/topic.php?p=190642#190642
but for Fasmg only. Any chance those could be translated to plain Fasm?
You can translate the macros made for fasmg quite directly:
Code:
macro @INIT name,prefix
{
        macro name tail&
        \{
                match label, prefix#f
                \\{
                        label tail
                        prefix#b equ prefix#f
                        prefix#r equ prefix#f
                \\}
                \local ..anonymous
                prefix#f equ ..anonymous
        \}

        define prefix#f
        name
}

rept 10 i:0
{
        @INIT @#i,@#i
}    
Post 12 Apr 2025, 13:23
View user's profile Send private message Visit poster's website Reply with quote
a



Joined: 10 Apr 2025
Posts: 17
Location: Ukraine
a 12 Apr 2025, 13:55
kake_zinger wrote:
'Here lies a very deep, introspective string,'\
"which may even continue on multiple lines.',13,10,0

if you want for multi-line macro to automatically insert a new-line then:
Code:
macro PrintString [args]
{
    common
      local startm, endm, count
      count = 0
      jmp endm
      startm:
    forward
      count=count+1
    forward
      db args
      if ~ count = 1
        count=count-1
        db 0xd,0xa
      end if
    common
      db 0
      endm:
      mov rax, startm            ; string address
      mov rcx, endm - startm - 1 ; string size (minus 1 null)
}

PrintString "Here lies a very deep, introspective string",\
            "which may even continue on multiple lines."    
Post 12 Apr 2025, 13:55
View user's profile Send private message Reply with quote
a



Joined: 10 Apr 2025
Posts: 17
Location: Ukraine
a 12 Apr 2025, 14:12
my macro seems to be too large, because it counts the amount of the parameters
if only fasm would had a convenient way to detect the last parameter, u know by something like this:
Code:
if last
 db 0
end if
if not last
 db 0xA
end if    

or better yet:
Code:
if current_param = -1
 db 0
end if    

or it would be nice having an % variable to return something
Post 12 Apr 2025, 14:12
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20625
Location: In your JS exploiting you and your system
revolution 12 Apr 2025, 14:26
If the string is a counted length then you don't need the 0 terminator.

Or alternatively, if the string is zero terminated then you don't need the length.
Post 12 Apr 2025, 14:26
View user's profile Send private message Visit poster's website Reply with quote
a



Joined: 10 Apr 2025
Posts: 17
Location: Ukraine
a 12 Apr 2025, 14:41
revolution wrote:
If the string is a counted length then you don't need the 0 terminator.

Or alternatively, if the string is zero terminated then you don't need the length.

I think kake_zinger wants a macro to cover a both cases at the same time, so that you dont have to bother with it, a swiss knife kind of macro
kake_zinger wrote:
A way to conveniently use throwaway strings within code without endless label inventing, or even keeping track of a serial#.
Post 12 Apr 2025, 14:41
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1149
Location: Russia
macomics 12 Apr 2025, 15:47
revolution wrote:
If the string is a counted length then you don't need the 0 terminator.

Or alternatively, if the string is zero terminated then you don't need the length.

Controversial statements, at least because then the strings will not be interchangeable and you will have to spend time calculating the length.
Post 12 Apr 2025, 15:47
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8394
Location: Kraków, Poland
Tomasz Grysztar 12 Apr 2025, 18:40
a wrote:
if only fasm would had a convenient way to detect the last parameter, u know by something like this: ...
With fasm2 this becomes quite natural:
Code:
macro PrintString args&
    local startm, endm
    jmp endm
    startm:
    iterate item, args
        db item
        if % < %%
          db 0xd,0xa
        end if
    end iterate
    db 0
    endm:
    mov rax, startm
    mov rcx, endm - startm - 1
end macro    
Post 12 Apr 2025, 18:40
View user's profile Send private message Visit poster's website Reply with quote
kake_zinger



Joined: 15 Jul 2004
Posts: 59
kake_zinger 13 Apr 2025, 08:27
macomics wrote:
revolution wrote:
If the string is a counted length then you don't need the 0 terminator.

Or alternatively, if the string is zero terminated then you don't need the length.

Controversial statements, at least because then the strings will not be interchangeable and you will have to spend time calculating the length.


I like to define labeled strings like this because it covers both cases at once, depends on whether one uses WriteConsole/File or printf more which one is a better default:
Code:
; Better for WriteConsole/File, easy usage
@@: msg_debug     dq @f - @b - 8  ; current data size (dq) needs to be subbed
    db 'Attempting to write a console message...', 13, 10, 0
    ; String always at msg_debug + datatype_size; pick one type and stick with it
;
; Note the next anon label is still part of the definition
;
; Better for printf but need to specify size with length operations
@@: dq @f - @b - 8
    ; Address always at string_label - datatype_size
    msg_debug_3 db 'Attempting to write a console message yet again...', 13, 10, 0
@@: ; If using label after length version, size must be specified when using length:
    ; mov r8, qword [msg_debug_3 - 8]
    

I don't mind sacrificing 8 bytes (or whatever max string size type is used) for always having a precalculated value for the length in case it's needed.
Post 13 Apr 2025, 08:27
View user's profile Send private message Reply with quote
kake_zinger



Joined: 15 Jul 2004
Posts: 59
kake_zinger 13 Apr 2025, 09:31
Tomasz Grysztar wrote:
You can translate the macros made for fasmg quite directly

Please don't be oblivious to your own genius-level skill and experience, that's just about an impossible task for someone who's new to Fasm macros, probably even for intermediate level (as testified by the lack of solutions this far, or did my searches fail? Please do prove me wrong..).
Post 13 Apr 2025, 09:31
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1149
Location: Russia
macomics 13 Apr 2025, 09:35
kake_zinger wrote:
Please do prove me wrong..).

The lack of solutions for fasm1 rather suggests that macros are easy to rewrite on fasm1. So they don't share the obvious, but simply rewrite it and use it.
Post 13 Apr 2025, 09:35
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8394
Location: Kraków, Poland
Tomasz Grysztar 13 Apr 2025, 11:38
kake_zinger wrote:
(as testified by the lack of solutions this far, or did my searches fail? Please do prove me wrong..).
There is one from 2006, even.
Post 13 Apr 2025, 11:38
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 14 Apr 2025, 10:00
very interesting.
Quote:

macro @@@ colon { }

I don't know about colon. This is news to me.
Code:
;fasm do this ?
if a eq colon or eqtype
 do some
end if
    

what other reserved words are there for macros?
Post 14 Apr 2025, 10:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20625
Location: In your JS exploiting you and your system
revolution 14 Apr 2025, 10:30
There is nothing special about colon. You can also write the macro like this:
Code:
macro @@@ arg { }    
It is just a named parameter.
Post 14 Apr 2025, 10:30
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 14 Apr 2025, 12:25
ok.
How do many @ in macro ?
Something like this macro @,5 { created macro @@@@@ arg \{\} }
;in code
@@@@@:
Post 14 Apr 2025, 12:25
View user's profile Send private message Reply with quote
kake_zinger



Joined: 15 Jul 2004
Posts: 59
kake_zinger 14 Apr 2025, 18:35
Tomasz Grysztar wrote:
There is one from 2006, even.

Oh that's a bait'n'switch, it does not implement numbered @0...@9 macros with jumping at them with @f1...9 / @b1...9 like you quoted earlier from Fasm2 macros. Seems I was right in my assessment of the conversion being non-trivial and none posted public this far (but maybe 19 years is too short of a time for settling such trivial matters).

But it was a good hook for getting someone to explore Fasm2, which I'm now doing, and enjoying
Code:
include '@@.inc'    

immensely. Too bad about them ordinary folks with their plain Fasm, no multi-anon-jmp-macros for you... (just wait another 19 years and they might arrive!)
Post 14 Apr 2025, 18:35
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.