Skip to content

Instantly share code, notes, and snippets.

@lucasdemarchi
Created June 23, 2016 22:41
Show Gist options
  • Save lucasdemarchi/f3944595d3e2bca3cf420359c85c2745 to your computer and use it in GitHub Desktop.
Save lucasdemarchi/f3944595d3e2bca3cf420359c85c2745 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <inttypes.h>
union Bla {
int a;
char b;
inline uint8_t &operator[](size_t i) { return reinterpret_cast<uint8_t *>(this)[i]; }
inline uint8_t operator[](size_t i) const { return reinterpret_cast<const uint8_t *>(this)[i]; }
};
union Bli {
int a;
char b;
uint8_t bytes[0];
};
void foo(int p)
{
Bla bla{};
bla.a = p;
printf("%#.2x %#.2x %#.2x %#.2x\n", bla[0], bla[1], bla[2], bla[3]);
}
void bar(int p)
{
Bli bli{};
bli.a = p;
printf("%#.2x %#.2x %#.2x %#.2x\n", bli.bytes[0], bli.bytes[1], bli.bytes[2], bli.bytes[3]);
}
int main(int argc, const char *argv[])
{
int p = atoi(argv[1]);
foo(p);
bar(p);
return 0;
}
0000000000400630 <foo(int)>:
400630: 89 f8 mov %edi,%eax
400632: 41 89 c0 mov %eax,%r8d
400635: 0f b6 f0 movzbl %al,%esi
400638: 0f b6 d4 movzbl %ah,%edx
40063b: c1 e8 10 shr $0x10,%eax
40063e: 41 c1 e8 18 shr $0x18,%r8d
400642: 0f b6 c8 movzbl %al,%ecx
400645: bf 90 07 40 00 mov $0x400790,%edi
40064a: 31 c0 xor %eax,%eax
40064c: e9 9f fe ff ff jmpq 4004f0 <printf@plt>
400651: 66 66 66 66 66 66 2e data16 data16 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
400658: 0f 1f 84 00 00 00 00
40065f: 00
0000000000400660 <bar(int)>:
400660: 89 f8 mov %edi,%eax
400662: 41 89 c0 mov %eax,%r8d
400665: 0f b6 f0 movzbl %al,%esi
400668: 0f b6 d4 movzbl %ah,%edx
40066b: c1 e8 10 shr $0x10,%eax
40066e: 41 c1 e8 18 shr $0x18,%r8d
400672: 0f b6 c8 movzbl %al,%ecx
400675: bf 90 07 40 00 mov $0x400790,%edi
40067a: 31 c0 xor %eax,%eax
40067c: e9 6f fe ff ff jmpq 4004f0 <printf@plt>
400681: 66 66 66 66 66 66 2e data16 data16 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
400688: 0f 1f 84 00 00 00 00
40068f: 00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment