Guys i was wondering how would i go about doing the above.
Example is i want 1024 bytes or 1 kilobyte of string data into a buffer.
I have no idea whatsoever on how to do this.
Thanks.
[EDIT]
mov esi, offset source ; put address into the source index
mov edi, offset dest_buffer ; put address into the destination index
loop_top:
mov al, [esi] ; copy byte at address in esi to al
;inc esi ; increment address in esi
mov [edi], al ; copy byte in al to address in edi
inc edi ; increment address in edi
cmp al, 0 ; see if its an ascii zero
jne loop_top ; jump back and read next byte if not
[/EDIT]
Ah found this howto in the help files.
Sorry
Hi Gavin-
There are some problems with the code you posted-
You don't increment esi, so it will just move the same byte over and over
You don't check to see if you did it 1024 times, so it will loop forever until you exceed your alloted memory and get a memory fault
To move 1024 bytes, put 1024 in a register (eg. ecx) and decrement each loop until it is zero.
Hi Jimg.
I changed the code and i get the error still.
Lol sorry i put the mov ecx, 1024 in the wrong part lol oops.
Works fine now ;) thanks
mov esi, offset source ; put address into the source index
mov edi, offset dest_buffer ; put address into the destination index
loop_top:
mov al, [esi] ; copy byte at address in esi to al
mov [edi], al ; copy byte in al to address in edi
inc edi ; increment address in edi
mov ecx,1024
dec ecx
cmp ecx, 0 ; see if its an ascii zero
jne loop_top ; jump back and read next byte if not
So i ran the debuger and as you said i get an access violation writting to the destination buffer.
My buffers are like this.
dest_buffer db ?
source db 'a'
Hello Gavin,
Why not just use something like invoke lstrcpy ADDR Dest,ADDR Source? or you could do:mov edi,offset source
mov ecx,-1
repne scasb
not ecx ;length of source string
mov esi,offset source
mov edi,offset destination
@@:
lodsb ;movs byte from source and increments esi and decrements ecx
or al,0
jz @f ;quit when source = 0
or cl,0
jz @f ;quit when counter = 0
stosb ;movs byte to destination and increments edi
jmp @b
@@:
I haven't had a chance to check this code for errors but it should work.
best regards,
czDrillard
dest_buffer db ?
source db 'a'
--------------------------------------------------
You need to allocate space for the strings
in the .data and .data? areas or allocate heap memory.
.data
source db 'a',0 ; zero terminated 1 byte source string
.data?
dest_buffer db 1025 dup (?) ; uninitialized buffer, can hold 1024 byte string along with a 0 terminating byte
Unless you test for for a shorter (less than 1024 bytes) source string by checking for a zero byte,
it will pull whatever follows the source, possibly setting off a protection violation.
Using invoke lstrcpy as czDrillard posted is the easiest way to copy a string.
Hi gavin, :lol
"i want 1024 bytes or 1 kilobyte of string data"
What mean "string" for you?
.data
source db 1024 dup (31h)
db 0
dest_buffer db 1024 dup (39h)
.code
push esi ; preserve registers edi and esi
push edi
mov esi, offset source ; put address into the source register esi
mov edi, offset dest_buffer ; put address into the destination register edi
mov ecx,1024
loop_top:
mov eax, [esi+ecx-4] ; copy dword at address in esi+ecx-4 to eax
sub ecx,4
mov [edi+ecx], eax ; copy dword in eax to address in edi+ecx
jne loop_top ; jump back and read next dword if ecx is not zero
pop edi ; restore registers edi and esi
pop esi
or:
.code
mov eax,1024
loop_top:
mov ecx, dword ptr [source+eax-4] ; copy dword at address in source+eax-4 to ecx
sub eax,4
mov dword ptr [dest_buffer+eax], ecx ; copy dword in ecx to address in dest_buffer+eax
jne loop_top ; jump back and read next dword if eax is not zero
Regards
Lingo
.data
szStringFrom db "0123456789",0
szStringTo db 128 dup(0)
.code
CopyData proc Dest:DWORD,Source:DWORD,Bytes:DWORD
push edi
push esi
cld
mov edi,Dest
mov esi,Source
mov ecx,Bytes
shr ecx,2
rep movsd
mov ecx,Bytes
and ecx,3
rep movsb
pop esi
pop edi
ret
CopyData endp
;.....
invoke CopyData,addr szStringTo,addr szStringFrom,7
Regards
Hi,
Saw this while passing! Been some time since my last posting! The small proc below may be helpful!
mov esi,OFFSET source
mov edi,OFFSET dest_buffer
xor eax,eax
mov ecx,1024
@@:
mov al,[esi]
test eax,eax
jz @F
mov [edi],al
add edi,1 ; Add is faster than INC on newer PCs
add esi,1
dec ecx
jnz @B
@@:
..
If you didn't know @@: is an unnamed label in MASM. @F refers to the unnamed label in front and @B is the one before the code where it is used.
Thomas
:U :U
Hi lads.
Thanks for all your help.
It's good to get the different solutions from you all.
Thanks again for yout time.