So, I understand the general abstract concept of a buffer: It's an allocation in memory that holds data before it gets processed. I'm trying to complete a homework problem which requires me to write an ASCII string into a buffer within a procedure. So, I understand that I'm supposed to pass an address of an array to the procedure when calling it, for example...
main PROC
mov EAX, packed ; pass a packed decimal to EAX
mov ESI, OFFSET ascArray ; pass the offset of an empty array to ESI
call PackedToAsc ; call the function
So the function is supposed to return 'a pointer to a buffer with the ASCII decimal string.' I'm probably being stupid here, but I'm not quite clear on what exactly a buffer is in this instance. Is it an array? Do I need to declare it as such in the .data section? How do I declare the pointer to the ASCII string inside the procedure? What is meant by a buffer in this context? More practically, I need to access the buffer that the data is put into when the procedure finishes, and I'm not sure how to do that. Sorry if I'm not being clear enough here... let me know how I can clarify the question.
EDIT -- I'm in x86, and I'm using MASM.
Assuming x86, this depends on whether your buffer starts with data in it or not. It also depends on if your buffer is variable size.
Taking case one, and you know that your buffer will never exceed, say, 20 bytes, you can declare it in the data section (NASM syntax):
buffer: times 20 db 0
In the data section declares 20 0
bytes that you can now use. If you don't need to initialize with data, you use the .bss section (NASM syntax):
buffer: resb 20
Which tells NASM to reserve 20 bytes.
If the size of your buffer is variable, however, things aren't so easy. You have to dynamically allocate memory from your OS, and this is very OS dependent. You basically have 2 options:
malloc
: This may be easier or harder, depending on platform calling conventions. There's a reasonable reference heremalloc
. They usually involve adding another page of memory to your process and letting you manage it yourself. This is what malloc does internally and is the only option if you can't use the C library.