Question

I know that the most basic (and least abstract) code for programming is machine code (with binary of 0s and 1s being the typical machine code).

I also know that computers can save data even if they are turned off, by different types of computer memory (storage device memory, RAM and other computer system devices which can "remember" some data).

Is all data stored in computers ("all data remembered in a given computer memory") stored as machine code?
Is what's saved in a computer's "memory" (and becoming actually effective by electrical current correctly distributed to the computer system) just binary machine code in the sense that if I could read and understand that data directly without an operating system interfacing it for me it should appear in my mind as machine code, or rather, is it something else?

Was it helpful?

Solution

I wouldn't put it that way. What is stored in memory is bits. It's just data in binary form.

Humans might then label some of those bits as "machine code" because we intend or expect it to be executed; or because it will be executed. That's a matter of interpretation we apply. The memory module just sees a bunch of bits. It just sees some data and has no knowledge of what the meaning or intended purpose of that data will be.

Memory is not limited to storing executable code. Computers also store other data in memory. For instance, your computer might store a copy of an email, or a picture or photograph you took. Those aren't code, but they can be stored in memory.

It may be helpful to think that the memory can be used to store data or can be used to store code -- but there is nothing in the computer architecture that enforces any distinction between the two. From the perspective of the memory module, it is just bits.

OTHER TIPS

It is sometimes regarded just the other way round. We have storage for data, everything is data in first place. Machine code is data which is meant to be read into a processor's execution pipeline. So the distinction is a semantic one.

This has become somewhat ambiguous nowadays, where we have stuff like true-type fonts, which are perceived as data, but when you look closely, form a turing-complete system.
Similarly, it is not wrong to define the data that constitutes some of the popular document formats, as programs. The PDF is a good example. But it is not quite machine code; rather it is an interpreted piece of software, several layers of abstraction away from the hardware.

The premise of your question would mostly have been true for the predecessor of computers, the loom. There, the machine code was encoded as a pattern which actually, physically represented the pattern the machine was supposed to generate from that data.

More practically speaking, real-world operating system kernels have mechanisms to distinguish between data and executable code very clearly, also for security reasons. First, there is the concept of segments in program files, which can be marked code, or data, but not both. Crazy programming patterns like self-modifying code are mostly no longer possible on modern systems, because code segments are write-protected. Second, there are concepts like data execution protection (DEP) that prevent accidental attempts to direct the processors' instruction pointers to areas of memory that, by declaration, contain data.

Licensed under: CC-BY-SA with attribution
Not affiliated with cs.stackexchange
scroll top