News:

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

String manipulation

Started by j00n, February 16, 2011, 10:51:00 PM

Previous topic - Next topic

j00n

can someone please show me how to manipulate a string with registers?

if i have the string "Hello my name is j00n"

how would I edit only the "00" portion of the string to replace it with "EE" for example?

I don't have any code because I don't know how to do this... i'm just looking for an example to follow and study

i'd like to see how this is done without macros please


also how would I concatenate two string values without the use of macros

firstname + " " + lastname = firstname lastname

MichaelW

One very simple method:

    mov edx, OFFSET mystr
    mov WORD PTR [edx+18], "EE"

eschew obfuscation

KeepingRealBusy

To concatenate several strings (your example) or any two strings, you need a destination string at least as long as the combined length of the strings plus one more character for the terminating null. This is one way to do the concatenation:



.data

source1 BYTE "firstname"
source1Size equ ($-source1)

source2 BYTE " "
source2Size equ ($-source2)

source3 BYTE "lastname",0
source3Size equ ($-source3)

destination BYTE (source1Size + source2Size + source3Size ) DUP (0)

.code

mov  edi,OFFSET destination

mov esi,OFFSET source1
mov ecx,source1Size
rep movsb

mov esi,OFFSET source2
mov ecx,source2Size
rep movsb

mov esi,OFFSET source3
mov ecx, source3Size
rep movsb



Dave.

dedndave

there are also macros for working with strings
refer to  \masm32\help\hlhelp.chm
i prefer Dave's method, though   :bg

one more tip   :P
        mov dword ptr StringData,'n00j'
when you put a string in the source operand, reverse the characters

KeepingRealBusy

Quote from: dedndave on February 17, 2011, 01:25:14 AM
one more tip   :P
        mov dword ptr StringData,'n00j'
when you put a string in the source operand, reverse the characters

Dave,

I disagree with that. MASM generates a normal string sequence just as if you did this in the .data section with a BYTE definition. This is true whether you use single quotes (mov eax,'j00n'), or you use double quotes (mov eax,"j00n"), or even if you use a hex definition (mov eax,06A30306Eh). Only when the data is loaded into the register do the bytes get swapped, and they get swapped back to normal order when the register is stored.

Dave.

jj2007

Quote from: dedndave on February 17, 2011, 01:25:14 AM
there are also macros for working with strings

Dave,
Macros are awful, you don't learn anything, except with Olly :bg

include \masm32\MasmBasic\MasmBasic.inc
  Init
  Let esi=FileRead$("\Masm32\include\Windows.inc")
  FileWrite "MyFile.txt", Replace$(esi, "PROTO", "Proto", 4)  ; mixed case looks nicer (4=whole word only)
  Inkey "OK"
  Exit
end start

oex

Dave: Dave
Dave: Dave's Method
Dave: Dave, Dave
jj: Dave,

Argh! and I thought ASM was confusing :lol
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

KeepingRealBusy

Quote from: oex on February 17, 2011, 03:11:42 PM
Dave: Dave
Dave: Dave's Method
Dave: Dave, Dave
jj: Dave,

Argh! and I thought ASM was confusing :lol

oex,

That is why you use quotes, to separate dedndave from KeepingRealBusy. If it is any easier for you, just call me KRB.

Dave.

Magnum

Quote from: MichaelW on February 16, 2011, 11:03:25 PM
One very simple method:

    mov edx, OFFSET mystr
    mov WORD PTR [edx+18], "EE"



Your method looks it might make an interesting way to write a string at runtime.

I can't figure out the logic involved.

Would I start at edx and add single letters like this -> edx, edx + 6, edx + 12 ?

Have a great day,
                         Andy

KeepingRealBusy

Magnum,

If you remember, the original question was:

Quote from: j00n on February 16, 2011, 10:51:00 PM

if i have the string "Hello my name is j00n"

how would I edit only the "00" portion of the string to replace it with "EE" for example?


I have used the same method as Michael suggested many times to edit template error messages as needed.

Dave.

Magnum

My question is only related to the original post.

Maybe I wasn't clear about what I am looking for.

Have a great day,
                         Andy

hutch--

Whats the big deal, you write one string after the other while ensuring the buffer is big enough.


  str1 db "first string",0
  str2 db "second string",0
  str3 db "                          buffer that is big enough                            "


You then load the ADDRESS of str3 into a register then write str1 to it noting the location of the first zero, then write the second string to the end of the first one in str3.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Magnum

Quote from: hutch-- on February 18, 2011, 05:02:12 AM
Whats the big deal, you write one string after the other while ensuring the buffer is big enough.


  str1 db "first string",0
  str2 db "second string",0
  str3 db "                          buffer that is big enough                            "


You then load the ADDRESS of str3 into a register then write str1 to it noting the location of the first zero, then write the second string to the end of the first one in str3.

I would like to store/write one letter at a time so that if someone debugs or views it in a hex editor,
they can never see more than one "letter at a time."

I know this is 16 bit code, but it accomplishes what I would like using direct video writes.

mov          [si],word ptr 0441h ; 04h is char attribute,
                                              ; 41h is an 'A'
             mov          si,404              ;    4 = red       0  = black
             mov          [si],word ptr 096eh ;'n' 1 = blue      7  = white
                                              ;    3 = cyan      5  = magenta
                                              ;    6 = brown     8  = gray
                                              ;    9 = br. blue  10 = lt. green
                                              ;   14 = yellow    11 = lt. cyan
                                              ;   13 = lt. magenta12 = lt. red
             mov          si,406              ;    2 = green     15 = br. white
             mov          [si],word ptr 0264h ; d  12 = br. red
             mov          si,408                         
             
             mov          [si],word ptr 0472h ;  r
             mov          si,410
             mov          [si],word ptr 0965h ;  e
             mov          si,412          ;
             mov          [si],word ptr 0277h ;  w
             mov          si,414     
Have a great day,
                         Andy

evlncrn8


mov edx, OFFSET mystr
mov WORD PTR [edx+18], "EE"


could be optimised a bit further..


mov word ptr [offset mystr + 18], 'EE'


:)