Code is data....

Started by
30 comments, last by DerPhill 18 years, 3 months ago
To actually answer the OP:
Quote:
struct People {

int age = 16;

string name = "John Smith";

float height = 70; // this is in inches

Gender sex = MALE; // this is an enum



} Person;


but isn't this data as well?

Yes and no. Usually when someone says "code is data" in reference to lisp, they're talking about being able to modify code at compile-time. Most of the time, lisp macros, and thus "code is data", are used to allow for higher level modelling of data, which can also be acheived in languages which have rich data modelling capabilities. The other cases, such as implementing control structures or creating complex minilanguages, aren't particularly well-suited to simple data structures, and are usually better off being implemented some other way.
Advertisement
Wiki to the rescue.
More abstractly, in a Von Neumann machine there is no distinction between machine code and data. Both are just numbers stored in memory - it entirely depends on how the CPU interprets the data. In my computer architecture class it was common for the professor to assign programs where the data was embedded inside the code (that is, one or more of the 32 bit machine instructions in the program was used as data in the program)

Self-modifying programs also fuzzy the code-data distinction. At one point, you have code that is executing. The next, that same code is now "data" that the program is modifying. Later, that data becomes code that is executed again.
There is in principle nothing stopping you from bundling a C++ compiler with your program, permitting it to compile code into libraries on-the-fly, and calling your new libraries. It should be even simpler in Java, which already has the HotSpot compiler built into the VM anyway. It's like object orientation, in a way : Sure, you can do it in C, it's just that the language isn't built around the concept.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Quote:Original post by King of Men
There is in principle nothing stopping you from bundling a C++ compiler with your program, permitting it to compile code into libraries on-the-fly, and calling your new libraries. It should be even simpler in Java, which already has the HotSpot compiler built into the VM anyway. It's like object orientation, in a way : Sure, you can do it in C, it's just that the language isn't built around the concept.

Indeed. And if you do want to do that, you might take a look at the Tiny C compiler, which is small enough to be embedded into another application without too much pain, and also comes as a library version to help you do just that (of course, it'll only compile C code, not C++, but even so it's quite impressive)

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Quote:Original post by Alpha_ProgDes
OK. This is code:

Is it? What's it do when you run it?

How about something more abstract. Is the following code or data?
afas842630asfdasfa932134

Either or both, depending on its location in computer memory. If it is as random as it looks, it would probably cause a very nice crash if you tried to execute it, and god only knows what it would do even if it didn't crash. But any number expressible in 32 bits could in principle be a valid opcode, and any number at all is valid data.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Quote:Original post by SabreMan
afas842630asfdasfa932134


When converted to afac842630acfdacfa932134, it produces the following assembly code (from DEBUG):

SCASWLODSBTEST AH, [AC30]STDLODSBCLIXCHG BX, AXAND [SI],SI


Doesn't make much sense as code... It also comes out as gibberish as a string of text (contains special characters that won't show up right with the GDNet font, so I won't display it).

edit: For anyone who's interested, the number B81300CD10C3 is a machine-language routine to set the video mode to 13h. Expressed as a decimal, it looks like this: 202391757328579.
Quote:Original post by King of Men
Either or both, depending on its location in computer memory.

Location in computer memory has nothing to do with it. The question is whether or not it has executable semantics, and you only know that when you consider a platform of execution. That is whether it's code, whether it's data or whether it's just some garbage resulting from a cat walking across my keyboard is context-dependent. You can't correctly answer the question without the context.

Suppose I wrote a program which reads the byte-stream and performs some computations based on it. That would provide the byte-stream with executable semantics, hence from that point of view it would be code. This is how code can be contained in relational databases, XML documents, and source code files. The distinction between code and data is not as clear as many people would like to believe, which is one good reason why languages which provide a strong distinction might be considered to be overly-constraining.
Quote:
When converted to afac842630acfdacfa932134, it produces the following assembly code (from DEBUG):

What do the idiosyncracies of a particular processor family have to do with the question?
Quote:Original post by SabreMan
Quote:Original post by King of Men
Either or both, depending on its location in computer memory.

Location in computer memory has nothing to do with it. The question is whether or not it has executable semantics, and you only know that when you consider a platform of execution. That is whether it's code, whether it's data or whether it's just some garbage resulting from a cat walking across my keyboard is context-dependent. You can't correctly answer the question without the context.


Perhaps I phrased it awkwardly, but the point I was actually trying to make is the same one as yours : It depends on context. But I think I disagree with you on the 'executable syntax' bit : Any random set of numbers is code as far as I'm concerned. Sure, it may be bad code that causes your computer to crash, but that's irrelevant. What makes it code is the context of being supplied to the CPU as instructions. Data, on the other hand, is any set of numbers that code operates upon. It may become code in its turn : For example, machine code is data when sitting on a hard disk or being copied somewhere, but becomes code when loaded into memory.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.

This topic is closed to new replies.

Advertisement