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.
3 replies to this topic
#1
Posted 25 December 2009 - 12:34 PM
Life's too short to be cool. Be a nerd.
|
|
|
#2
Posted 25 December 2009 - 12:49 PM
each instruction is a sequence of bytes.
labels are translated to addreses.
labels are translated to addreses.
#3
Posted 26 December 2009 - 03:00 PM
How you compile it depends on the target processor. Assembly is NOT machine code, however. They are still quite different.
#4
Posted 28 December 2009 - 07:13 AM
It's basically the same way any other language is converted to machine code. I'll walk you through an example using Intel code:
(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.
; eax = edx mov eax, edxIf 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
MOV r32, r/m32
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


Sign In
Create Account


Back to top









