What is the actual relation between assembly, machine code, bytecode, and opcode?
I have read most of the SO questions about assembly and machine code, such as this,
You have clearly done some homework of your own on this, and I say good stuff (and voted you up one).
As you are experiencing, the more you read, the more you say, "huh ?"
Okay, first off, when you encounter the word "bytecode" just close the window and stop reading, because you are on the wrong path; probably a tangent at best and at worst you could be reading someone trying to sound smarter than he really is by tossing techhy sounding buzzwords into his writing.
Now, as for the word "opcode", yes those really do exist, but do understand that those numbers are actually symbolic, for humans to grasp conceptually. In real life, they are super-ultra-tiny switches.
If you really like history, and technology before the internet (or color TV for that matter) look up phrases like butterfly switches, vacuum tubes, butterfly girls, and I forget the other words. This was back before transistors existed. The original huge computers actually used vacuum tubes and generated enough heat to warm an entire floor (or two or three) of an office building in the dead of Winter. The electrical current draws were astounding.
The thing to keep in your mind about all this is that those computers were "programmed" by individually flipping butterfly switches ("bat handles" were another term sometimes used) which connected and disconnected individual lines from individual tubes, and I forget what else.
The facts were: You programmed a computer by flipping the bat handles that were connected to the lines that were connected to various tubes.
When you write an opcode of 90h, (I think that's a NOP in x86, somebody correct me and I'll fix it) you are doing (with today's hi-tech wowee-zowee) the same thing that the butterfly girls did back in the stone age of computers.
Specifically, you are "throwing" these "butterfly switches"...
Here's the big difference (and part of today's hi-tech wowee-zowee)...
They had to throw exactly those switches at exactly one place on the floor. You will be flipping them anywhere you want. Three other programs will cooperate and make those decisions for you.
Those three programs are - The Assembler - The Linker - The Loader
So then (I hope) that this has helped lay the foundation for you to understand that the OPCODE is a mental representation of a bunch of little switches that will be "opened" or "closed".
(Actually, the hi-tech wowee-zowee has taken it a step further, but it's the same effect as the butterfly switches of previous gnerations.)
Anyway, it works like this.
Humans decided that there would be an instruction to do nothing; called a NOP
So, you type the letters NOP
in your text editor like this
NOP ;This is a No operation instruction
You then save the file.
You then ask the assembler to assemble that file
When the assembler sees the NOP
he creates the 90
(in hex) in the Object file which he is creating for the linker.
The Linker uses the object file and creates an executable file
The Loader places that executable file wherever it wants. (Note, in olden days of microcomputers, the software writer had to decide where to place that executable file; that was conflict bait like you wouldn't believe.)
Anyway, the NOP
became 90
in some place in the EXE
file and the loader stuck it in a good area for you, based on 179 rules you don't have to worry about any longer.
The loader then gets out of the picture and lets your program have the CPU.
The CPU fetches your first instruction and starts obeying.
When the CPU gets to the byte containing 90
it will be the same thing as the butterfly switches from generations past.
While the current will not be traveling a bunch of long wires on the floor, it will be doing highly similar (and functionally equivalent) things inside the ASIC.
Now with all that written (thanks if you're still actually reading) you can understand this boiled down one line explanation of what an opcode actually is...
Now for your second question about what is machine code.
If any of this is unclear, ask in the comments section and I'll try to edit this answer.