Message-ID: <348B9501.6054@mailexcite.com> Date: Mon, 08 Dec 1997 01:35:18 -0500 From: Doug Gale Reply-To: dgale AT mailexcite DOT com MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: use esp? References: <34896044 DOT 7E7C AT cs DOT tu-berlin DOT de> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: oshawappp10.idirect.com Lines: 50 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Christian Fleischer wrote: > > Hello! > > I am writing a 3d-engine under djgpp with nasm. > Is it somehow possible to redirect the esp-register, so that it does not > point to the stack but just to calculate something? The program also has > some interrupt-routines itself (keyboard-handler etc.) but I think they > could be turned off so long with cli and sti... > > Christian It is possible to use esp in a calculation, as long as interrupts are disabled. !!! DON'T DO IT !!! The cli instruction is VERY slow in protected mode (it takes hundreds of cycles). It would be MUCH faster to make space on the stack (see below) or even just "push" a register that you don't need for a moment, use it for the calculation, and "pop" it back. One way to make temporary storage on the stack for any number of local variables anywhere in ASM: I will do this example in Intel syntax because NASM was specified. ; I need a register here, but I have run out sub esp,4 ; Allocate 4 bytes on the stack mov [esp],eax ; Initialize newly allocated space sub [esp],edx ; Subtract some number from it shl [esp],3 ; Multiply by 8 mov ecx,[esp] ; Get the result into a register add esp,4 ; Release stack allocated storage You can reserve more space by subtracting more from esp (example: 8 for 2 dwords, 12 for 3 dwords, etc). You would then use [esp], [esp+4], [esp+8] for the 3 dword example. Just remember to release same amount with the add esp,X line or it will certainly crash. Also, you can do a memory-to-memory move using this: push [_src] pop [_dst] Although this is a bit slower than the usual method using a register, it can be used when your registers are FULL and you can't spare a register to copy the variable.