2
$\begingroup$

I have a question about machine words. We have data and a code segment in the memory which is addressed. If we take a word from it and let's say it is a code (instruction), and another one is some data. Can we have a situation that some word is sometimes code and sometimes data?

$\endgroup$
1
$\begingroup$

If your question is "can one and the same sequence of bits sometimes represent a machine instruction and sometimes represent data" then the answer is definitely yes.

If your question is "can one and the same location in memory sometimes hold a machine instruction and sometimes hold data" then the answer is it depends on the machine architecture. The Harvard architecture strictly segregates instruction memory and data memory into physically separate parts of the computer memory, with separate data buses. However, the Von Neumann has a single common memory which can hold either instructions or data at any location.

If your question is "can one and the same location in memory be treated sometimes as an instruction and sometimes as data by one and the same program" then the answer in a Von Neumann architecture is yes, in principle. However, this leads to the possibility of writing self-modifying code, which is considered undesirable because such code is difficult to maintain and is a security risk. Therefore all modern operating systems make sure that the programs they are running have a strict segregation between data and instructions, and cannot modify their own code. This is usually implemented within the operating system itself rather than at the physcial memory level.

Note, however, that the distinction between data and code still depends on context - what is data to one program may be code for another program. For example, the output of an assembler, which the assembler treats as data, will be treated as machine instructions when the assembled program is actually being run.

$\endgroup$
0
$\begingroup$

The von Neumann architecture (more or less what all current CPUs have as a ground model) specifically stores programs in memory, where they can be manipulated as data or executed. Your compiler does write instructions to be executed later. When loading the program into memory to be executed the instructions are handled as regular data to be copied from disk. Each time you copy a program for example, you are manipulating it as inert data.

Allowing a program to fiddle with it's own (or even other program's) instructions is clearly a sure recipe for a giant mess or grievous security issues. Modern CPUs handle protection bits as part of the virtual memory machinery, allowing the operating system to mark memory areas as read-only, read-write, or execute-only. Some stack smashing attacks targeting stack overflows work exactly by overwriting a memory area with data and later tricking the program to execute it as instructions.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy