Comprehensive reference for x86-64 assembly, microcontroller communication protocols, and Arduino programming
| 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 |
Smaller portions of registers can be accessed:
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
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--
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
; 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
; Input: RDI = dest, RSI = src, RDX = count
memcpy:
mov rcx, rdx ; RCX = count
rep movsb ; Repeat move byte from [RSI] to [RDI]
ret
; 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
// 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);
}
}
// 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);
}
// 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);
}
// 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
}
// 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);
}
// 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);
}
}
// 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
// Attach interrupt to pin 2
attachInterrupt(digitalPinToInterrupt(2), isr, RISING);
void isr() {
// Interrupt service routine
}
// Detach interrupt
detachInterrupt(digitalPinToInterrupt(2));
| 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 |
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);
}
}
// Set pin modes
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Read/write example
digitalWrite(LED_PIN, digitalRead(BUTTON_PIN));
// 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);
// 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));
}
My simple library
..of useful code