My simple library

..of useful code



Chapters

x86-64 Assembly & Microcontroller Guide

Comprehensive reference for x86-64 assembly, microcontroller communication protocols, and Arduino programming

x86-64 Assembly Fundamentals

Registers

Register Bits Purpose
RAX 64 Accumulator (return value)
RBX 64 Base register
RCX 64 Counter (loop control)
RDX 64 Data (I/O operations)
RSI 64 Source index (string operations)
RDI 64 Destination index (string operations)
RSP 64 Stack pointer
RBP 64 Base pointer (stack frames)
R8-R15 64 Additional general purpose registers
Register Naming Conventions

Smaller portions of registers can be accessed:

  • EAX - Lower 32 bits of RAX
  • AX - Lower 16 bits of RAX
  • AH/AL - High/low 8 bits of AX

Basic Instructions

Data Movement

mov rax, rbx      ; Copy RBX to RAX
mov [rdi], rax    ; Store RAX at address in RDI
mov rax, [rsi]    ; Load RAX from address in RSI
lea rdi, [rax+4]  ; Load effective address (RAX+4) into RDI

Arithmetic Operations

add rax, rbx      ; RAX = RAX + RBX
sub rcx, 10       ; RCX = RCX - 10
imul rdx         ; RDX:RAX = RAX * RDX (signed)
div rbx          ; RAX = quotient, RDX = remainder (unsigned)
inc rax          ; RAX++
dec rbx          ; RBX--

Control Flow

cmp rax, rbx      ; Compare RAX and RBX
jmp label        ; Unconditional jump
je label         ; Jump if equal (ZF=1)
jne label        ; Jump if not equal (ZF=0)
jg label         ; Jump if greater (signed)
ja label         ; Jump if above (unsigned)
call function    ; Call a function
ret              ; Return from function

Common Algorithms

String Length

; Input: RDI = string address
; Output: RAX = length
strlen:
    xor rax, rax      ; RAX = 0 (counter)
.loop:
    cmp byte [rdi+rax], 0
    je .done
    inc rax
    jmp .loop
.done:
    ret

Memory Copy

; Input: RDI = dest, RSI = src, RDX = count
memcpy:
    mov rcx, rdx      ; RCX = count
    rep movsb         ; Repeat move byte from [RSI] to [RDI]
    ret

Factorial

; Input: RAX = n
; Output: RAX = n!
factorial:
    cmp rax, 1
    jle .base_case
    push rax
    dec rax
    call factorial
    pop rbx
    imul rax, rbx
    ret
.base_case:
    mov rax, 1
    ret

Microcontroller Communication

UART (Serial Communication)

// Arduino UART example
void setup() {
  Serial.begin(9600); // Initialize UART at 9600 baud
}

void loop() {
  if (Serial.available() > 0) {
    char received = Serial.read();
    Serial.print("Received: ");
    Serial.println(received);
  }
}
UART Characteristics
  • Asynchronous communication
  • Point-to-point (two devices)
  • Common baud rates: 9600, 19200, 38400, 115200
  • Configurable parameters: baud rate, data bits, parity, stop bits
  • Uses TX (transmit) and RX (receive) lines

I2C (Inter-Integrated Circuit)

// Arduino I2C Master
#include 

void setup() {
  Wire.begin(); // Join I2C bus as master
}

void loop() {
  Wire.beginTransmission(0x68); // Device address
  Wire.write(0x00); // Register address
  Wire.endTransmission();

  Wire.requestFrom(0x68, 1); // Request 1 byte
  if (Wire.available()) {
    byte data = Wire.read();
  }
  delay(100);
}
I2C Characteristics
  • Synchronous communication
  • Multi-master, multi-slave
  • Uses SDA (data) and SCL (clock) lines
  • 7-bit addressing (up to 128 devices)
  • Standard speeds: 100kHz (standard), 400kHz (fast), 1MHz (fast plus)

SPI (Serial Peripheral Interface)

// Arduino SPI Master
#include 

void setup() {
  SPI.begin();
  digitalWrite(SS, HIGH); // Deselect slave
}

void loop() {
  digitalWrite(SS, LOW); // Select slave
  byte received = SPI.transfer(0x55); // Send 0x55, receive byte
  digitalWrite(SS, HIGH);
  delay(100);
}
SPI Characteristics
  • Synchronous communication
  • Single master, multiple slaves (with chip select)
  • Uses MOSI (master out), MISO (master in), SCLK (clock), SS (slave select)
  • Full duplex (simultaneous send/receive)
  • Higher speed than I2C (up to 10MHz+)

Microcontroller Components

Timers/Counters

// Arduino Timer1 example
#include 

void setup() {
  Timer1.initialize(100000); // 100ms period
  Timer1.attachInterrupt(timerISR); // Attach ISR
}

void timerISR() {
  // Called every 100ms
}

void loop() {
  // Main program
}

Analog-to-Digital Converter (ADC)

// Arduino ADC example
void setup() {
  Serial.begin(9600);
  analogReference(DEFAULT); // Set reference voltage
}

void loop() {
  int sensorValue = analogRead(A0); // Read analog pin
  float voltage = sensorValue * (5.0 / 1023.0);
  Serial.println(voltage);
  delay(100);
}

Pulse Width Modulation (PWM)

// Arduino PWM example
void setup() {
  pinMode(9, OUTPUT); // PWM pin
}

void loop() {
  // Fade LED
  for (int duty = 0; duty <= 255; duty++) {
    analogWrite(9, duty);
    delay(10);
  }
}

Arduino Programming Guide

Basic I/O Operations

// Digital I/O
pinMode(13, OUTPUT);    // Set pin 13 as output
digitalWrite(13, HIGH); // Set pin 13 high
int state = digitalRead(2); // Read pin 2

// Analog I/O
analogWrite(9, 128);    // PWM output (50% duty)
int value = analogRead(A0); // Read analog pin

Interrupts

// Attach interrupt to pin 2
attachInterrupt(digitalPinToInterrupt(2), isr, RISING);

void isr() {
  // Interrupt service routine
}

// Detach interrupt
detachInterrupt(digitalPinToInterrupt(2));

Useful Libraries

Library Purpose
Servo.h Control servo motors
EEPROM.h Non-volatile memory access
LiquidCrystal.h LCD display control
SD.h SD card reading/writing
WiFi.h/Ethernet.h Network connectivity
RTClib.h Real-time clock functions

Serial Communication

void setup() {
  Serial.begin(115200);
  while (!Serial); // Wait for serial port
}

void loop() {
  Serial.println("Hello, world!");
  if (Serial.available()) {
    String input = Serial.readString();
    Serial.print("Echo: ");
    Serial.println(input);
  }
}

Common Peripheral Interfaces

GPIO Configuration

// Set pin modes
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);

// Read/write example
digitalWrite(LED_PIN, digitalRead(BUTTON_PIN));

Analog Sensors

// Read temperature sensor (LM35)
float tempC = analogRead(TEMP_PIN) * 0.488;

// Read potentiometer
int potValue = analogRead(POT_PIN);
int angle = map(potValue, 0, 1023, 0, 270);

Motor Control

// DC motor with L298N
void setMotorSpeed(int speed) {
  if (speed > 0) {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
  } else {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
  }
  analogWrite(ENABLE, abs(speed));
}