Question

I'm currently working on an assignment in AT&T Assembly and now I have to append two strings:

message: .asciz "String 1"
before: .asciz "String 2"

I have really no idea how to do this or how to begin. I've already searched on internet but I couldn't find any helpful information. I think I have to manually copy the characters of the second string to the end of the first string but I'm not sure about that.

Could anyone please explain to me how to do this? :)

Was it helpful?

Solution

This is not a trivial matter. Strings are variable in length and occupy different spaces in memory, and there has to be some way to know how long they are or where they end. With C or C++, a nul bytes (bytes of zero value) indicates the end of the string. With some other program languages, you have a pointer to the start of the string and the length of the string stored separately, which has the advantage of letting you store binary (including bytes of zero value) in the string. Even with C and the rest, you have to have a pointer to where the string starts.

What generally has to happen is that you have to use asm to contact the operating system and request a block of memory that is currently free that is big enough to contain the contents of the two strings once they are attached. This would be memory separate from either of the two strings to start with, and it comes from what is referred to as the Memory Heap, Once you are given the beginning point of that memory block, you copy the contents of the first string into it, then you continue on while copying the contents of the second string in there right behind the first. Then you release the memory that had been assigned to the first string and reassign the block to that string by changing its pointer, and possibly its length. The released memory is returned to the Memory Heap by the Operating System for reuse elsewhere.

Actually, the operating system is not the only source of freed up memory. Some compilers, even assemblers, either handle memory management on their own, or provide suitable tools to the programmer to do it as the need arises.

In other words, this can be a very ambitious undertaking, and you have to know quite a bit about what is going on to do it right. You do it wrong, you can expect consequences like crashing your system and needing to reboot.

OTHER TIPS

This question fails to mention the target memory, which makes it somewhat difficult to answer. I also don't know if you're in 16 bit, 32 bit or 64 bit. For convenience's sake, I'll also just assume they're C style 0-terminated strings.

Anyway, this seems to be the general procedure:

  • Get the length of the first string (instructions on writing an asm strlen can be found here: http://www.int80h.org/strlen/)
  • Set the ptr to the target memory
  • Copy the first string to the destination memory, using rep(e/ne) movsb with the size in ecx.

This can be CPU-optimized by using 'movsd' by first doing a shr ecx, 2 on your length to get it in batches of 4 bytes, and then doing the remainder with movsb. I've seen this done like this:

mov     edi, dest
mov     esi, string_address
mov     ecx, string_length
mov     eax, ecx
shr     ecx, 2
repne movsd
mov     cl, al
and     cl, 3
repne movsb ; esi and edi move along the addresses as they copy, meaning they are already set correctly here
  • Get the length of the second string (be sure to back up your edi in stack or another register if needed; it contains the address you need to copy the next string to)
  • Copy the second string to the destination memory (as I said, the correct address should be in edi after the first string operation)
  • For safety, add a new 0 behind it.

If you're copying the second string to the end of the first string, you need one less copy operation, but you have to make sure there is actually enough space there to copy the second string without overwriting other vital stuff.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top