HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Does Assembly Language depend on an Assembler or the family of processor?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
theprocessorlanguageassemblyassemblerdoesfamilydepend

Problem

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)

Solution

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.

Context

StackExchange Computer Science Q#98406, answer score: 3

Revisions (0)

No revisions yet.