How to write X86_64 _assembler_?

Purpose: I want to write assembler X86_64. Note: tagged community wiki

Reference Information. I am familiar with C. I already wrote the MIPS assembly. I wrote some x86 assembly. However, I want to write an assembler x86_64 - it should output machine code with which I can go and start execution (for example, in JIT).

Question: how can this be approached? I understand that this problem looks very attractive. I want to start with a basic minimum set:

  • Upload to Register
  • Arithmetic operators on registers (only integers in order, no need to mess with FPU)
  • Conditionals
  • Goes over

Just a basic set to make it complete. Has anyone done this? Suggestions / resources?

+5
source share
2 answers

Assembler, like any other "compiler", is best written in the form of a lexical analyzer that feeds the grammar of the language into the processor.

Assembly language is usually simpler than regular compiled languages, since you don’t have to worry about line-crossing lines, and the format is usually fixed.

I wrote assembler for a (fictional) processor about two years ago for educational purposes and basically looked at each line as:

  • optional label (e.g. :loop).
  • (e.g. mov).
  • (e.g. ax,$1).

The easiest way to do this is to make sure tokens are easily distinguishable.

That's why I made the rule that labels should start with :- this made it easier to parse the string. String Processing Process:

  • ( ; ).
  • , .
  • - .
  • - .

, , . , . Intel AT & T, .

, , , (, doJmp, doCall, doRet), , .

, doCall , doRet .

, encInstr:

private static MultiRet encInstr(
    boolean ignoreVars,
    String opcode,
    String operands)
{
    if (opcode.length() == 0) return hlprNone(ignoreVars);
    if (opcode.equals("defb"))  return hlprByte(ignoreVars,operands);
    if (opcode.equals("defbr")) return hlprByteR(ignoreVars,operands);
    if (opcode.equals("defs"))  return hlprString(ignoreVars,operands);
    if (opcode.equals("defw"))  return hlprWord(ignoreVars,operands);
    if (opcode.equals("defwr")) return hlprWordR(ignoreVars,operands);
    if (opcode.equals("equ"))   return hlprNone(ignoreVars);
    if (opcode.equals("org"))   return hlprNone(ignoreVars);

    if (opcode.equals("adc"))   return hlprTwoReg(ignoreVars,0x0a,operands);
    if (opcode.equals("add"))   return hlprTwoReg(ignoreVars,0x09,operands);
    if (opcode.equals("and"))   return hlprTwoReg(ignoreVars,0x0d,operands);

hlpr... , . , , adc , add and `, ( , ).

, , , . .

, ( ), , , , .

CPU, , .

, . . , . ignoreVars ( , , 0).

+8

, . , , , elftoolchain.

+6

All Articles