How Do Assemblers Map x86 Instruction Mnemonics to Binary Machine Instructions?

前端 未结 3 448
面向向阳花
面向向阳花 2021-02-07 18:06

I\'m interested in writing an x86 assembler. I\'m wondering what is a good way to map x86 assembly mnemonic instructions (using an Intel-like syntax) into the corresponding bina

相关标签:
3条回答
  • 2021-02-07 18:25

    For x86, it's complicated as hell. A little less complicated since 32-bit processors took over, but yeah. Still a pain.

    You may want to take a look at nasm ( http://www.nasm.us ). It's an open source 32-bit assembler. See how they do it. Or, use it instead. :)

    0 讨论(0)
  • 2021-02-07 18:28

    Do you want to understand the physical mapping of mnemonics to machine code? If so volume 2A & 2B of the the Intel IA32/IA64 reference manuals describe the binary format of x86 machine code .

    The x86 instruction set page on Wikipedia has a compact listing of all the instructions categorized by when they were introduced, which might help you prioritize what to implement first.

    However, if you are asking about how to go about parsing an assembly code text file to get to the point where your program could start writing out machine code then you basically need to understand how to write a compiler. The tools lex and yacc are good places to start but if you don't know how build a compiler you'll also need to get a book. I think the Dragon book is the best one out there but there are any number of other books you could use, SO has plenty of recommendations.

    0 讨论(0)
  • 2021-02-07 18:38

    It's just a straight-up one-to-one mapping; the Intel documentation describes all of the instructions and their encodings. You'll need to build a giant lookup table or something equivalent to do the matching and code generation.

    0 讨论(0)
提交回复
热议问题