News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

String Generation

Started by Citric, May 06, 2005, 02:15:05 AM

Previous topic - Next topic

Citric

Just something i have been thinking about recently,

i know you cant do this,


mov [stringVar], "This is my String"


but you would have to move the correct ascii codes into memory, to generate the string you want.

how would you easily generate Strings on the fly.

just a thought.

mnemonic

I don't know what you mean by saying "generate"... but it looks like you want to copy a string.
There is some proc called "szCopy" in the masm32 library. There are also some more procs and macros that handle strings. Have a look at it.
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

pbrennick

Citric,
There is a macro that does this for you.  Take a look at macro.asm (make sure you have the latest one.  The sas macro does exactly what you are discribing, I think, take a look at it.

EDIT:  Hi mnemonic, looks like we cross-posted, sorry, but I think the sas macro is a better fit as it works at the assembly level.  Anyway, sorry.

hth:
Paul

Mark Jones

An easy way to generate strings on the fly is by including macros.asm and using its szText macro.


sztext HelloWorld, "Hello world!!!"
sztext Title, "Window Title"
invoke MessageBox, hWnd,addr HelloWorld, addr title, MB_OK

(untested code, from memory)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

pbrennick

Wow, busy topic...  :clap:

Nice example Mark.

Paul

Mark Jones

Indeed Paul, I think we all chimed in at exactly the same time. :bg
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

mnemonic

@Paul:
No issue.
In here it´s like: the more replies the more info. It all assembles to a sharper picture. :wink

@Citric:
I recommend having a look at the "MASMLIB.HLP" file in the "help" folder (if it is up-to-date i don´t know...).

Regards, Jens
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

pbrennick

Ctric,
In that help file if you look under Psuedo Mnemonics, you will see:

Quote
sas This macro assigns a quoted string to a DWORD variable.
sas var,"This is a string"

As you can see it does a nice job of creating the code 'on the fly' necessary to assemble as you requested.  It doesn't get much press but I like it...

hth:
Paul

Citric

Quote from: pbrennick on May 06, 2005, 03:29:30 AM
Ctric,
In that help file if you look under Psuedo Mnemonics, you will see:

Quote
sas This macro assigns a quoted string to a DWORD variable.
sas var,"This is a string"

As you can see it does a nice job of creating the code 'on the fly' necessary to assemble as you requested. It doesn't get much press but I like it...

hth:
Paul


Hi Paul

So let me get this straight it puts the address of the String in the variable assigned.

ie from above var containts the address of "This is a string".

cool that is exactly what i was looking for.

Hi All

Thanks for the replies.

AeroASM

This is hardly string generation on-the-fly. This is:


.data
buffer db 512 dup(0)
.code
start:
mov edi,offset buffer
mov al,"H"
stosb
mov al,"e"
stosb
mov al,"l"
stosb
mov al,"l"
stosb
mov al,"o"
stosb
mov al,0
stosb
end start

Citric

this is more i ment by
Quote from: Citric on May 06, 2005, 02:15:05 AM
but you would have to move the correct ascii codes into memory, to generate the string you want.

Quote from: AeroASM on May 06, 2005, 06:58:45 AM
This is hardly string generation on-the-fly. This is:


.data
buffer db 512 dup(0)
.code
start:
mov edi,offset buffer
mov al,"H"
stosb
mov al,"e"
stosb
mov al,"l"
stosb
mov al,"l"
stosb
mov al,"o"
stosb
mov al,0
stosb
end start


But that gets messy and very hard to read!

AeroASM

It is also slower and larger, but is the only way to make strings on-the-fly. Therefore, don't!

Citric

Quote from: AeroASM on May 06, 2005, 07:07:34 AM
It is also slower and larger, but is the only way to make strings on-the-fly. Therefore, don't!

Thats because its run time generation and not compile time generation like the other suggestions.

Rifleman

Hi Citric,
As you saw, the sas macro does create 'on the fly' strings.  Aero gets funny at times but he is a helpful guy ordinarily.  His code actually is a very close expansion to how the macro works.  I don't think he even looked at it!!  :bdg

Paul

hutch--

Citric,

Its an OK way of embedding string data directly into code but you will be more efficient doing it on the stack 4 bytes at a time.


"masm is great"

becomes

push 00000074h    ; 74h = "t"
push "grea"
push " is "
push "masm"


use the string a ESP then rebalance the stack.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php