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

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

问题描述

我在阅读一篇关于如何构建 Jit Compiler ,作者本质上使用这个代码:

  //处理器说明:
// 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();

除了他知道如何手动将汇编指令映射到数字代码之外, 。

解决方案

我是文章作者,希望你喜欢它! / p>

要构造这些值,我基本上做了

  $ 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:文件格式elf64-x86-64


反汇编.text:

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

您可以在左列中看到指令的字节。我不建议太深地学习指令编码,除非你有一个很好的理由。它们非常复杂,汇编程序非常善于生成这些东西。还有很多其他东西要学习在这个级别,将更好地利用你的大脑能量。请阅读 Agner Fog手册,这是一个伟大的开始。


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();

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

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天全站免登陆