Hello All!
I have some variables in my program, for example:



.data
cSomeText db "Some text here",0

.data?
TextBuffer db 100 dup (?) --> I can't change this type of declararion because I'll need it later on


I wanna move the value of cSomeText to TextBuffer. I tried the following:


mov edx,offset cSomeText
mov TextBuffer,edx

mov edx,offset cSomeText
mov TextBuffer,[edx]

mov edx,offset cSomeText
mov [TextBuffer],edx


None of them worked. How can I do it?
Thanks in advance.
Cthulhu
Posted on 2003-05-31 03:33:21 by Cthulhu
this is interesting... :grin:

just assume that me is a dumb looking at your 3 possibilities, that afaik lack one. which is :

mov edx,offset cSomeText
mov ,

btw, i dont know it work or not, but how can you said it dont work? saying that mean you're using somewhat mechanism to prove that, maybe with ollydbg? just use it to see wether the destination selector is same like the source. or look something else.
Posted on 2003-05-31 03:40:17 by dion
stringlength equ $-cSomeText

.data
cSomeText db "Some text here",0

.data?
TextBuffer db 100 dup (?)

.code
mov ecx,stringlength
lea esi, cSomeText
lea edi, TextBuffer
rep movsb

Play with string functions when using strings :)
Anyway your question is very common. Assuming that everything works like dword/word/byte data value.
Posted on 2003-05-31 04:22:00 by roticv
Hello roticv!
Thanks for your reply!
It works properly now. I thought it could be a commom question for assembly language beginners.

I'm using mainly IcZelion's tuts and The Art of Assembly Language to learn it. Are there any more papers you'd recommend?

Thanks again man!
Cthulhu
Posted on 2003-05-31 14:10:15 by Cthulhu

this is interesting... :grin:

just assume that me is a dumb looking at your 3 possibilities, that afaik lack one. which is :

mov edx,offset cSomeText
mov ,

btw, i dont know it work or not, but how can you said it dont work? saying that mean you're using somewhat mechanism to prove that, maybe with ollydbg? just use it to see wether the destination selector is same like the source. or look something else.


u forgot about this crazy one and a lot of more combinations :alright:

mov ,offset cSomeText
mov , edx
Posted on 2003-05-31 17:28:40 by AceEmbler
Try this tutorial :)
Written by alloces
Posted on 2003-05-31 20:20:05 by roticv
I usually end up using something like this. Essentially you just want to copy a string so it would go like this :
CopyString proc uses esi edi Source:DWORD,Dest:DWORD

mov esi,Source
mov edi,Dest

@@:

mov al, [esi]
inc esi
mov [edi], al
inc edi
cmp al,0
jne @B

mov eax,edi
ret
CopyString endp


EDIT: I guess I should note why I prefer it this way, the mov/inc instructions are pairable so they essentially are executed at the same time, cmp register to immed. takes 1 clock so in reality ignoring the jmp this should take only 3 clocks per character, much better than lodsb/stosb or movsb which can take up to 10 per char. At least that's the way I read agner when I wrote it.
Posted on 2003-05-31 21:13:21 by donkey
Of course donkey, no one actually stated that you must use esi and edi for strings :).

Try:


CopyString:
mov edx,[esp+4]
mov ecx,[esp+8]

@@:

mov al, [edx]
inc edx
mov [ecx], al
inc ecx
or al,al
jne @B
ret 8
Posted on 2003-05-31 21:22:17 by roticv
I know roticv,

It's just that when I actually take the time to worry about optimizing code, which is extremely rare as I see it as a useless waste of my time, it's because I need to perform the same function alot. When I wrote that I was moving thousands of strings about and using ECX as a counter so I used the source and destination registers as I didn't need them at the time. I added the proc tags to decorate this example only.
Posted on 2003-05-31 21:37:13 by donkey
Thanks roticv for the tutorial and thanks donkey for the optimized routine.
I'm sure all this will be very useful for me.

Best regards
Cthulhu
Posted on 2003-05-31 23:13:52 by Cthulhu
If you're really lazy and you're developing under W32 enviroments....

just use the lstrcpyn API!
Posted on 2003-06-01 09:35:11 by Drocon
hmm...now that i think abt it, shouldn't wsprintf also work?

everything from the input string is directly copied to the output buffer, with the exclusion of format control characters, which are replaced by the arguments which are pushed onto the stack...so if the input buffer contains no format control characters, and no arguments are pushed onto the stack, wont it simply copy the input string to the output buffer??

i havent tried this myself, and i dont know if wsprintf fails when nothing is pushed onto the stack...but it seems to make sense atm
Posted on 2003-06-01 19:35:42 by AnotherWay83
There are many ways to do this with the API. However the API has a huge penalty and I don't think it's suitable when ASM provides us with a much more elegant solution. My routine also has the advantage that the return value is the offset of the zero terminator of the destination string, that address can be re-used to cat strings as well.

ie
copystring pstring1,pbuffer
dec eax
copystring pstring2,eax

I didn't use it like this but I built it in just in case.

EDIT: whoops put eax in the wrong param - corrected it
Posted on 2003-06-01 20:12:35 by donkey
hello guys

i used to work with string using windows api, now i am trying to work with string using asm.
here is the function, is this ok:



.code
start:
invoke CopyString, offset texto, offset buffer, textlen
invoke MessageBox, NULL, eax, eax, NULL
invoke ExitProcess, NULL

CopyString proc uses esi edi ecx src:DWORD, dest:DWORD, len:DWORD
mov esi, src
mov edi, dest
mov ecx, len

cld
rep movsw ;movsd

sub edi, len
mov eax, edi
ret
CopyString endp
Posted on 2003-06-01 21:36:59 by Jnrz
Hi jnrz,

A string is made up of bytes so when you use movsw you are moving words. If your string length is say 32 characters your algo will actually mov 64. Try using movsb instead. The algo will work ok for unicode strings however. movs_ is a very inefficient instruction and requires quite a few reps to make it something you want to use. The better alternative for short strings is mov ptr,data/ inc ptr.

The original request was for null terminated strings so I assumed that the length was unknown. If you know the length you can probably save alot of time doing it this way. It works on the assumption that the data will be DWORD aligned and if it isn't it will not make a difference because of the termination character.

mov ecx,strlength
add ecx,3
and ecx,-4
shr ecx,2
mov edi,pbuffer
mov esi,pstring

rep movsd
Posted on 2003-06-01 23:19:35 by donkey