OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Mar 18, 2024 10:40 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Do arrays made by gcc grow downwards in x86
PostPosted: Wed Dec 02, 2020 11:06 pm 
Offline
Member
Member

Joined: Tue Jul 14, 2020 4:01 am
Posts: 70
title.


Top
 Profile  
 
 Post subject: Re: Do arrays made by gcc grow downwards in x86
PostPosted: Wed Dec 02, 2020 11:46 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
The plain old C/C++ arrays neither grow nor shrink.
If you allocate memory dynamically via malloc() and then realloc() it, the new pointer can be less than, equal to or greater than the old one (which you got from malloc()).
Not sure if that helps. Clarify the question if it doesn't.


Top
 Profile  
 
 Post subject: Re: Do arrays made by gcc grow downwards in x86
PostPosted: Thu Dec 03, 2020 12:19 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7612
Location: Germany
As for order of elements, consider that &x[2] needs to be "two larger than" &x[0], so having them in reverse order would only complicate things unnecessarily.

But I, too, am intrigued. Why are you asking?

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re: Do arrays made by gcc grow downwards in x86
PostPosted: Thu Dec 03, 2020 1:25 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
Solar wrote:
But I, too, am intrigued. Why are you asking?


I wonder if it has anything to do with the x86 expand-up and expand-down segments.
But it would be odd to use those and the segmentation mechanism in general.


Top
 Profile  
 
 Post subject: Re: Do arrays made by gcc grow downwards in x86
PostPosted: Thu Dec 03, 2020 3:13 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: Do arrays made by gcc grow downwards in x86
PostPosted: Thu Dec 03, 2020 3:29 am 
Offline
Member
Member
User avatar

Joined: Tue Sep 15, 2020 8:07 am
Posts: 264
Location: London, UK
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.


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.

I’m also intrigued as to why this question is being asked? Perhaps the poster is confusing arrays and stacks?

_________________
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su


Top
 Profile  
 
 Post subject: Re: Do arrays made by gcc grow downwards in x86
PostPosted: Thu Dec 03, 2020 3:32 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
I'm pretty sure that anyone posting on these forums has at least a passing acquaintance with x86 assembly language - or at least a desire to learn the basics.

But, in any case, how would such a person know whether decrementing a pointer value is moving upwards or downwards through RAM? If they understood arrays and pointer arithmetic then they wouldn't need to ask the question.


Top
 Profile  
 
 Post subject: Re: Do arrays made by gcc grow downwards in x86
PostPosted: Thu Dec 03, 2020 12:46 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
I'd like to hear why you ask as well, clementttttttttt. Could you describe the actual problem you are trying to solve?

My guess is that the real question is something along the lines, "how do I access a C-style format string (on the stack) from assembly", but that's just speculation. We'd really need to know what the OP is trying to accomplish, rather than what they are asking about, to to have any chance of helping.

EDIT
For the record: IIUC, string arguments are not stored on the stack, only pointers to the strings are (and even those might not be, depending on how the compiler optimizes the references and/or argument passing).

Constant strings (string literals and those string variables which were initialized with string literals when declared) are stored globally, usually in an .rodata section (which the paging mechanism will usually enforce by marking the mapped pages as read-only, but that would be up to your virtual memory manager), even if they are only visible locally within a given function.

Mutable global strings which are declared as a sized char array rather than initialized (which are fixed in size, but not in the data they contain) are in the global .data section.

Local mutable strings - which, remember, are just char arrays - are stored on the stack, but the array elements, like the fields of other data structures, go 'up' as far as I can tell, so that the pointer arithmetic is consistent.

For strings whose storage space is dynamically allocated, the data would be on the heap, and has to be explicitly freed when you no longer are using it (otherwise it causes a memory leak).

In all of these cases, they grow 'up' through memory, not down, no matter how the stack grows, i.e., my_array[1] is always higher in memory than my_array[0], my_array[2] is higher still, etc.

Comments and corrections welcome.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: Do arrays made by gcc grow downwards in x86
PostPosted: Thu Dec 03, 2020 6:31 pm 
Offline
Member
Member

Joined: Tue Jul 14, 2020 4:01 am
Posts: 70
I had an idea of save register conveniently by moving esp to an array and dump the registers using pushad.


Top
 Profile  
 
 Post subject: Re: Do arrays made by gcc grow downwards in x86
PostPosted: Thu Dec 03, 2020 8:37 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
clementttttttttt wrote:
I had an idea of save register conveniently by moving esp to an array and dump the registers using pushad.

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.


Top
 Profile  
 
 Post subject: Re: Do arrays made by gcc grow downwards in x86
PostPosted: Fri Dec 04, 2020 3:54 am 
Offline
Member
Member
User avatar

Joined: Tue Sep 15, 2020 8:07 am
Posts: 264
Location: London, UK
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:
Code:
uint8_t myArray[128];    // a 128 byte array


Then find the address of the top of that array:
Code:
void* arrayTop =(void*)&myArray[127];


Now use some inline assembly to load the value of the stackTop pointer into ESP (remember to save the value of ESP somewhere first!), then when you push registers they should fill the array top to bottom, as a stack.

Not sure how useful this is.

_________________
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su


Top
 Profile  
 
 Post subject: Re: Do arrays made by gcc grow downwards in x86
PostPosted: Fri Dec 04, 2020 5:07 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5069
clementttttttttt wrote:
I had an idea of save register conveniently by moving esp to an array and dump the registers using pushad.

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?

Using the stack this way is pretty common on some 8-bit and 16-bit architectures, where stack operations can be simpler, faster, or smaller than ordinary register-to-memory moves. It's usually not simpler on x86, since there is no convenient register you could use to keep track of the stack location while you have ESP pointed elsewhere. It's usually not faster either, although that varies depending on the CPU. The size advantage is still there though!

bloodline wrote:
Then find the address of the top of that array:
Code:
void* arrayTop =(void*)&myArray[127];

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:
Now use some inline assembly to load the value of the stackTop pointer into ESP

Careful with that, GCC and Clang both assume you won't touch ESP in your inline assembly.


Top
 Profile  
 
 Post subject: Re: Do arrays made by gcc grow downwards in x86
PostPosted: Sat Dec 05, 2020 1:24 am 
Offline
Member
Member
User avatar

Joined: Tue Sep 15, 2020 8:07 am
Posts: 264
Location: London, UK
Octocontrabass wrote:
bloodline wrote:
Now use some inline assembly to load the value of the stackTop pointer into ESP

Careful with that, GCC and Clang both assume you won't touch ESP in your inline assembly.


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.

_________________
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group