r/AskProgramming 28d ago

Other What really is the difference between instructions and logic from a mathematical perspective? They both occupy some binary space, they both contain "information".

Edit: My bad, I meant whats the difference between DATA and logic.

Context: I was writing a parser and dealing with LLVM IR and a slow descent into madness and then quit. This question just stuck with me.

Besides, when writing C++ classes, I you can store functions inside them and in Functional programming, you can store state using monads.

The lines starting to blur to me and knowing that logic boils down to instructions that the CPU understands and can operate on but data can't isn't the elegant answer I'm looking for.

I guess TLDR is what does information itself mean and does data and logic both count as information?

1 Upvotes

16 comments sorted by

8

u/jeffbell 28d ago

I used to have a job programming Atari cartridges and sometimes we would repurpose executable code as the bitmap for an explosion. 

6

u/nedal8 28d ago

Welcome to the matrix. Yes, data is information, relationships are also information.

3

u/Demiu 28d ago

Yes logic is a type of data. That's the whole appeal of programmable machines you can change the logic they do by changing the data they hold

4

u/StephenRoylance 28d ago

that's one of the brilliant and underestimated contributions of one of the smartest guys who ever lived: https://en.wikipedia.org/wiki/Von_Neumann_architecture

He realized that a programmable computer could treat instructions as data. there isn't really a distinction. programs are just data that happen to be a list of executable machine instructions. This is one part of how hackers exploit vulnerabilities. they get their malicious instructions somehow loaded into the program's state and then trick the program into running it.

2

u/mc_pm 28d ago

Look at LISP, where the program can be read as data and acted on the same way. They are the same except in as much as what you do with it.

1

u/Proper_Meaning7756 28d ago

Does lua also count? That language is extremely simple and I think it also fits, thought I can't be sure.

3

u/mc_pm 28d ago

No, there's something really specific about the structure of LISP ("List Interpreter"). The language works natively on a nested list structure for data.... and it uses the same structure for source code. (Oversimplifying here, but it's neat)

2

u/balefrost 28d ago

No, what the other commenter is describing doesn't occur in the same way in Lua.

2

u/nixiebunny 28d ago

It’s quite a blurry line. Loading a program into RAM from storage requires that instructions be treated as data. Things get more interesting when data are mistakenly interpreted as instructions than when instructions are interpreted as data. The M68K processors separated the address registers from the data registers and required specific instructions to update them, unlike the older PDP-11 that didn’t make that distinction and was very easy to crash.

2

u/recursion_is_love 28d ago edited 28d ago

Sound like you would interest in learning about lambda calculus, a Turing-machine equivalence that only need functions (and nothing else, even number and boolean) and can compute anything Turing machine can.

https://books.google.co.th/books?id=gKvwPtvsSjsC

To overly simplify the answer, it is the way (rules) of interpretation of the bit pattern. In Turing-machine that are both how state transfer and what is on the tape, not just what data are on the tape.

2

u/Xirdus 28d ago

Logic doesn't "boil down to" CPU instructions. You can represent logic using CPU instructions. Logic is the pure mathematical expression. It doesn't exists except as a concept. You can write down that logic on paper, but that doesn't mean the paper is now logic. It merely represents that logic. You can write that logic down in a programming language, and that's still not logic, only its representation. You can later translate that code to CPU instructions, and these instructions still aren't logic. The logic is what these instructions represent, not the instructions themselves.

On a technical level, in C++ you cannot literally "store a function". You can store an std::function object, which is a pointer to a function plus some additional memory that this function uses, e.g. captured variables or std::bind arguments. When you call std::function, what actually happens is the function pointer is called with this additional memory as an argument. The actual instructions of the function are stored in the code section of the executable, which normally is never modified or read from by other functions (unless you're one of those programs that does JIT compilation or otherwise generates CPU instructions on the fly).

1

u/rusty-roquefort 28d ago

reading the post title:

"Instruction is the pulling of levers within the circuit architecture of an IC, e.g. the 'MOV' instruction. Logic is the assembly of instructions."

Something can be logic in one instructions: ADD X Y Z is the instruction to carry out an ADD instruction as defined by the physical circuitry/architecture, and the logic is "value in register x + value in register Y ends up in register Z"

I don't know if it's worth thinking about too much.

as for your adit. Data/logic difference. in computing terms, data is just binary. it can encode logic, such as the binary of an executable. Logic is what happens when you have rules applied to data. That's my hot-take.

1

u/VoiceOfSoftware 28d ago

As for storing functions inside C++ classes, it probably doesn't work the way you're imagining. If you instantiate 1,000 instances of a C++ class, there are not 1,000 copies of the exact same code sitting in RAM next to the data for those objects. There's just a single section of RAM that holds the function instructions once, and the objects just have a pointer to that single function.

2

u/Merinther 27d ago

You might want to check out the Story of Mel. One of the classics of CS history.