Skip to content

Instantly share code, notes, and snippets.

View magniff's full-sized avatar
🦀
Fe₂O₃ · nH₂O

Aleksandr Koshkin magniff

🦀
Fe₂O₃ · nH₂O
  • Flock Safety
  • San Francisco
  • 12:49 (UTC -07:00)
  • YouTube @0xfabaceae
View GitHub Profile
@magniff
magniff / main.rs
Created October 9, 2025 15:57
hello world in machine code (rust, linux)
use std::mem;
use std::ptr;
fn main() {
// x86_64 machine code: write(1, "hello world\n", 12)
const CODE: &[u8] = &[
0xb8, 0x01, 0x00, 0x00, 0x00, // mov eax, 1
0xbf, 0x01, 0x00, 0x00, 0x00, // mov edi, 1
0x48, 0x8d, 0x35, 0x08, 0x00, 0x00, 0x00, // lea rsi, [rip+0x8]
0xba, 0x0c, 0x00, 0x00, 0x00, // mov edx, 12
@magniff
magniff / main.rs
Created March 31, 2025 20:50
CPU name printer
use std::arch::x86_64::__cpuid;
unsafe fn get_vendor_name() -> String {
let vendor_name = __cpuid(0);
let mut vendor_name_string = String::with_capacity(12);
vendor_name_string.push_str(&String::from_utf8_lossy(&vendor_name.ebx.to_le_bytes()));
vendor_name_string.push_str(&String::from_utf8_lossy(&vendor_name.edx.to_le_bytes()));
vendor_name_string.push_str(&String::from_utf8_lossy(&vendor_name.ecx.to_le_bytes()));
vendor_name_string
}
@magniff
magniff / spinlock.rs
Created May 13, 2023 18:49
Spinlock in Rust
use std::cell::UnsafeCell;
use std::sync::atomic::{AtomicBool, Ordering};
static UNLOCKED: bool = false;
static LOCKED: bool = true;
struct SpinLock<T> {
inner: UnsafeCell<T>,
state: AtomicBool,
}
@magniff
magniff / walrus_tuple.py
Created September 9, 2022 20:39
walrus
(
sys := __import__("sys"),
your_value := input(),
factorial := lambda value: 1 if not value else factorial(value-1) * value,
(
print("Error: Your input is not a decimal number"),
sys.exit(1),
)
if not str.isdecimal(your_value)
else (result := factorial(int(your_value))),
@magniff
magniff / fib.py
Created September 9, 2022 20:14
fibonacci
(
(lambda fibonacci: lambda v, then: fibonacci(fibonacci, v, then))
(
lambda fibonacci, value, then:
then(1)
if
value == 0 or value == 1
else
fibonacci(
fibonacci,
from __future__ import annotations
from dataclasses import dataclass
from typing import Callable, Generic, TypeVar, cast, NoReturn, Optional, Any
I = TypeVar("I")
O = TypeVar("O")
R = TypeVar("R")
T = TypeVar("T")

myparserlib =)

from __future__ import annotations

from dataclasses import dataclass
from typing import (
    Any, Callable, Generic, List, Optional, Sequence, Tuple, TypeVar, cast
)
@magniff
magniff / gist:fac1b6b698341c41c216d9990689a0b0
Last active November 25, 2020 15:45
PyLance: Type aliasing

Here's my code:

Parser = Callable[[str, State], ParseResult[T]]
def monadic_fail(parser: Parser[T]) -> Parser[T]:
    def new_parser(stream: str, state: State) -> ParseResult[T]:
        if isinstance(state, ParseError):
            return ParseResult(output=None, state=state)
        return parser(stream, state)
    return new_parser