• Stars
    star
    112
  • Rank 310,237 (Top 7 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 3 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Compile asm code into C# functions on fly!

Asm To Delegate

Compile asm code into C# functions on fly! For now supports only x86 Windows 64-bit.

Get started

Get the lib (it's probably distributed by nuget) and it's recommended to write these usings:

using Iced.Intel;
using static Iced.Intel.AssemblerRegisters;
using AsmToDelegate;

Examples

Add two integers

Adds two integers:

var asm = new Assembler(bitness: 64);
asm.mov(rax, rcx);
asm.add(rax, rdx);
asm.ret();
var add = asm.ToFunctionPointerWinX64<ulong, ulong, ulong>();
Assert.Equal(44ul, add(31, 13));

Something complex

Finds a * b + c * d:

var asm = new Assembler(bitness: 64);

var a = rcx;
var b = rdx;
var c = r8;
var d = r9;

asm.mov(rax, a);
asm.imul(rax, b);
asm.mov(rbx, c);
asm.imul(rbx, d);
asm.add(rax, rbx);
asm.ret();
var add = asm.ToFunctionPointerWinX64<long, long, long, long, long>();
Assert.Equal(210L, add(5, 2, 10, 20));

... assuming the conventions.

Compute TSC

Returns Time Stamp Counter:

var asm = new Assembler(bitness: 64);
asm.rdtsc();
asm.shl(rdx, 32);
asm.add(rax, rdx);
asm.ret();
var tsc = asm.ToFunctionPointerWinX64<ulong>();
Console.WriteLine($"{tsc()} cycles since last reset");

What about F#?

F# has computation expressions which allow for an absolutely mind-blowing syntax.

Here's the same function which computes the number of cycles.

open AsmToDelegate.FSharp

let getCycles = asm {
    rdtsc
    shl rdx 32uy
    add rax rdx
    ret
}

printfn $"{getCycles ()}"

Feedback from Fred:

feedback

NaiveNanoBench

I was inspired by nanoBench, advanced linux tool for measuring costs of different instructions. So I made a very naive version of it for C#.

Here's an example. We measure the time taken by mov rcx, 6 instruction.

using Iced.Intel;
using static Iced.Intel.AssemblerRegisters;
using AsmToDelegate;
using NaiveNanoBench;

var movFunction = new Assembler(bitness: 64);
for (int i = 0; i < 1000; i++)
{
    movFunction.mov(rcx, 6);
}
movFunction.ret();

var compiled = movFunction.ToUnmanagedFunctionWinX64<ulong, ulong>();

var bench = new NanoBench(userInvokationsPerCall: 1000);

bench.Bench(compiled.Delegate);
bench.ShowDistributionOfResults();

It will warm up the thing until it more or less understands what time it takes to run the delegate, then it will switch to the actual measurement phase, perform 100 measurements where each of them takes approximately 1 second, and return the mean and the distribution. It all depends on a lot of factors, so I will just show the result (you can bench your own), as it's an example for the lib, not real answer to "how much time it takes to run a single mov".

mean: 175 ns.

More Repositories

1

UnitsOfMeasure

Conceptual repo. Most advanced compile time safe units of measure for C# and F#
C#
57
star
2

dotfiles

My dot files
Lua
39
star
3

LambdaCalculusFSharp

ฮป calculus library made purely in and for F#
F#
32
star
4

nvim-latex-preconfig

Neovim Preconfiguration for LaTeX
Lua
29
star
5

MoreFuncUI

Open-source collection of additional Avalonia.FuncUI components, wrappers of those of Avalonia
F#
23
star
6

CodegenAnalysis

Library for obtaining and analyzing the codegen of JIT for .NET. Can be used for both tests (assertions) and making reports (benchmarks).
C#
22
star
7

commstuff

Some code I refer to in my articles
Jupyter Notebook
16
star
8

gpt4all.nvim

Plugin to work with gpt4all. Fork of ChatGPT.nvim
Lua
15
star
9

InductiveVariadics

Variadic arguments for C# with inductive declaration. Made via C# 9's source generators
C#
12
star
10

json2fs

Tool which converts json from file into F# records
F#
11
star
11

WebAppMinimalTemplate

This is a template for web app in C#. No server, no javascript, no IDE, no manual deployment!
HTML
10
star
12

FStar.Lib.NET

Ship F* library as a .NET nuget package
F#
7
star
13

SetSystem

Types became boring? Try a set system!
6
star
14

FastForeachEnumeratorArticle

Some sources for a habr article
C#
6
star
15

WhiteBlackGoose

Go to wbg.gg
HTML
6
star
16

ScreamShell

Shell that screams huge letters
F#
5
star
17

GuessBigO

A thing which guesses the time complexity of an algorithm
C#
5
star
18

TypeInfo

HTML
4
star
19

DotNetLibraryGuide

Here's how IMO your library's structure should look like
PowerShell
4
star
20

MoreBadges

C#
3
star
21

FStarHelloWorld

My learning of F*
F*
3
star
22

FsMinimalWebpageTemplate

Template repo for a minimal html page in github pages, that is, hosted for free. The code is in F#
F#
3
star
23

FunConsoleGame

Windows-only console game with spaceships
C#
3
star
24

neovim-goose

Fork of neovim, my edition
Vim Script
3
star
25

hgt2png

Cross-platform tool for transforming hgt files into 32-bit and 64-bit pngs
C#
2
star
26

QuantumComputingMatrices

Here we collect matrices for manual transformations of quantum states
Jupyter Notebook
2
star
27

leostudio

My project from 2016 of little me
Pascal
2
star
28

Rainball

A simple 3D project with pseudo water simulation based on MxEngine
C++
2
star
29

AngouriMath.Terminal

Currently: experimental repository for creating a terminal for AM based on F# Interactive.
C#
2
star
30

frex

Fractal Explorer... my hello world with raylib
GLSL
2
star
31

dotnet-proj

CLI tool to work with Directory.Build.props/csproj/fsproj etc.
F#
1
star
32

cheatsheets

General cheatsheets for subjects/areas/sciences for me to refer to
TeX
1
star
33

ThreeLanguageAxes

3 language axes chart: Type System, Runtime, Paradigm
F#
1
star
34

MyFunRepos

This is a public repo for a website with my fun repos
F#
1
star
35

nix-utils

Collection of GNU-like utils
Shell
1
star
36

MinimalismSinglePageWebsiteTemplate

For open-source projects and libraries, if you need something minimalistic
HTML
1
star
37

AmericanKeyboardLayoutForGerman

This is a layout for German language with umlauts (รผ รค รถ รŸ), but everything else kept from American kb
1
star
38

FieldCache

A good replacement for Lazy<T>
C#
1
star
39

nsxiv-goose

My fork of nsxiv
C
1
star
40

andromeda.nvim

Colorscheme for neovim inspired by andromeda from VS Code
Vim Script
1
star
41

FunProjects

Some crazy projects, horror code
C#
1
star
42

GunsVsMonsters

My game from 2016
Pascal
1
star
43

NaiveCSharpFSharpEval

This package is only to test something. It synchronously evaluates C# or F# code and returns the string output.
F#
1
star
44

OhnoLisp

C# implementation of something looking like Lisp idk, too lazy to read the wikipage about what LISP is
C#
1
star
45

RandomAgentBasedFinancialMarketSimulation

Based on a paper
C#
1
star
46

Yadg.NET

Naive documentation generator from the .NET compiler's output
C#
1
star
47

tri

Work with images incrementally and reproducibly!
Rust
1
star
48

lamca

Lambda Calculus Calculator in Haskell
Haskell
1
star
49

typst-hello-world

Typst - replacement for LaTeX!
Typst
1
star
50

pack-deb-dotnet-example

Example of packing a .deb package (Debian/Ubuntu) of a .net app
Shell
1
star
51

asc-community

Full code of ASC Community web-site
C#
1
star