|
Forum
|
|
|
String manipulation - from C++ to Assembly, I've reached a point in my education where I need to translate how to manipulate strings in ASM.
|
|
| admiral27 |
|

Member
 
Group: Members
Posts: 33
Member No.: 7931
Joined: 17-May 08

|
I've reached a point in my education when I need to translate how to manipulate strings in ASM.
For example, lets say I have this in C++
| CODE | hMemory = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT), MEMSIZE ); pMemory = GlobalLock( hMemory ); SendMessage( hEdit, WM_GETTEXT, MEMSIZE-1, pMemory );
|
This should fill an area of memory located by pMemory from the control hEdit. What I want to do is to pull out chunks of text, 80 characters at a time. How would you manipulate text in ASM?
Any type of example will be greatly appreciated, even if it uses a different method than this. Thank you.
|
| |
|
|
|
| admiral27 |
|

Member
 
Group: Members
Posts: 33
Member No.: 7931
Joined: 17-May 08

|
I'm sorry... I've never been very good at posting to forums. I seem to either put too much or too little.
I'm trying to understand how to deal with string manipulation in ASM. I know how to do the global alloc to get a handle to string, but I want to do a bit more.
Lets just assume I've successfully pulled the text from a EDIT control (through a "save to file" function) and I want to do other things with the string.
I have a pointer to the text that was in the "EDIT" control. I would like to take that pointer and copy it in 80 character chunks and send it to the printer. What I'm not sure of is how to do it, especially with GlobalAlloc; I'm used to using Alloc or Malloc. My thinking is that I should use pointer incrementing or push the values on the stack... Honestly, I'm THAT new to all of this, so thats why I supplied so little and I'm hoping for different ideas, since I'm sure there's lots of ways to accomplish the same thing.
Since I first posted, I found a list of instructions I have never seen before
| CODE | rep movsb;copies a string repe cmpsb;compares a string repne scasb;scans a string for a character rep stosb;sets a character in a string
cld;sets the direction flag to forward mov esi, source;move the source address in to esi mov edi, dest;move the destination address in to edi mov ecx, length;move the length to copy in to ecx rep movsb;copy length bytes from esi to edi |
I'm wondering if this is the first step.
|
| |
|
|
|
| shoorick |
|

Extremely Active Member
     
Group: Admins
Posts: 2303
Member No.: 160
Joined: 22-June 04

|
here is my ready code (FASM) for saving and loading UNICODE text from/to MULTILINE edit control - no comments at all, if there any question - ask
==================== (failed to upload nor zip, neigther inc) ====================
| CODE | ;======================================================================= proc load_file _name ;----------------------------------------------------------------------- local cnt dd ? local mem dd ? ;----------------------------------------------------------------------- push edi push esi invoke CreateFile,[_name],GENERIC_READ,FILE_SHARE_READ,0,\ OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0 cmp eax,INVALID_HANDLE_VALUE mov esi,eax je .done invoke GetFileSize,esi,0 or eax,eax mov edi,eax je .done_close add eax,2 invoke LocalAlloc,LMEM_MOVEABLE,eax mov [mem],eax invoke LocalLock,eax push eax lea ecx,[cnt] mov word [eax + edi],0 invoke ReadFile,esi,eax,edi,ecx,0 invoke LocalUnlock cmp edi,[cnt] je .done_close invoke LocalFree,[mem] mov [mem],INVALID_HANDLE_VALUE .done_close: invoke CloseHandle,esi mov eax,[mem] .done: pop esi pop edi ret ;----------------------------------------------------------------------- endp ;======================================================================= ; ;======================================================================= proc save_file _name,_buff,_size,_bom ;----------------------------------------------------------------------- push edi push esi xor eax,eax mov esi,[_buff] cmp word [esi],BOM jne @F mov [_bom],0 @@: mov edi,[_size] add edi,edi jnz @F mov edi,esi mov ecx,-1 repne scasw sub edi,[_buff] sub edi,2 @@: invoke CreateFile,[_name],GENERIC_WRITE,0,0,CREATE_ALWAYS,\ FILE_ATTRIBUTE_NORMAL,0 cmp eax,INVALID_HANDLE_VALUE je .done mov esi,eax cmp [_bom],0 je @F lea ecx,[_name] invoke WriteFile,esi,bom,2,ecx,0 cmp [_name],2 je @F mov edi,-1 jmp .close @@: lea ecx,[_name] invoke WriteFile,esi,[_buff],edi,ecx,0 invoke SetEndOfFile,esi sub edi,[_name] .close: invoke CloseHandle,esi mov eax,edi .done: pop esi pop edi ret ;----------------------------------------------------------------------- endp ;======================================================================= ; ;======================================================================= proc append_file _name,_buff,_size ;----------------------------------------------------------------------- push edi push esi mov esi,[_buff] mov edi,[_size] cmp word [esi],BOM jne @F add esi,2 sub edi,2 @@: add edi,edi jnz @F mov edi,esi mov ecx,-1 repne scasw sub edi,[_buff] sub edi,2 @@: invoke CreateFile,[_name],GENERIC_WRITE,FILE_SHARE_READ,0,\ OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0 cmp eax,INVALID_HANDLE_VALUE je .done mov esi,eax invoke SetFilePointer,eax,0,0,FILE_END lea ecx,[_name] invoke WriteFile,esi,[_buff],edi,ecx,0 sub edi,[_name] .close: invoke CloseHandle,esi mov eax,edi .done: pop esi pop edi ret ;----------------------------------------------------------------------- endp ;======================================================================= ; ;======================================================================= proc SaveEdit _edit,_file ;----------------------------------------------------------------------- invoke SendMessage,[_edit],WM_GETTEXTLENGTH,0,0 push eax invoke SendMessage,[_edit],EM_GETHANDLE,0,0 invoke LocalLock,eax or eax,eax pop edx jz @F push eax stdcall save_file,[_file],eax,edx,1 pop edx push eax invoke LocalUnlock,edx pop eax ret ;----------------------------------------------------------------------- endp ;======================================================================= ; ;======================================================================= proc AppendToFile _edit,_file ;----------------------------------------------------------------------- invoke SendMessage,[_edit],WM_GETTEXTLENGTH,0,0 push eax invoke SendMessage,[_edit],EM_GETHANDLE,0,0 invoke LocalLock,eax or eax,eax pop edx jz @F push eax stdcall append_file,[_file],eax,edx,1 pop edx push eax invoke LocalUnlock,edx pop eax ret ;----------------------------------------------------------------------- endp ;======================================================================= ; ;======================================================================= proc LoadEdit _edit,_file ;----------------------------------------------------------------------- stdcall load_file,[_file] cmp eax,INVALID_HANDLE_VALUE je @F push 0 push eax invoke SendMessage,[_edit],EM_GETHANDLE,0,0 mov [_file],eax invoke SendMessage,[_edit],EM_SETHANDLE invoke LocalFree,[_file] xor eax,eax @@: ret ;----------------------------------------------------------------------- endp ;=======================================================================
|
|
| |
|
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Track this topic
Receive email notification when a reply has been made to this topic and you are not active on the board.
Subscribe to this forum
Receive email notification when a new topic is posted in this forum and you are not active on the board.
Download / Print this Topic
Download this topic in different formats or view a printer friendly version.
|
|
|