3
$\begingroup$

From what I understand, the x86 family of processors understands the same instruction set and x86-32, x86-16 machine code can be executed by an x-86-64 processor because of backwards compatibility.

Now since Machine Code is usually produced from Assembly Language and we use an assembler for that purpose, does that mean that the family of processors doesn't define an Assembly Language but it is rather the Assembler that we use since it actually parses the code?

Let's say I have an Assembly Code like this which is assembled by MASM for an 80386 processor:

MOV AX, 5
ADD AX, 10

Now assume that this another Assembly Code which is assembled by another hypothetical assembler (lets say HASM) for the same 80386 processor:

MOV 5
ADD 10

Q1: So for the same processor we can have two different Assembly Code which when assembled using the right assembler can produce the same Machince Code?

Q2: So let's say I have a PC running another family of processor (ARM) and write an Assembly Code and assemble it by MASM which produces machine code compatible for the 80386 processor. Now, without executing, it I copy the machine code over to another PC with the 80386 processor, would the code run Okay?

Q3: If I have two PCs with 80386 processors, one running Windows & the other linux. If I use MASM to assemble a source code on Windows for the 80386 processor, why then, when I copy the machine code over to Linux, it won't execute? Both PCs are running the same processor and machine code is the same! How does OS come into play here? (Please assume no OS specific API function has been used)

$\endgroup$
2
$\begingroup$

Q0: > does that mean that the family of processors doesn't define an Assembly Language but it is rather the Assembler that we use since it actually parses the code?

It's not the processor/family that defines the detailed syntax of the assembly language for a particular CPU - the Assembler program is what defines the assembly language it accepts. You can have two different/incompatible assembly languages for the same processor/family.

Of course the CPU itself will determine what concepts are needed in the assembly language - e.g. 16 registers or 8, of different sizes, etc.

Q1: So for the same processor we can have two different Assembly Code which when assembled using the right assembler can produce the same Machince Code?

Yes. "right assembler"=>"corresponding assembler"

Q2: So let's say I have a PC running another family of processor (ARM) and write an Assembly Code and assemble it by MASM which produces machine code compatible for the 80386 processor. Now, without executing, it I copy the machine code over to another PC with the 80386 processor, would the code run Okay?

No, because the machine code is processor-/family-specific and you are copying between different processor/family

Q3: If I have two PCs with 80386 processors, one running Windows & the other linux. If I use MASM to assemble a source code on Windows for the 80386 processor, why then, when I copy the machine code over to Linux, it won't execute? Both PCs are running the same processor and machine code is the same! How does OS come into play here? (Please assume no OS specific API function has been used)

At a low level the generated binary code of e.g. an instruction to move a constant into a register is obviously compatible between these two PCs because it is aimed at the same processor/family.

HOWEVER even if it isn't obvious in the assembly code you write, when turned into a binary this is inevitably tightly related to the Operating System - all useful executable programs rely on many Operating System services and these are not the same between your two PCs, so the program won't run in any meaningful way. Maybe it might be possible for the ultra pedantic to come up with a raw binary which could be run on any operating system, but if it's independent of the operating system then it couldn't do anything useful like input or output which is provided by the OS, so while it might prove a point, that point wouldn't actually be very useful.

$\endgroup$
  • $\begingroup$ About Q2, I said I was assembling the source code for the 80386 processor but on a different machine not running that processor. I am NOT running the source code. So if I move the machine code over to the desired/compatible 80386 processor, it should work right? $\endgroup$ – Parker Queen Oct 11 '18 at 17:41
0
$\begingroup$

Assembly language and assembler (assembly language compiler which produces machine code) is necessarily architecture-dependent.

What might still be architecture-independent that you may have in mind is called the CIL (common intermediate language) which is the lowest-level human-readable language.

Q1: It may happen to be so for some short assembly code but generally not always so for large, long, complicated assembly program. NASM and MASM have different mnemonic but are able to produce machine code for nearly the same variety of architectures. The produced codes are generally different due to various tricks and tips thrown in.

Q2: It is called cross-compiler. You can compile your code on a supercomputer using top-secret NSA compiler tools then run it on an 80386 processor.

Q3: Surely, you cannot. Linux and Windows have different executable file formats. You can read more about COFF, ELF, Windows .exe files. It is just that OS needs to read the executable file load some codes into RAM.

$\endgroup$
  • $\begingroup$ Can you please answer the three questions I posted at the end? I already know of the answer you provided in its abstraction but I wanted a more practical understanding. $\endgroup$ – Parker Queen Oct 10 '18 at 15:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.