flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [fasm g] how do I concatenate strings

Author
Thread Post new topic Reply to topic
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 20 Apr 2016, 22:33
How do I concatenate multiple strings into one?
Code:
macro cpu? type
  include 'arch/'#`type#'.inc'
end macro

cpu 8086    

doesn't seem to be working in fasm g
Post 20 Apr 2016, 22:33
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3551
Location: vpcmipstrm
bitRAKE 21 Apr 2016, 03:38
I don't know if this is the intended way?
Code:
macro cpu? type*
  eval 'include "arch/',`type,'.inc"'
end macro

cpu 8086    
Yet, it seems to work. Very Happy
Post 21 Apr 2016, 03:38
View user's profile Send private message Visit poster's website Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 21 Apr 2016, 07:05
I'm so dumb Sad I tried eval, but used it improperly. Thanks a lot!
Post 21 Apr 2016, 07:05
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8106
Location: Kraków, Poland
Tomasz Grysztar 21 Apr 2016, 08:22
bitRAKE: good idea! It is a nice solution thanks to its simplicity. It is much nicer than the one I would propose:
Code:
macro cpu? type
  local path
  virtual at 0
    db 'arch/',`type,'.inc'
    load path : $ from 0
  end virtual
  include path
end macro 

cpu 8086    
The direct calculation of concatenation is also possible, though the absence of special operators for string manipulation makes it very tedious:
Code:
struc concat? first*,rest*&
        . = string first
        iterate s, rest
                local str
                str = s
                . = string . + str shl (((bsr . - 1) shr 3 + 1) shl 3)
        end iterate
end struc

macro cpu? type
  local path
  path concat 'arch/',`type,'.inc'
  include path
end macro 

cpu 8086    
When working on this example I discovered another hidden bug in the fasm g engine. Thank you! Wink


Last edited by Tomasz Grysztar on 21 Apr 2016, 08:53; edited 1 time in total
Post 21 Apr 2016, 08:22
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8106
Location: Kraków, Poland
Tomasz Grysztar 21 Apr 2016, 08:47
I have uploaded the bugfix. Now that macro works correctly also in this form:
Code:
struc concat? first*,rest*&
        . = string first
        iterate s, rest
                . = string . + (s) shl (((bsr . - 1) shr 3 + 1) shl 3)
        end iterate
end struc    
Before I fixed the bug, it did not work because the iteration parameter messed up the recognition context for the subsequent "." symbol.
Post 21 Apr 2016, 08:47
View user's profile Send private message Visit poster's website Reply with quote
fabbel



Joined: 30 Oct 2012
Posts: 11
fabbel 31 Jan 2023, 07:56
Hi

Quote:

The direct calculation of concatenation is also possible,
though the absence of special operators for string manipulation makes it very tedious:


Any particular reason why not adding some string operators indeed ?

Tx
Rgds
Post 31 Jan 2023, 07:56
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8106
Location: Kraków, Poland
Tomasz Grysztar 31 Jan 2023, 12:14
fabbel wrote:
Any particular reason why not adding some string operators indeed ?
It has never been a nuisance enough to get to the front of the queue of things to implement. I did have an operator for string concatenation planned - under a BAPPEND name, to complement the existing BSWAP operator. But I also knew that adding concatenation would likely invite questions about other string operations.

The reason why this never was especially pressing issue is that it tends to become hidden once you have some kind of framework established. Let me show a couple samples:
Code:
include 'inline.inc'

inlinemacro strcat? a*, b*
        return = string (a) or (b) shl (8*lengthof (a))
end inlinemacro

inlinemacro concat? head*, tail&
        local buffer
        buffer = string head
        iterate item, tail
                buffer = strcat(buffer,item)
        end iterate
        return = buffer
end inlinemacro

inlinemacro current_line
        rept 1, line:__line__
                return = concat(__file__,' [',`line,']')
        end rept
end inlinemacro

display 'Current line: ', current_line()    
Code:
include 'xcalm.inc'

calminstruction calminstruction?.err? list*&
        local buffer, item
        compute buffer, ''
    loop:
        match item=,list, list
        jno final
        compute buffer, string buffer + item shl (8*lengthof buffer)
        jump loop
    final:
        compute buffer, string buffer + list shl (8*lengthof buffer)
        asm err buffer
end calminstruction

calminstruction test
        err     'This is a',10,9,9,'multi-line message'
end calminstruction
test    
There are many interesting things happening and the lack of string operators is just a minor detail. The design of fasmg (and now CALM) has always been about providing building blocks for an emergent language, what I used to call "complex solutions with simple features" when I first articulated this concept in case of fasm 1.

To be clear: I'm not saying that specialized operators are completely unnecessary because you can emulate them with existing features. It's just that their implementation becomes less crucial because of that and I like to focus on things that open new doors. But the right hour for implementing them may still come.
Post 31 Jan 2023, 12:14
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2023, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.