了解如何手动发射处理器指令 [英] Understanding how to emit processor instructions manually

查看:130
本文介绍了了解如何手动发射处理器指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到有关如何构建的 JIT编译器,笔者基本上是使用这个code:

I was reading an article about how to construct a Jit Compiler, and the author essentially uses this code:

// Processor instructions for:
//   mov eax, 0   
//   ret 
unsigned char code[] = {0xb8, 0x00, 0x00, 0x00, 0x00, 0xc3};

void *mem = mmap(NULL, sizeof(code), PROT_WRITE | PROT_EXEC,
    MAP_ANON | MAP_PRIVATE, -1, 0);  

memcpy(mem, code, sizeof(code));

int (*func)() = mem;   
return func();

我得到了code,除了他是怎么知道如何手动映射汇编指令数值codeS的一切。我需要什么,以研究为understant呢?

I get everything about the code except for how he knew how to manually map the assembler instructions to numerical codes. What do I need to study in order to understant that?

推荐答案

我的文章的作者,希望你喜欢它!

I'm the article author, hope you enjoyed it!

要构建的价值观,我基本上没有

To construct those values, I basically did

$ cat test.S
  .intel_syntax noprefix
  mov eax, 0
  ret
$ gcc -c -o test.o test.S
$ objdump -d -M intel test.o

test.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <.text>:
   0:   b8 00 00 00 00          mov    eax,0x0
   5:   c3                      ret

您可以看到在左侧栏中的说明的字节数。我不建议学习指令编码太深,除非你有一个很好的理由。他们是pretty复杂,装配处于产生这种东西真的很不错。有吨的其他东西要学在这一层面,将更好地利用你的大脑能量。阅读瓦格纳雾手册的一个伟大的开始。

You can see the bytes for the instructions in the left column. I don't recommend studying the instruction encodings too deeply unless you have a really good reason. They're pretty complicated, and assemblers are really good at generating this stuff. There are tons of other things to learn at this level that will make better use of your brain energy. Read Agner Fog's manuals for a great start.

这篇关于了解如何手动发射处理器指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆