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...
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
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...
There is another little problem with windows and its stack allocation which can potentialy cause an exception in your app.
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
MyProc PROC LOCAL ArrayX:BYTE LOCAL ByteX:BYTE mov ByteX,10 ret MyProc ENDP
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?
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.
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 :)
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.
If you need a value that will be filled during run time with your code,
.data MyString db "Hi, I am a string variable in the .DATA section",0
If you need a temporary value in a procedure, make it a LOCAL that uses stack memory,
.data? hMain dd ?
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, email@example.com
LOCAL lpVariable:DWORD LOCAL TempString:BYTE
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. :)
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
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
Thanks guys. I'll try a little with that stuff.