Skip to content

Instantly share code, notes, and snippets.

@nownabe nownabe/hello.c
Last active Nov 23, 2016

Embed
What would you like to do?
RubyでRubyVMを実装してちょっとだけRubyVMの気持ちになってみる
#include <stdio.h>
int main(void) {
printf("Hello, world!");
return 0;
}
/*
$ gcc -c hello.c
$ gobjdump -d -M intel hello.o
hello.o: ファイル形式 mach-o-x86-64
セクション .text の逆アセンブル:
0000000000000000 <_main>:
0: 55 push rbp
1: 48 89 e5 mov rbp,rsp
4: 48 83 ec 10 sub rsp,0x10
8: 48 8d 3d 1b 00 00 00 lea rdi,[rip+0x1b] # 2a <_main+0x2a>
f: c7 45 fc 00 00 00 00 mov DWORD PTR [rbp-0x4],0x0
16: b0 00 mov al,0x0
18: e8 00 00 00 00 call 1d <_main+0x1d>
1d: 31 c9 xor ecx,ecx
1f: 89 45 f8 mov DWORD PTR [rbp-0x8],eax
22: 89 c8 mov eax,ecx
24: 48 83 c4 10 add rsp,0x10
28: 5d pop rbp
29: c3 ret
*/
puts "Hello, world!"
__END__
$ ruby -e 'puts RubyVM::InstructionSequence.compile_file("hello.rb", false).disasm'
== disasm: #<ISeq:<main>@hello.rb>======================================
0000 putself ( 1)
0001 putstring "Hello, world!"
0003 send <callinfo!mid:puts, argc:1, FCALL|ARGS_SIMPLE>, <callcache>, nil
0007 leave
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.