If I want to show for example several messageboxes with different textstrings on several places in my code I can put all the text strings in .data section or in a stringtable.

My question is if there are any risks or other disadvantages to put the textstrings here and there in .code section like this
	call @F

@@:
jmp @F
db "My textstring",0
@@:
pop edx
add edx,2
invoke messagebox,0,edx,0,MB_OK


I don't have to name the string and I have the text next to the place where the string is used.Just curious.

Best regards
Posted on 2005-01-23 05:20:57 by minor28
The only real limitation is that the text is read only unless you change the attributes of the sections with the linker. It is generally easier to place them BEFORE the start label so you don't have to jump over them.

The technique is handy if you want an EXE to be a bit harder to modify and it does save you a .DATA section if you have very few strings that only need to be read but a .DATA section is far more flexible for larger apps.

It is also handy to place a short string within a procedure and jump over it just to keep the code self contained and not add a data section if you don't want one.
Posted on 2005-01-23 05:25:41 by hutch--
minor28, it's bad for performance to mix code and data - especially if you're going to do writes to the data. If you have code and modified data in the same "region" (I think somewhere between 1-4k on my CPU, depends on cache size), you can see *extreme* slowdowns - there was an alt.lang.asm thread about some encryption that touched the issue.

As for not having to name the string, use a macro like CTEXT - it allows unnamed string variables to be generated and used directly in, for example, invoke statements. IMHO it's best to put such strings in a .const section to keep the data together and away from code, and if you don't like having an addtional section in the .exe, you can use the /MERGE linker switch.
Posted on 2005-01-23 06:20:27 by f0dder


push 0
push 0
call @F
db "Hello World"
@@:
push 0
call MessageBox


More elegant.
Posted on 2005-01-23 07:04:05 by roticv


CTEXT MACRO y:VARARG
LOCAL sym

CONST segment
IFIDNI <y>,<>
sym db 0
ELSE
sym db y,0
ENDIF
CONST ends

EXITM <OFFSET sym>
ENDM

invoke MessageBox, NULL, CTEXT("hello, world!"), CTEXT("yadda yadda"), MB_OK

much more elegant.

(I think the macro is from bitRAKE).
Posted on 2005-01-23 10:28:49 by f0dder
Thank you,

My conclusion is: There is no obvious risk to put textstings in the code section, at least one disadvantage could be slowdowns of performance and there are more elegant solutions.

In my case (it is exceptionhandling) I will use the CTEXT.

I found this macro
CTEXT macro text:vararg 

LOCAL TxtName
.data
TxtName byte text,0
.code
exitm <addr TxtName>
endm

It worked.

But the suggested macro
CTEXT MACRO y:VARARG 

LOCAL sym

CONST segment
IFIDNI <y>,<>
sym db 0
ELSE
sym db y,0
ENDIF
CONST ends

EXITM <OFFSET sym>
ENDM

didn't work. Exception in the exceptionhandler.
Posted on 2005-01-23 12:18:28 by minor28
The CTEXT I provided is for constant data - that is, read-only strings. If you try to modify the string, you will get an exception indeed.
Posted on 2005-01-23 12:22:09 by f0dder
I have a similar problem, need to change a immediate, dont care about speed, how do I tell the linker to make it possible to modify my immediate in code section?
Posted on 2005-01-23 12:49:55 by daydreamer
/section :text,rew
Posted on 2005-01-23 13:01:43 by f0dder
/section :text,rew
/section :text,rew

doesnt work, reports a linker error

I have this code


lea ebx,[imb]
db 0c6h
db 0fh
db 0e5h
imb db 0ffh ;SHUFPS XMM4,XMM5,255
....
....
....
....
dec BYTE PTR [ebx]

as there are only immediate byte aviable to control SHUFPS, I want to manipulate that imb
Posted on 2005-01-23 14:18:06 by daydreamer
Sorry, /section:.text,rew
Posted on 2005-01-23 15:05:38 by f0dder
Sorry, /section:.text,rew

thanks f0dder, now it works
Posted on 2005-01-23 16:04:18 by daydreamer