Skip to content

Instantly share code, notes, and snippets.

View Chubek's full-sized avatar

Behrang Nevii Chubek

View GitHub Profile
@Chubek
Chubek / README.md
Last active February 15, 2024 13:09
DJB2 Hash Function in Aarch64/ARM64 & x86-64 Assembly

Intro

DJB2 is a very simple, yet powerful, hash function. I am currently implementing DEFLATE + Zlib in Rust. DEFLATE needs a hash function to keep track of duplicate strings. The fastest way we can do this without SIMD is impelementing it in Assembly.

DJB2 hash function, as defined in http://www.cse.yorku.ca/~oz/hash.html, is:

    unsigned long
    hash(unsigned char *str)
 {
@Chubek
Chubek / README.md
Last active March 31, 2023 04:56
Lazy-splitting in Python

Lazy-splitting means getting the first n token-separated substrings without splitting the entire string first. I wrote a heuristic that does this in Python.

The method I provide has its ups, and downs. It is faster on larger streams, and slower on smaller streams. For smaller stream, just the calling convention, initializations, etc takes much more time than CPython's implementation. But with larger streams, of course, it's much faster, as it does not split the entire stirng all at once.

First let's go through how I prototyped this: Let's generate a file of size 1MB, using Python's string mode (-c flag):

touch rand && python3 -c "from random import choices;from pathlib import Path;Path('rand').write_bytes(bytearray(choices(list(range(65, 122)),k=1000000)))" && wc -c rand
@Chubek
Chubek / README.md
Created April 5, 2023 05:33
DJB2 Hash in Aarch64 & x86-64 Assembly

The following two files contain implementation of DJB2 hash algorithm in both Aarch64 and x86-64 Assembly languages.

What is DJB2?

DJB2 is a simple hash function. It is defined as:

unsigned long hash(char *str) {
        unsigned long hash = 5381;
@Chubek
Chubek / EXAMPLE.LICENCE.TPP.MD
Last active April 6, 2023 01:50
Totem Pro Parte Meta-License

Copyright (C) for each working part 2023 Chubak Bidpaa;
All the working parts in the software included in this repository are released 'totem pro parte' under MIT License;

Parts replication allowed (Y/N):

? Part ? Part
Y code snippets Y functions
Y subroutines Y classes
N structures Y arrays
Y variables Y else
@Chubek
Chubek / README.md
Last active April 16, 2023 14:28
Encoding all 29 Genetics Table + Several Codon Frequency Tables for use in Assembly

Background

So, for the past several hours I have began a bioinformatics project that I suspect may end sooner than I expected, and that project is, a DNA to Protein translator and reverse-translator aptly titled TransGatacca. The main crux of the program is in x86-64 and Aarch64 Assembly languages. However it is desined to be callable from C and will also have bindings in Rust using global_asm! macro. There will also be Python bindings.

The aim of this software is speed, and speed alone, so I am trying to do as many micro-optimizations as possibly there can be. One of these venues is of course, the genetics table --- and later, codon frequency table. One needs to encode these tables in such way that they can be represented all inside one 64-bit register or memory address with zero overflow, and that the remain unique across all the possible permutations of the nucleotide triplets, and finally, they can be represented a contiguous series of memory

@Chubek
Chubek / README.md
Last active January 27, 2024 15:15
Zinteger.py: Dynamically-generated wrappers for ctypes integers
@Chubek
Chubek / README.md
Last active April 22, 2023 00:16
SpudMac: Cross-Arch x86-64 and Aarch64 GAS Macros with help from CPP

SpudMacs: Cross-Architecture GAS & CPP Macros

Introduction

In this gist I present two files, spudmacs.x86-64.S and spucmacs.aarch64.S. These two files contain identically-named and signatured GAS and CPP macros (in the case of derived macros) that achieve the same task in both Aarch64 and x86-64 Assmbly languages. There are two types of macros in both files:

  • Gnu Aassembler Macros (base and derived)
  • C Prerprocessor Macros (Idents and Literals)

These macros aim to make cross-arch assembly programs possible. You can expand on them yourself based on your needs, especially with CPP macros that do not cover all the basis.

@Chubek
Chubek / README.md
Last active April 28, 2023 15:25
PRNG subroutine for x86-64 & Hash subroutine for Aarch64

The following gist contains two Assembly files, one x86-64 in AT&T syntax and the other, an Aarch64 Assemly code. It also has a C file expressing how the two subroutiens may be used, as the subroutines in both files are callable in C. Keep in mind that both .S files are annotated with comments on the very top of the file.

Intro and Disclaimer

These subroutines were mainly authored for the sake of education. My cousin passed away yesterday and I'm upset so I wanted to keep busy. Please do not use these two subroutines in any serious manner unless you have scrutinized them well and you are sure they will endow you with the necessary functionality.

Note on the Assembler Macros and Diretives

Note that both these files contain GNU Assembler macros, and as we will see, they are meant to be compiled using GCC or Clang along with a C program declaring them as external symbols.

@Chubek
Chubek / README.md
Created May 17, 2023 00:22
dnsrezulf.py Request A, AAAA, TXT and CNAME Records from any DNS Resolver

I originally wrote dnsrezulf.py to prototype the Assembly + C code for the DNS resolver in Panah. That being said, I decided to package it separately as a Gist because I reckon it merits that. You may find a very simple, yet fast, DNS Resolver in C + Assembly, only for Linux, in the afore-linked repository.

What is dnsrezulf.py?

Basically, this Python script is the Walter Blanco to dig's Walter White. If dig is partying at the club, dnsrezulf.py is sitting at hoome, popping ritty after ritty, and writing useless code whilst he runs out of money. If dig is Daniel J. Bernstein, dnsrezulf.py is Terry A. Davis. If dig is Linux, dnsrezulf.py is a magnetic tape which has to be manually handled to read the next punch card. Basically, you get the gist (pun intended).

That being said, dnsrezulf.py contains implementation of the DNS protocol from scratch, based on RFC #1035. It is also commented to hell and back, and reading the entire script from top to bottom, bot

@Chubek
Chubek / README.md
Last active May 31, 2023 13:46
Tools to Mix C and Assembly

This gist contains C preprocessor files to mix C and Assembly easier.

  • syscnr.inc includes all the Linux system call numbers.
  • registers.inc includes all the Linux ABI registers for both C and system calls. Arguments start at 0.
  • syscall.inc includes macros for syscalls based on argument number.

Please do not remove the newline at the end of each file. Head to my Github profile where you will find a lot of goodies, a lot of projects and the such and so. My project Panah has some good Assembly code mixed with C.

Enjoy.