Bu dokümanda, projede bulunan x86-64 Assembly kodlarını nasıl derleyip çalıştırabileceğinizi öğreneceksiniz.
- Ön Gereksinimler
- Yöntem 1: Online Compiler (En Kolay)
- Yöntem 2: NASM (Önerilen)
- Yöntem 3: Visual Studio (MASM)
- Yöntem 4: C# ile Test Etme
- Yöntem 5: Debugger ile Adım Adım
Bu projede kullanılan Assembly kodu formatı:
- Mimari: x86-64 (64-bit)
- İşletim Sistemi: Windows
- Calling Convention: Microsoft x64 (RCX, RDX, R8, R9 parametreler)
- Syntax: MASM-benzeri
Assembly kodunu tarayıcıda derleyip test edin.
- https://godbolt.org adresine gidin
- Sol üstten "Language" seçeneğini "Assembly" yapın
- "Compiler" olarak "x86-64 MSVC" veya "x86-64 gcc" seçin
- Kodunuzu yapıştırın ve sonuçları görün
Not: Godbolt kodu çalıştırmaz, sadece derler ve makine kodunu gösterir.
- https://www.onlinegdb.com adresine gidin
- "Language" seçeneğini "Assembly - NASM" yapın
- Kodunuzu yapıştırın
- "Run" butonuna basın
Dikkat: Online GDB için NASM formatına çevirme gerekebilir.
NASM (Netwide Assembler), Windows, Linux ve macOS'ta çalışan popüler bir assembler'dır.
Windows:
# Chocolatey ile:
choco install nasm -y
# Veya manuel indirme:
# https://www.nasm.us/pub/nasm/releasebuilds/
# nasm-x.xx.xx-installer-x64.exe dosyasını indirin ve kurunLinux:
sudo apt-get install nasmmacOS:
brew install nasmAssembly dosyalarınız MASM formatında olduğu için önce NASM formatına çevirmeniz gerekir.
MASM Formatı (Mevcut):
.code
add_numbers proc
mov rax, rcx
add rax, rdx
ret
add_numbers endp
endNASM Formatı (Çevrilmiş):
bits 64
section .text
global add_numbers
add_numbers:
mov rax, rcx
add rax, rdx
retDerleme komutu:
nasm -f win64 dosya.asm -o dosya.objLinking (DLL oluşturma):
# Visual Studio varsa:
link /dll /out:assembly_lib.dll dosya.obj
# MinGW varsa:
ld -shared -o assembly_lib.dll dosya.objMicrosoft'un resmi assembler'ı. Visual Studio Build Tools ile birlikte gelir.
- Visual Studio Installer'ı indirin
- "C++ Desktop Development" workload'unu yükleyin
- "MSVC Build Tools" seçeneğini işaretleyin
Developer Command Prompt açın:
Başlat > Visual Studio 2022 > Developer Command Prompt for VS 2022
Derleme komutu:
ml64 /c dosya.asmLinking:
link /dll /out:assembly_lib.dll dosya.objProjede bulunan bir dosyayı derlemek için:
cd "c:\Users\...\basic-assembly\03_add"
ml64 /c example_01.asm
link /dll /out:add_functions.dll example_01.objAssembly fonksiyonlarını C# ile çağırarak test edin.
NASM formatında math_functions.asm oluşturun:
bits 64
section .text
global add_numbers
add_numbers:
mov rax, rcx
add rax, rdx
retDerleyin:
nasm -f win64 math_functions.asm -o math_functions.obj
link /dll /out:math_functions.dll math_functions.objTestAssembly.cs dosyası:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("math_functions.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern long add_numbers(long a, long b);
static void Main()
{
long result = add_numbers(10, 20);
Console.WriteLine($"10 + 20 = {result}");
}
}Derleyin ve çalıştırın:
csc TestAssembly.cs
.\TestAssembly.exeBeklenen çıktı:
10 + 20 = 30
Assembly kodunu satır satır çalıştırarak öğrenin.
Kurulum:
- https://x64dbg.com adresinden indirin
- Zip dosyasını açın
x64dbg.exeçalıştırın
Kullanım:
- File > Open > DLL dosyanızı seçin
- CPU penceresinde assembly komutları görünür
- F7 ile adım adım ilerleyin
- Yazmaçları sağ panelden izleyin
- Visual Studio'da C++ projesi oluşturun
- Project Properties > Linker > Input > Additional Dependencies
- Assembly DLL'inizi ekleyin
- Breakpoint koyun ve F10/F11 ile ilerleyin
- Registers penceresini açın (Debug > Windows > Registers)
Kurulum:
winget install Microsoft.WinDbgKullanım:
.load dll_yolu
bp fonksiyon_adi
g
r (yazmaçları göster)
t (tek adım ilerle)
Projede bulunan 03_add/example_01.asm dosyasını test edelim.
03_add/example_01_nasm.asm oluşturun:
bits 64
section .text
global add_params
add_params:
mov rax, rcx
add rax, rdx
retcd 03_add
nasm -f win64 example_01_nasm.asm -o example_01.obj
link /dll /out:add_lib.dll example_01.objusing System;
using System.Runtime.InteropServices;
class Test
{
[DllImport("add_lib.dll")]
static extern long add_params(long a, long b);
static void Main()
{
Console.WriteLine($"5 + 10 = {add_params(5, 10)}");
Console.WriteLine($"100 + 250 = {add_params(100, 250)}");
Console.WriteLine($"-5 + 15 = {add_params(-5, 15)}");
}
}Çalıştır:
csc Test.cs
.\Test.exeBeklenen çıktı:
5 + 10 = 15
100 + 250 = 350
-5 + 15 = 10
Visual Studio Developer Command Prompt kullanın veya PATH'e ekleyin:
$env:PATH += ";C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.xx.xxxxx\bin\Hostx64\x64"NASM'ı PATH'e ekleyin:
$env:PATH += ";C:\Program Files\NASM"DLL dosyasının .exe ile aynı klasörde olduğundan emin olun veya tam yol belirtin:
[DllImport(@"C:\tam\yol\add_lib.dll")]Visual Studio Build Tools'u doğru yükleyin:
# Link.exe'nin yolu:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.xx.xxxxx\bin\Hostx64\x64\link.exe- İlk aşama: Online compiler (Godbolt) ile kodları test edin
- İkinci aşama: NASM kurarak lokal olarak derleyin
- Üçüncü aşama: C# ile test kodları yazın
- İleri seviye: Debugger ile adım adım çalıştırın
- NASM Documentation: https://www.nasm.us/doc/
- MASM Reference: https://docs.microsoft.com/en-us/cpp/assembler/masm/
- x86-64 Instruction Set: https://www.felixcloutier.com/x86/
- Godbolt Compiler Explorer: https://godbolt.org
- x64dbg: https://x64dbg.com
- Bu projede kullanılan MASM formatı
.code,.data,proc,endpgibi direktifler içerir - NASM formatı
section .text,global, basit etiketler kullanır - Windows x64 calling convention: İlk 4 parametre RCX, RDX, R8, R9
- Dönüş değeri her zaman RAX'te
- Yazmaçları korumak için PUSH/POP kullanın (ileri seviye konular)