Skip to content

Instantly share code, notes, and snippets.

@petabyt
Created December 11, 2021 23:13
Show Gist options
  • Save petabyt/6acb95ce6b29f9cd46d78377d68b5d6e to your computer and use it in GitHub Desktop.
Save petabyt/6acb95ce6b29f9cd46d78377d68b5d6e to your computer and use it in GitHub Desktop.
arm32 assemble b branch instruction
void assemble_b(uintptr_t *from, uintptr_t *to, uint8_t buf[4]) {
uint32_t temp = (uintptr_t)to;
// Assemble instruction based on location
// of new addr relative to old address
if (from < to) {
temp = (temp - 8) / 4;
memcpy(buf, &temp, 4);
} else {
temp = 0xffffff - ((from - to + 4) / 4);
memcpy(buf, &temp, 4);
}
// arm32 branch "b"
buf[3] = 0xea;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment