views:

4358

answers:

7

What is the difference between object code, machine code and assembly code?

Can you give a visual example of their difference?

A: 

Assembly code is discussed here.

"An assembly language is a low-level language for programming computers. It implements a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture."

Machine code is discussed here.

"Machine code or machine language is a system of instructions and data executed directly by a computer's central processing unit."

Basically, assembler code is the language and it is translated to object code (the native code that the CPU runs) by an assembler (analogous to a compiler).

nzpcmad
+19  A: 

Assembly code is plain-text and (somewhat) human read-able source code that has a mostly direct 1:1 analog with machine instructions. This is accomplished using mnemonics for the actual instructions/registers/other resources. Examples include things like JMP or MULT for the jump and multiplication instructions.

Machine code is binary (1's and 0's) code that can be executed directly by the cpu. If you were to open a "machine code" file in a text editor you would see garbage, including unprintable characters.

Object code is a portion of machine code that hasn't yet been linked into a complete program. It's the machine code for one particular library or module that will make up the completed product. It may also contain placeholders or offsets not found in the machine code of a completed program that the linker will use to wire everything together.

Joel Coehoorn
+1: nice, but somewhat simplifying answer - not all assembly instructions are translated 1:1 to machine instructions, and object files may also contain other data (relocation information, symbol tables, ...)
Christoph
Added a weasel word for your first issue, edited to make the 2nd clearer.
Joel Coehoorn
+9  A: 

Assembly code is a human readable representation of machine code:

mov eax, 77
jmp anywhere

Machine code is pure hexadecimal code:

5F 3A E3 F1

I assume you mean object code as in ab object file. This is a variant of machine code. With a difference that the jumps are sort of parameterize such that a linker can fill them in.

An assemler is used to convert assembly code into machine code (object code) A linker links several object (and library) files to generate an executable.

I have once written an assembler program in pure hex (no assembler available) luckily this was way back on the good old (ancient) 6502. But I'm glad there are assemblers for the pentium opcodes.

Gamecat
Respect! I have tried to do a little of hex coding. Like I said Respect!
WolfmanDragon
No no no no. Machine code is not hex code. it's pure binary. Hex code is just a convenient representation of binary.
Breton
If we are really going into extremes, its not binary, it is an amount of stored electricity in a circuit. ;-)
Gamecat
@Gamecat, good answer !
Simucal
+2  A: 

8B 5D 32 is machine code

mov ebx, [ebp+32h] is assembly

lmylib.so containing 8B 5D 32 is object code

Quassnoi
A: 

I think these are the main differences

  • readability of the code
  • control over what is your code doing

Readability can make the code improved or substituted 6 months after it was created with litte effort, on the other hand, if performance is critical you may want to use a low level language to target the specific hardware you will have in production, so to get faster execution.

IMO today computers are fast enough to let a programmer gain fast execution with OOP.

Alberto Zaccagni
A: 

Assembly is short descriptive terms humans can understand that can be directly translated into the machine code that a CPU actually uses.

While somewhat understandable by humans, Assembler is still low level. It takes a lot of code to do anything useful.

So instead we use higher level languages such as C, BASIC, FORTAN (OK I know I've dated myself). When compiled these produce object code. Early languages had machine language as their object code.

Many languages today such a JAVA and C# usually compile into a bytecode that is not machine code, but one that easily be interpreted at run time to produce machine code.

Jim C
Your comment about Java and C# - both use Just In Time compilation so that bytecodes are not interpretted. C# (.NET generally) compiles to Intermediate Language (IL) which is then JITed into native machine language for the target CPU.
Craig Shearer
+3  A: 

The other answers gave a good description of the difference, but you asked for a visual also. Here is a diagram showing they journey from C code to an executable.

Graphics Noob