News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Allocating the right amount of memory ...

Started by James Ladd, May 08, 2005, 04:27:56 AM

Previous topic - Next topic

James Ladd

I need to allocate 'n' times a structure in memory.
Like 'n' * sizeof MYSTRUCT

Im ok on how to do the allocation, but not on the multiplication to arrive at the correct
allocation size. ie: I cant just do a    mov eax, var * sizeof MYSTRUCT

So how do I do this ?

rgs, striker

hutch--

A couple of questions, how many times must you do the calculation . If its within loop code I would be looking for a fast way to do it and this will depend if its a constant multiplication factor or not. Shifts, LEA and ADD are the type of instructions that come to mind if its a constant, if not, you can use the integer capacity of FP code to do a simple MUL or code a normal integer mul with MUL or IMUL.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

James Ladd

I do it once, and currently I do this....


    mov eax, sizeof STRUCT
    mov ecx, count_of_items_i_want
    mul ecx
    ; go on and allocate 'eax' bytes


Do this appear correct ?

rgs, striker

hutch--

I doubt that you will improve on that for a variable size mul.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

thomasantony

Hi,
   If the size of your array is a power of 2 (like 2, 4,8,16,32 etc). You can use shifts instead. SHL for multiply, SHR for divide. For example for multiplying eax by 8 do

shl eax,3  ; 8 = 2^3

and for multiplying by 32

shl eax,5  ; 32 = 2^5


Thomas :U
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

Mark Jones

Hutch, could it be any faster if the math were "offloaded" to the ALU? Maybe this is a question for Michael. :)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

raymond

Quote from: striker on May 08, 2005, 05:25:28 AM
I do it once, and currently I do this....


    mov eax, sizeof STRUCT
    mov ecx, count_of_items_i_want
    mul ecx
    ; go on and allocate 'eax' bytes


Do this appear correct ?


rgs, striker


You can save yourself one line of code with:
    mov eax, sizeof STRUCT
    mul  count_of_items_i_want


AND, being done only once in your app, you would be wasting a zillion times more time trying to optimize those two lines compared to the fractional nanosecond you could save. ::) ::) ::) ::)

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

James Ladd

Thanks for the replies.
I just wanted to know how to do it, not the fastest way.

Raymond - thanks for saving me 1 line. Ill use your approach.