Author Topic: 'Joining' strings  (Read 11136 times)

ahluka

  • Jackass IV
  • Posts: 794
  • Karma: +10/-201
'Joining' strings
« on: August 19, 2005, 05:32:42 AM »
Well now I know which syscall is equivalent to C's system() function, I've hit another little snag. How in the name of Mary Moe do you concatenate strings in assembly? Would I be right in assuming you copy the base string into memory then calculate the end of it, appending the other string?

I'm going to read about the string operations in asm, but from what I remember they're just for storing / loading strings.

ygfperson

  • Founders
  • Posts: 601
  • Karma: +10/-1
    • Last.fm
'Joining' strings
« Reply #1 on: August 19, 2005, 11:09:12 AM »
I suppose you could call strcat(), or use a simple for loop to copy the other contents onto the end of your string:
Code: [Select]

char* source;
char* dest;
while (*source++);
while (*source++ = *dest++);

or its equivalent in assembly. There are quite a few asm operations meant to string manipulation

ahluka

  • Jackass IV
  • Posts: 794
  • Karma: +10/-201
'Joining' strings
« Reply #2 on: August 19, 2005, 11:50:17 AM »
Thank you, I get the idea now. I don't remember much about string ops, better look them up.

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
'Joining' strings
« Reply #3 on: August 23, 2005, 04:34:47 PM »
Remember that "string" in assembly is nothing more than a collection of characters. There are no real string operators as such, all you would do is move bytes to contigious memory locations to append one string to another.

Checkout MOVS, and STOS (Move string and Store string). MOVS moves the string element addressed by the (E)SI register to the location addressed by the (E)DI register one element at a time. Use REP / REPZ / REPNZ to repeat for all elements of a string.

edit>> Well, I guess there are string operators, but you get my drift.

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
'Joining' strings
« Reply #4 on: August 23, 2005, 05:42:32 PM »
Code: [Select]

org  100h

jmp start

string1 db 'Hello '
string2 db 'World'
string3 db 'XXXXXXXXXXX'

start:

cld

lea si, string1
lea di, string3

mov cx, 6

rep movsb

lea si, string2

mov cx, 5

rep movsb


end


would append World to Hello and put the result in string3 for example.

ahluka

  • Jackass IV
  • Posts: 794
  • Karma: +10/-201
'Joining' strings
« Reply #5 on: August 24, 2005, 08:54:30 AM »
Yay my saviour :D