Skip to content

Instantly share code, notes, and snippets.

@JonathonReinhart
Created August 30, 2015 08:40
Show Gist options
  • Save JonathonReinhart/bb0897216ac0688c3d2f to your computer and use it in GitHub Desktop.
Save JonathonReinhart/bb0897216ac0688c3d2f to your computer and use it in GitHub Desktop.
x86-64 registers structure definition
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#define __packed __attribute__((packed))
/* https://commons.wikimedia.org/wiki/File:Table_of_x86_Registers_svg.svg */
#define REGDEF_hl(Z) \
union __packed { \
uint64_t r##Z##x; \
uint32_t e##Z##x; \
uint16_t Z##x; \
struct __packed { \
uint8_t Z##l; \
uint8_t Z##h; \
}; \
}
#define REGDEF_l(Z) \
union __packed { \
uint64_t r##Z; \
uint32_t e##Z; \
uint16_t Z; \
uint8_t Z##l; \
}
#define REGDEF_nol(Z) \
union __packed { \
uint64_t r##Z; \
uint32_t e##Z; \
uint16_t Z; \
}
#define REGDEF_bwd(Z) \
union __packed { \
uint64_t r##Z; \
uint32_t r##Z##d; \
uint16_t r##Z##w; \
uint8_t r##Z##b; \
}
struct uc_x86_state {
REGDEF_hl(a);
REGDEF_hl(b);
REGDEF_hl(c);
REGDEF_hl(d);
REGDEF_l(sp);
REGDEF_l(bp);
REGDEF_l(si);
REGDEF_l(di);
REGDEF_bwd(8);
REGDEF_bwd(9);
REGDEF_bwd(10);
REGDEF_bwd(11);
REGDEF_bwd(12);
REGDEF_bwd(13);
REGDEF_bwd(14);
REGDEF_bwd(15);
REGDEF_nol(ip);
REGDEF_nol(flags);
};
#undef REGDEF_hl
#undef REGDEF_l
#undef REGDEF_bwd
#undef REGDEF_nol
#define pr(x) printf("offset of " #x ": 0x%lX\n", offsetof(struct uc_x86_state, x))
int main(void)
{
pr(rax);
pr(eax);
pr(ax);
pr(al);
pr(ah);
__asm("mov %rsp,%rax\n");
__asm("mov %esp,%eax\n");
__asm("mov %sp,%ax\n");
__asm("mov %spl,%al\n"); /* I didn't even know this existed */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment