Do arrays made by gcc grow downwards in x86
Posted: Wed Dec 02, 2020 11:06 pm
title.
The Place to Start for Operating System Developers
http://forum.osdev.org./
I wonder if it has anything to do with the x86 expand-up and expand-down segments.Solar wrote:But I, too, am intrigued. Why are you asking?
A person asking this sort of question probably won’t know x86 asm. A better approach would be to write a quick test program. Filling a small array with some known values, and the using pointer arithmetic to see if the array pointer -1, then -2, then -3, etc... returns the expected values.iansjack wrote:There's a very simple way to answer this sort of question. Write a simple test program and inspect the assembler code produced by the compiler.
Write an assembly routine and do just that, PUSHAD and pass ESP to a C function. If you make that C function return a new ESP value back to the assembly routine, you can conveniently switch context, not just save/restore.clementttttttttt wrote:I had an idea of save register conveniently by moving esp to an array and dump the registers using pushad.
I’m guessing this is related to the other post you made recently about multitasking issues. I think really you should allocate sufficient RAM from your main memory pool, but that said an array is a legitimate way to reserve a known size of RAM, so you could dim an array:clementttttttttt wrote:I had an idea of save register conveniently by moving esp to an array and dump the registers using pushad.
Code: Select all
uint8_t myArray[128]; // a 128 byte array
Code: Select all
void* arrayTop =(void*)&myArray[127];
Why are you saving the registers? Is there anything else that might use the stack while you have ESP pointed to the array? How will you point ESP to the correct location afterwards?clementttttttttt wrote:I had an idea of save register conveniently by moving esp to an array and dump the registers using pushad.
You probably want &myArray[128] here, since the stack pointer must point past the end of the array in order to fill the array. (Or perhaps &myArray[32] since PUSHAD only writes 32 bytes.)bloodline wrote:Then find the address of the top of that array:Code: Select all
void* arrayTop =(void*)&myArray[127];
Careful with that, GCC and Clang both assume you won't touch ESP in your inline assembly.bloodline wrote:Now use some inline assembly to load the value of the stackTop pointer into ESP
I didn’t know that, but It makes sense given C’s reliance upon the stack! Perhaps we can advise @clementttttttttt to restore a valid ESP after he’s pushed the registers into his array.Octocontrabass wrote:Careful with that, GCC and Clang both assume you won't touch ESP in your inline assembly.bloodline wrote:Now use some inline assembly to load the value of the stackTop pointer into ESP