are local variables of function made on stack?if i declare array of 100000 int elements as local array in some function will this be done on stack?isnt stack limited?there are no new,malloc etc function equivalent to asm opcodes, so are this OS only functions?has anyone gone mad because all of this layers, i mean you have high class languages, little lower ones, then asm...micro asm...hardware...hardware construction...material..laws of physics..life..death,i am getting kinda desperate its too much of this:) no more OOB dreams for me...
Posted on 2001-04-22 20:28:00 by Geecko
are local variables of function made on stack?if i declare array of 100000 int elements as local array in some function will this be done on stack?isnt stack limited?
Yes all local variables are stored on the stack and yes the stack is limited. By default it's initially 1000h bytes, and can grow to 100000h bytes (though I'm not sure about this). So I wouldn't create an array of 100000 elements on the stack.
there are no new,malloc etc function equivalent to asm opcodes, so are this OS only functions?
You can't allocate memory with opcodes only, C doesn't do this either, you can use the windows API's functions GlobalAlloc/GlobalFree to allocate memory. Thomas
Posted on 2001-04-23 03:46:00 by Thomas
btw. an opcode for mem allocations wouldn't make much sense, because opcodes are interbreted by the cpu, an the cpu dosen't care about memory managment, only an OS needs to do so...
Posted on 2001-04-23 07:35:00 by thenop
There is another little problem with windows and its stack allocation which can potentialy cause an exception in your app.

MyProc PROC
LOCAL ArrayX[8192]:BYTE
LOCAL ByteX:BYTE

mov ByteX,10
ret
MyProc ENDP
The above wil cause an exception, why, because stack memory is allocated only one page (Each page is 4096 bytes) at a time so access to a page when hasn't been allocated. - . . Current page (4096 bytes) . - . . Guard page . - . . Page after . - If your stack grows, ie beyond the current page limit, it causes an exception, which is handled withen windows to allocate more space in the stack, so the guard page becomes the current page, and the page after becomes the guard page. But what will happen if you dont access to guard page to allow the stack to grow? Its quite simple, you get an exception. Sorry, this becomes quite complicated, so i woudn't worry about it to much
Posted on 2001-04-24 09:11:00 by George
is this stack the same stack as stack referenced by when we use push,pushw...etc,and if i do like: LOCAL var1 LOCAL var2 and i use just var2, how can i get to it,must i pop first var1 and only then i get var 2, or can i use some pointer that can i move within this stack?
Posted on 2001-04-24 09:45:00 by Geecko
You can use var1 and var2 just like normal variables. They are part of the stack, but the stack is just a piece of memory so you can access it without having to push and pop. Just remember that after the function returns they are destroyed (effectively) I'm sure someone can give you a more detailed explanation, but just use them! umbongo.
Posted on 2001-04-24 10:07:00 by umbongo
hi stack is not limited by its nature in curent micro's... stack is accesed by indirect adressing via ESP register, now this registel CAN GO anywhere in physical memory :) However OS code (via GDT and LDT sysyem tables) can modify range, direction of grow ond size of stack for lower process...like windows does) You go mad only when you are at the TOP of those structures and suddenly realize you know nothing about what;s UNDER you...the whole BASE under your legs can go crumbling down... when you start from the BASE and make your way UP...)i know current teachers dont do that ...eh..they need efficient robots...not inteligent ppl :) ) there is no FEAR and NO MADDNESS ...just PURE KNOWLEDGE...NO HOPE NO FEAR ..just FREEDOM ... :D i myself can make a micro with independent transistors if i have to :) ...lucky i dont kave to tough :(... make its microcode, then its OPcodes then an assembler then a FORTH system then a PASCAL compiler then a OOP Database system, then a NeuralNetowk...for it... if i can only LIVE long enough....but i dont think i will... :( besides i am also very lazy :) to do all that :)
Posted on 2001-04-24 17:12:00 by BogdanOntanu
Geeko, Memory in assembler is no big deal and it works in much the same way as any other language that can allocate memory. It is reasonably flexible depending on what you need. If you need a value initialised at startup, put it in the .DATA section of your code.

    .data
      MyString db "Hi, I am a string variable in the .DATA section",0
If you need a value that will be filled during run time with your code,

    .data?
      hMain dd ?
If you need a temporary value in a procedure, make it a LOCAL that uses stack memory,

    LOCAL lpVariable:DWORD
    LOCAL TempString[128]:BYTE
Stack size in 32 bit PE files that MASM builds is about 1 meg which is plenty big enough for most applications. This can be set with options during the build stage. When you need memory that has to be controlled in size by code such as when you open a file or load a bitmap, you use operating system functions that allocate memory. GlobalAlloc(), OLE string memory, iMalloc etc ... Just remember that when you have finished with the memory, you must deallocate it. Assembler can do all of these forms of memory usage well so don't feel as if you are restricted with what you can do, once you get the swing of it, its easy and fast to work with. In assembler, the instructions that you use (mnemonics) are lower level than operating system code and of course, operating system code will always reduce down to the same type of instructions but an operating system is a large complex body of code and when you write a program within any operating system, you are bound to use its basic functions to interact with it. DOS used interrupts to provide basic functions and these were not PURE assembler, they were just function calls with the parameters passed in registers, in win32 you use the normal stack parameters because in most instances, there are not enough registers to pass the required number of parameters. Just keep up learning and you will find that writing code in assembler is fast fun stuff. Regards, hutch@pbq.com.au
Posted on 2001-04-24 19:46:00 by hutch--
IŽm confused. IŽll try to resume what I understood. For example: #1 IŽll ask the user: How much element do you want? #2 Get the result 500 in a string. #3 Use atodw MASM32 lib function to convert 500 in eax. #4 Use GlobalAlloc to Alloc 4000 bytes. (because each element will be of 8 bytes - IŽll store QWORD values in the array) #5 In the middle of the program is realized that needs not just 500 elements, but 1000. #6 The QUESTION: How can I reallocate more 500 (4000 bytes) elements and PRESERVE the first 500 elements? #6 After all, desalocate memory space (I donŽt know very well how to do it) IŽll apreciate any help!! Thanx in advance. :)
Posted on 2001-04-25 07:00:00 by wolfao
Hi memory management is not an easy task....any OS must deal with it sooner or later :) in asm you have to do most of your job yourself, HLL compilers do thei job also :) there are many ways to do that...but i will explain only 2 here: 1. Basic prealocated memory ============================= This is simple, but still can work for some apps: it goes like this: setup the HIGHEST value your matrix will have and then prealocate that memory zone using GlobalAlocc or whatever .. then advance/decrement a current record pointer into it for example this works for our game... because we can easy setupa ... let's say 255 max units per player...then we have max 8 players... so its maximum size is 8*256xunit_recors_size memory to alocate 2.Some management ======================= start with 500 elements pre alocated establish a low trigger limit and a high triger limit like lets say 100 so when a user want the 501 element make a new alloc of 500+100=600 elements and copy the original 500 elements into the new matrix then update the base pointer, and release original 500 elements when the user wants to delete some elements ...delay that recreating of a new array until he is below the LOW limit ... so when he reaches the 399 element decrement the matrix to 500 elements recreate.... and do the copy stuff again... this is a good start to memory management but this is a vast domain for experiments and debates :) i also care to mention that you can also do a linked list to every section you add and thus avoid the copy/recreate part ... inserts will be very fast this way....but block operations on such fragmentated matrixes will be very slow... So you must choose a method that best suites your app :) This message was edited by BogdanOntanu, on 4/25/2001 11:19:33 PM
Posted on 2001-04-25 23:12:00 by BogdanOntanu
Don't make it harder than it is, BogdanOntanu :). wolfao, if you want to increase the size of you GlobalAlloc'ed memory block, just use GlobalReAlloc. Simple as that. http://msdn.microsoft.com/library/psdk/winbase/memman_0lcz.htm This message was edited by f0dder, on 4/26/2001 9:26:11 AM
Posted on 2001-04-26 09:24:00 by f0dder
Thanks guys. I'll try a little with that stuff.
Posted on 2001-04-26 17:43:00 by wolfao