Jump to content

Is Assembly language compiled?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
I can't quite wrap my head around how a text file of assembly code is converted to a program. It's already basically in machine language, just with mnemonics. How do you compile assembly code? Also, how can you tell what version of Assembly language works on your platform? Sorry if this is a stupid question.
Life's too short to be cool. Be a nerd.

#2
innerLOL

innerLOL

    Newbie

  • Members
  • PipPip
  • 29 posts
each instruction is a sequence of bytes.

labels are translated to addreses.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,822 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
How you compile it depends on the target processor. Assembly is NOT machine code, however. They are still quite different.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
It's basically the same way any other language is converted to machine code. I'll walk you through an example using Intel code:
; eax = edx
mov    eax, edx
If we look at the Intel Instruction Set Reference Manual we can look at the MOV opcodes (page 689). We know that EAX and EDX are 32-bit registers, so we look for a MOV encoding that enables us to move a 32-bit register to another 32-bit register. If we look carefully, there are two ways we can do this:

Quote

MOV r/m32, r32
MOV r32, r/m32
(In case you didn't know, r/m stands for "register or memory.")
Since both operands are registers, and there's no register-to-register encoding, we have to pick one of these. Let's just go with the first one, r/m32, r32.
If you look to the column that says "Opcode," you see 89/r. This means that the opcode is 0x89 plus a MOD-R/M byte indicating what the operands are. I'm not going to get into detail, but there's a table you can use to determine what the proper encoding is.
Once we've determined the MOD-R/M byte, we can encode our instruction into binary: 0x89 0xD0. Compilation is just this process over and over again for each instruction. (It's a little more complicated with jump labels and function calls, but that's not the point here.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users