Skip to content

Instantly share code, notes, and snippets.

View ashafq's full-sized avatar

Ayan Shafqat ashafq

View GitHub Profile
/* I always wondered how variable length arguments worked in C.
* So, I just ended up implementing it in x86.
* NOTE: THIS CODE BELOW ONLY WORKS IN x86, AND NOT TESTED IN ANY OTHER ARCHITECTURE.
*
* To compile:
* gcc -Wall -o prog vargs.c && ./prog
* >> Testing: Hello world 10 -100
*/
/*
@ashafq
ashafq / Readme.md
Last active December 8, 2016 01:36
This is a simple example/tutorial on how to intermix assembly (x86_64) code with go.

The source files below should be arranged as:

./main.go
./sqrt/sqrt.s
./sqrt/sqrt.go

To run:

@ashafq
ashafq / tgabs.h
Last active December 8, 2016 02:21
How to write a type generic ABS() macro with GNU extension.
#ifndef ABS
#ifdef __GNUC__
#define ABS(x) __extension__({ \
__builtin_choose_expr( \
__builtin_types_compatible_p(__typeof__(x), long int), \
__builtin_labs(x), \
__builtin_choose_expr( \
__builtin_types_compatible_p(__typeof__(x), int), \
__builtin_abs(x), \
@ashafq
ashafq / l33t.py
Last active June 30, 2017 12:49
1337 §p€@k
__module_name__ = "Hexchat L33T"
__module_version__ = "1.0"
__module_description__ = "Do you speak l33t? No? Well, now you can with /l33t"
__author__ = "ashafq"
import hexchat
import random
def l33t_enc(string):
@ashafq
ashafq / conv.c
Created June 1, 2017 00:20
Convert an array of float to int16_t
#include <stddef.h>
#include <assert.h>
#include <immintrin.h>
static void v_f32_to_s1x15i(int16_t *dst, const float *src, size_t len)
{
const __m128 minval = _mm_set1_ps(-1.0F);
const __m128 maxval = _mm_set1_ps(+0x7fffff8p-27F);
const __m128 scale = _mm_set1_ps(32768.0F);
@ashafq
ashafq / rdtsc.rs
Last active April 5, 2018 22:03
Works on rustc 1.27.0-nightly (fb44b4c0e 2018-04-04)
#![feature(asm)]
fn rdtsc() -> u64 {
let eax: u32;
let edx: u32;
unsafe {
asm!("rdtsc; ": /* Assembly */
"={eax}"(eax), "={edx}"(edx) : /* Output registers */
: /* Input registers */
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>
/**
* @brief A shitty password generator in C++
*/
//
// It's 2019, yet no command line parsers in C++ :'(
//
#include <boost/program_options.hpp>
namespace opt = boost::program_options;
@ashafq
ashafq / fizzbuzz.S
Created May 8, 2020 17:23
Fizzbuzz in RV32IM assembly
//
// FizzBuzz in RISC-V Assembly
// build: riscv64-elf-gcc -mcmodel=medany -nostdlib \
// -march=rv32im -mabi=ilp32 -o start start.s
// run: Open in Ripes simulator ;)
//
.section .rodata
// Strings need to be aliged to two byte boundaries
#include <immintrin.h>
static void mat8x8mul(float *dst, const float *src_a, const float *src_b)
{
// Load 8 rows of B into 8 YMM registers
__m256 b0 = _mm256_load_ps(src_b + (0 * 8));
__m256 b1 = _mm256_load_ps(src_b + (1 * 8));
__m256 b2 = _mm256_load_ps(src_b + (2 * 8));
__m256 b3 = _mm256_load_ps(src_b + (3 * 8));
__m256 b4 = _mm256_load_ps(src_b + (4 * 8));