Is there any way to use the few avaliable registers as buffers? as defining the buffers really creates exe-bloat...

could somebody maybe give a few examples?

thanks!
Posted on 2003-06-09 21:01:21 by Drocon
By buffers I am assuming you mean a 4 byte text buffer. If you consider that most of the time you use a memory pointer when using your buffer, then it would not seem possible. A register is not in memory so it has no memory address that can be used for a pointer, so there is no way to pass it's offset to a function.
Posted on 2003-06-09 21:25:33 by donkey
i recall seeing the stack register (esp) used as a buffer somewhere... but i don't understand how it works...

i always thought it was possible to use registers as buffers in API calls, like receiving data in recv()...

:(
Posted on 2003-06-09 22:00:43 by Drocon

i recall seeing the stack register (esp) used as a buffer somewhere... but i don't understand how it works...


You mean, like this?


something proc
sub esp,512
mov eax,esp
invoke lstrcpy,eax,addr some_string
[do real work... ]
add esp,512
ret
something endp

Then, there is nothing special about it.
It is still a buffer in the memory. esp happens to (to be precise, is set up to) be the beginning address of the buffer in this set up.
Posted on 2003-06-09 22:09:58 by Starless
The stack register esp actually points to an address in memory when dereferenced so it could be used sort-of. In reality you are using the stack as the buffer the esp register is simply holding the address.

Recently I wrote a routine that used the process heap for the buffers, basically I allocated 2K on the heap then set up pointers inside that memory. This does not add to your executable size in any significant manner.

invoke GetProcessHeap
mov hHeap,eax
invoke HeapAlloc, hHeap, HEAP_ZERO_MEMORY, 2048
mov pBase,eax
mov pOutput,eax
add eax,1024
mov pCommand,eax
add eax,512
mov pSysDir,eax

As long as you keep the pointer to the base, you can continually use this buffer for anything or multiple things if you like. Since it is allocated at run-time it does not bloat your exe
Posted on 2003-06-09 22:14:42 by donkey

The stack register esp actually points to an address in memory when dereferenced so it could be used sort-of. In reality you are using the stack as the buffer the esp register is simply holding the address.

Recently I wrote a routine that used the process heap for the buffers, basically I allocated 2K on the heap then set up pointers inside that memory. This does not add to your executable size in any significant manner.

invoke GetProcessHeap
mov hHeap,eax
invoke HeapAlloc, hHeap, HEAP_ZERO_MEMORY, 2048
mov pBase,eax
mov pOutput,eax
add eax,1024
mov pCommand,eax
add eax,512
mov pSysDir,eax

As long as you keep the pointer to the base, you can continually use this buffer for anything or multiple things if you like. Since it is allocated at run-time it does not bloat your exe


so in this case, pBase, pOutput, etc. can be used as a buffer? In this case, what would they be definied as? a DWORD i assume...

thanks for the help though!!
Posted on 2003-06-10 00:15:21 by Drocon
All memory addresses are defined as DWORDs it is after all a 32 bit OS ;)

PBase should be left alone, it just permanently keeps your base address you need it for HeapFree later. The others can be buffers and local if you like.

Yes they are meant to be used as buffers. Remember also that the pointers are just that pointers, if you later want to rearrange the buffers you simple have to change the values in the pointers:

mov eax,pBase
mov pNewBuffer1,eax
add eax,256
mov pNewBuffer2,eax
add eax,256
mov pNewBuffer3,eax
add eax,512
mov pCommand,eax
add eax,512
mov pSysDir,eax

Just don't go over the end of the buffer. pSysDir in this case will start at 1.5 Kb and run to the end of the buffer.
Posted on 2003-06-10 00:24:50 by donkey
You can just use the stack for buffer. Local variables are using the stack.
Posted on 2003-06-10 04:25:00 by roticv
you miss understanding the register purpose. If esi value are 0x1100, so you are point to mem adddress at 0x1100. Its different use mov esi,eax and mov ,ax. Its completely different.




mov [esi],ax ;are valid
mov esi,ax ; are invalid because it have a different size

mov esi,0x110 ; Esi are now 0x110
mov [esi],eax ; esi still 0x110
mov esi,eax ; esi changed to eax value.



Keep learning. :alright: . You can reach me at Programming Room:1 YahooChat Room, if you still confused.
Posted on 2003-06-12 00:14:55 by realvampire