Archive for the ‘assembler’ Category
-
Hello, NASM World!
Posted on 1月 3rd, 2012 by cx20
NASM
NASM はフリーのアセンブリ言語である。名前の由来は Netwide Assembler より。
構文の特徴としては AT&T 記法ではなく Intel 記法が使われているという特徴がある。
ソースコード(64bit版)
section .text global _main extern _puts _main: push rbp mov rbp, rsp push rbx mov rbx, rsp and spl, 0xF0 mov rdi, msg call _puts mov rsp, rbx pop rbx mov rsp, rbp pop rbp ret section .data msg db "Hello, NASM World!", 0
コンパイル&リンク方法
$ nasm -f macho64 hello.asm $ gcc -o hello hello.o
実行結果
Hello, NASM World!
-
Hello, GAS World!
Posted on 1月 2nd, 2012 by cx20
GAS
GAS は GNU プロジェクトで使用されるアセンブリ言語である。Linux カーネルの開発にも用いられている。
構文の特徴としては Intel 記法ではなく、AT&T 記法が使われているという特徴がある。
ソースコード(32bit版)
.text .globl _main _main: pushl %ebp movl %esp, %ebp andl $-16, %esp subl $16, %esp call ___main movl $_msg, (%esp) call _puts movl $0, %eax leave ret .data _msg: .asciz "Hello, GAS World!"
ソースコード(64bit版)
.text .globl _main _main: pushq %rbp movq %rsp, %rbp leaq _msg(%rip), %rdi callq _puts xorl %eax, %eax popq %rbp ret .data _msg: .asciz "Hello, GAS World!"
上記コードは以下のC言語のソースを GCC でアセンブリコード出力(gcc -S hello.c)したものに相当する。
#include <stdio.h> int main( int argc, char* argv[] ) { printf( "Hello, GAS World!n" ); return 0; }
コンパイル&リンク方法(32bit版)
$ gcc -c hello.s -m32 $ gcc -o hello hello.o
コンパイル&リンク方法(64bit版)
$ gcc -c hello.s -m64 $ gcc -o hello hello.o
実行結果
Hello, GAS World!
-
Hello, LLVM Assembler World!
Posted on 1月 1st, 2012 by cx20
LLVM
LLVM Assembly Language は LLVM で使用されている中間言語である。LLVM は Low Level Virtual Machine の略で、言語に独立な仮想マシンである。
位置づけとしては Java VM のクラスファイルや、.NET Framework に対する MSIL(中間言語)に相当する。
ソースコード(Mac 版)
target triple = "x86_64-apple-macosx10.7.2" @str = internal constant [19 x i8] c"Hello, LLVM World!0" define i32 @main(i32 %argc, i8** nocapture %argv) nounwind uwtable ssp { %puts = tail call i32 @puts(i8* getelementptr inbounds ([19 x i8]* @str, i64 0, i64 0)) ret i32 0 } declare i32 @puts(i8* nocapture) nounwind
ソースコード(MinGW 版)
; ModuleID = 'hello.c' target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-f80:128:128-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S32" target triple = "i686-w64-mingw32" @str = private unnamed_addr constant [19 x i8] c"Hello, LLVM World!0" define i32 @main(i32 %argc, i8** nocapture %argv) nounwind { %puts = tail call i32 @puts(i8* getelementptr inbounds ([19 x i8]* @str, i32 0, i32 0)) ret i32 0 } declare i32 @puts(i8* nocapture) nounwind
上記コードは以下のC言語のソースを Clang でアセンブリコード出力(clang -S -O4 hello.c)したものに相当する。
#include <stdio.h> int main( int argc, char* argv[] ) { printf( "Hello, LLVM World!¥n" ); return 0; }
コンパイル(LLVM Clang コンパイラ)
$ clang -o hello hello.ll
実行結果
Hello, LLVM World!
-
Hello, MSIL World!
Posted on 12月 31st, 2011 by cx20
MSIL
MSIL は .NET Framework で使用されている中間言語(Intermediate Language)である。
ソースコード
.assembly extern mscorlib {} .assembly hello {} .method static void Main() cil managed { .entrypoint ldstr "Hello, MSIL World!" call void [mscorlib]System.Console::WriteLine(string) ret }
上記コードは以下の C# のソースに対応する。
アセンブリファイル(.NET でコンパイルしたモジュール)を ildasm で逆アセンブル(ildasdm hello.exe /out:hello.il)することで MSIL のコードを得られる。using System; class Hello { static void Main( String[] args ) { Console.WriteLine( "Hello, MSIL World!" ); } }
コンパイル&実行方法(Mono)
$ ilasm hello.il $ mono hello.exe
コンパイル&実行方法(.NET Framework)
C:¥> ilasm hello.il C:¥> hello
実行結果
Hello, MSIL World!