Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View mepcotterell's full-sized avatar
💭
I may be slow to respond.

Michael Cotterell mepcotterell

💭
I may be slow to respond.
View GitHub Profile
@mepcotterell
mepcotterell / gcd.s
Last active February 19, 2019 12:32
/* This file contains an unoptimized, recursive GCD implementation. */
/* The code assumes %rdi and %rsi will be unsigned... if signed, then cqto will be needed to sign-extend %rax to %rdx:%rax. */
/* What are some ways to optimize it? */
.data
fmtstr:
.ascii "%d\n"
.text
.globl gcd

%eflags

Flags

Flag Description
CF Carry Flag
PF Parity Flag
AF Adjust Flag
ZF Zero Flag

Interval Timer Notes

The system provides each process with three interval timers, each decrementing in a distinct time domain. When any timer expires, a signal is sent to the process, and the timer (potentially) restarts.

Timers will never expire before the requested time, but may expire some (short) time afterwards, which depends on the system timer resolution and on the system load; see time(7). Upon expiration, a signal will be generated and the timer reset.

@mepcotterell
mepcotterell / syscall_asm.md
Last active January 15, 2020 20:35
Sytstem Call Notes

System Call Notes

Invoking System Calls

To invoke a system call:

  1. Set %rax to the system call number (see Full Table below);
  2. Set %rdi, %rsi, %rdx, %r8, %r10, %r9 (in order) according to the glibc prototype given in the manual; then
  3. Perform syscall instruction.

main.s

	.text
	.globl main
	.type main, @function
main:
	pushq 	%rbp		;
	movq	%rsp, %rbp	;
	pushq	%rbx ;

main.s

	.text
	.globl main
	.type main, @function
main:
	pushq 	%rbp		;
	movq	%rsp, %rbp	;
	pushq	%rbx ;

main.c

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
 pid_t pid = fork();
@mepcotterell
mepcotterell / Makefile
Last active January 15, 2020 20:35
Assembly Notes
main: main.o asm.o
gcc -o main main.o asm.o
main.o: main.s
as --gstabs+ -o main.o main.s
main.s: main.c main.h
gcc -std=c17 -Wall --pedantic-errors -g -O0 -S -o main.s main.c
asm.o: asm.s main.h

pmem.c

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

int a = 4;
int b;

void foo(void) { }