I’m in the process of writing an assembly program that takes two strings as input and concatenates them. Here’s what I have: (using NASM syntax)
SECTION .data
hello: db "Hello ",0
world: db "world!",0
SECTION .text
; do the concatenation
Since I’ve never done any work with strings in x86 assembly before, I need to know how storing and manipulating strings work in the first place.
I’m guessing that once the length of each string is known, that concatenating would simply involve moving chunks of memory around. This part can be simplified by using libc. (I can use strlen() and strcat().)
My real problem is that I’m not familiar with the way strings are stored in x86 assembly. Do they just get added to the stack…? Do they go on a heap somewhere? Should I use malloc() (somehow)?
The strings in your example are stored the same way a global character array would be stored by a C program. They’re just a series of bytes in the data section of your executable. If you want to concatenate them, you’re going to need some space to do it – either do it on the stack, or call
malloc()to get yourself some memory. As you say, you can just usestrcat()if you are willing to call out tolibc. Here’s a quick example I made (AT&T syntax), using a global buffer to concatenate the strings, then print them out: