Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to dynamically allocate memory for an array in assembly 8086 using nasm

So i need to do something like this in assembly

int *arr = malloc(sizeof (int) * size);

where the user inputs the size and based on the size, a while loop will be called to populate the array.

So i need a pointer to the space malloc has created how do i do this in ass 86? Also, where do i store this pointer so i can use the array later on. To say do a binary search on the input?

regards arr

like image 296
arif Avatar asked Apr 06 '11 03:04

arif


2 Answers

Somehow I'm going to guess that this isn't what you want to do.

Simply, mallocing memory in assembly isn't particularly easy.

It's easy if you're going to do a callout to a system function, in the case you need to understand the calling conventions for your operating system routines and libraries and linkers. You could be calling some kind of OS function, through, perhaps, an interrupt, and this, again, depends on the operating system.

Because normally, assembly programs tend to have pretty static views of memory, and define their own memory map. You could allocate a large block of data, and then right your own "malloc" against that block. The original block will become yours when the routine loads. This is probably closer to what you want to do, but obviously it can be a lot more work.

If you're not allocating more than one array at a time, simply define a block in the assembly source that is "big enough" (10,000 integers, say). Then you can just use that.

Assuming you do call some memory allocation routine, the result will be returned either on the stack, or in a register. You would then either leave that value in a register dedicated to the task of holding it for the remainder of your processing, or you can simply store the value in to memory someplace else, and then load it when you need it later.

like image 168
Will Hartung Avatar answered Sep 29 '22 22:09

Will Hartung


Supposing you're under Win and making a console app (give more details next time), the solution found in The Art Of Assembly Language (in capital letters because that's a great book) is:

13.3.6.1 Allocate Memory

Function (ah):     48h  
Entry parameters:  bx- Requested block size (in paragraphs)  
Exit parameters:   If no error (carry clear):  
                       ax:0 points at allocated memory block  
                   If an error (carry set):  
                       bx- maximum possible allocation size  
                       ax- error code (7 or 8)  

This call is used to allocate a block of memory. On entry into DOS,
bx contains the size of the requested block in paragraphs (groups of
16 bytes). On exit, assuming no error, the ax register contains the
segment address of the start of the allocated block. If an error
occurs, the block is not allocated and the ax register is returned
containing the error code.  
If the allocation request failed due to insufficient memory, the bx 
register is returned containing the maximum number of paragraphs actually
available.

13.3.6.2 Deallocate Memory

Function (ah):    49h  
Entry parameters: es:0- Segment address of block to be deallocated  
Exit parameters:  If the carry is set, ax contains the error code (7,9)  

This call is used to deallocate memory allocated via function 48h above. The
es register cannot contain an arbitrary memory address. It must contain a
value returned by the allocate memory function. You cannot use this call to
deallocate a portion of an allocated block. The modify allocation function
is used for that operation.

13.3.6.3 Modify Memory Allocation

Function (ah):     4Ah
Entry parameters:  es:0- address of block to modify allocation size
                   bx- size of new block
Exit parameters:   If the carry is set, then ax contains the error code 7, 8,
                   or 9
                   bx contains the maximum size possible (if error 8)

This call is used to change the size of an allocated block. On entry, es must
contain the segment address of the allocated block returned by the memory
allocation function. Bx must contain the new size of this block in paragraphs.
While you can almost always reduce the size of a block, you cannot normally
increase the size of a block if other blocks have been allocated after the
block being modified. Keep this in mind when using this function.

page 719 if you need it ;)

like image 34
BlackBear Avatar answered Sep 29 '22 20:09

BlackBear