Skip to content

Instantly share code, notes, and snippets.

@Benabik
Created January 31, 2018 15:40
Show Gist options
  • Save Benabik/c34f8e390fe5a975336d2c882669f2bc to your computer and use it in GitHub Desktop.
Save Benabik/c34f8e390fe5a975336d2c882669f2bc to your computer and use it in GitHub Desktop.
Quick function to dump the current state of the registers
# x64, System V ABI, gas/llvm-as, OS X
# Can be called from C:
# void reg_dump();
# https://www.uclibc.org/docs/psABI-x86_64.pdf
# Return: %rax =(128 high bits in %rdx)
# Params: %rdi, %rsi, %rdx, %rcx, %r8, %r9, stack RTL
# Syscall: %rax, %rdi, %rsi, %rdx, %r10, %r8, %r9, stack RTL
# Callee Save: %rbx, %rbp, %rsp, %r12, %r13, %r14, %r15
# Caller Save: %rax, %rcx, %rdx, %rsi, %rdi, %r8, %r9, %r10, %r11
.globl _reg_dump
_reg_dump:
# Set RBP
pushq %rbp
mov %rsp, %rbp
# Save flags first
pushfq
# Save param registers (rdi saved later)
pushq %rsi
pushq %rdx
pushq %rcx
pushq %r8
pushq %r9
# Load non-param registers on stack for printf
subq $8, %rsp # alignment (starts on +8, need %16 at call)
pushq -8(%rbp) # flags
pushq 8(%rbp) # old RIP
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %r11
pushq %r10
lea 0x10(%rbp), %r10 # old RSP
pushq %r10
pushq %rdi
pushq (%rbp) # old RBP
pushq %rbx
pushq %rax
movb $0, %al # 0 vector regs
movq reg_format@GOTPCREL(%rip), %rdi
call _printf
# Caller save registers
mov 0x00(%rsp), %rax
mov 0x18(%rsp), %rdi
mov -0x10(%rbp), %rsi
mov -0x18(%rbp), %rdx
mov -0x20(%rbp), %rcx
mov -0x28(%rbp), %r8
mov -0x30(%rbp), %r9
mov 0x28(%rsp), %r10
mov 0x30(%rsp), %r11
lea -8(%rbp), %rsp # Keep flags on stack
popfq
popq %rbp
ret
reg_format:
.ascii "rax = %6$016lx, rbx = %7$016lx\n", \
"rcx = %3$016lx, rdx = %2$016lx\n", \
"rbp = %8$016lx, rsi = %1$016lx\n", \
"rdi = %9$016lx, rsp = %10$016lx\n", \
"r8 = %4$016lx, r9 = %5$016lx\n", \
"r10 = %11$016lx, r11 = %12$016lx\n", \
"r12 = %13$016lx, r13 = %14$016lx\n", \
"r14 = %15$016lx, r15 = %16$016lx\n", \
"rip = %17$016lx\n", \
"flags = %18$016lx\n\0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment