user1086635
user1086635

Reputation: 1544

How is a program loaded in ROM?

When you run a program on windows, it is loaded into computer memory organized as:

The data segment may contain data which is read only or has read and write access.

E.g:

char *c = "Hello World";

The string Hello World is said to be stored in read only section of memory. Is it stored in physical memory which is sometimes called RAM or is it stored in ROM which is read only? How can it be written if it is read only?

Upvotes: 1

Views: 3214

Answers (6)

Thomas Matthews
Thomas Matthews

Reputation: 57688

In many embedded systems, they have RAM and some type of Read Only Memory, often times called Flash (it can be programmed multiple times without being removed from the Printed Circuit Board).

Simple embedded applications place the executable and read-only data sections in Flash and execute out of Flash. The read/write variables are placed into RAM. Let us consider this model for your example code fragment:

  char * c = "Hello World!";

In the above statement, the variable c lives in RAM because the default setting for variables is read & write access. If you specified that the variable was constant, it would live in ROM {Actually, it would represent a location in ROM.}:

char * const c = "Hello World!"; // A constant pointer that lives in ROM.

The compiler treats the literal text "Hello World!" a little more complex. The actual text lives in ROM, either in the executable area or in a data area; depends on the translator. Many compilers will allocate memory in RAM and copy the literal into RAM and make the variable c point to the copy in RAM. This is because the literal was not specified as constant.

To avoid the copying of the literal into RAM, declare the variable pointing to constant data:

const char * c = "Hello World"; // A pointer to constant data.

The definition above still allows the pointer to point to different things during execution. If you want to refer to one instance of a text literal throughout the program, declare a constant pointer to constant data:

const * char * const c = "Hello World!"; // A constant pointer to constant data

This technique allows executables to load into RAM (for faster execution) and still access read-only data from ROM (which frees up SRAM for true read/write variables).

On most PCs, everything lives either on Non-volatile memory (hard drive, BIOS, etc) or in RAM. Common method is to load programs from ROM (includes hard drive) and execute in RAM. When loading the executable into RAM, the OS usually loads the Read-Only data into RAM also. The read-only data may be guarded by the OS so an exception is generated when the application writes to this area.

Upvotes: 2

Mark Ransom
Mark Ransom

Reputation: 308158

A PC has only one area of memory that really qualifies as ROM, and that is where the BIOS is stored. All of Windows and the programs loaded in Windows will be in RAM.

The x86 processor memory management is able to mark blocks of memory as read only, but the linker and OS have to work together to enable this. It happens after the program is loaded into memory.

Upvotes: 3

RHSeeger
RHSeeger

Reputation: 16262

If it helps, consider that there's two different ways in which memory can be read-only.

  • At the hardware level, you're referring to ROM. Once it's written to/created, the physical properties of it don't allow it's value to be changed.
  • At the software level, the OS (or other level higher than the user's program space) prevents the values stored in RAM from being modified.

When you're talking about read-only memory for program values, you're generally talking about the second type. The values are initialized when the program starts, and then the OS doesn't allow them to be modified.

Note: The above is an extremely simplistic explanation, but it does give the general idea.

Upvotes: 1

SanBen
SanBen

Reputation: 2798

It's stored in RAM.

Char pointers which are initialised with a string, like in your example are readonly. You can cast a const variable so that you can "write" to it but the behaviour of your program will not be predictable.

Readonly regions of RAM are protected by the Operating system.

This probably varies depending on what kind of system you're on. The answer above would be for your standard PC. Embedded systems might actually write the constant data to some kind of non volatile memory.

Upvotes: 1

Alan Stokes
Alan Stokes

Reputation: 18964

It is stored in RAM. The Operating System, in cooperation with the processor itself, is able to protect regions of memory so that any attempt to write to them from user code causes an exception.

Upvotes: 2

ikirachen
ikirachen

Reputation: 77

It is stored int RAM memory. ]:>

Upvotes: -1

Related Questions