Skip to content

Instantly share code, notes, and snippets.

@sw17ch
Created August 16, 2012 20:40
Show Gist options
  • Save sw17ch/3373458 to your computer and use it in GitHub Desktop.
Save sw17ch/3373458 to your computer and use it in GitHub Desktop.
#include <stdio.h>
typedef struct {} a;
typedef struct { a a1; a a2; } b;
int main(int argc, char * argv[])
{
a as[999999];
printf("%d\n", sizeof(b) == sizeof(a));
printf("%zu\n", sizeof(as));
int i;
for(i = 0; i < sizeof(as) / sizeof(as[0]); i++)
{
puts("wat");
}
return 0;
}
/* $ gcc wat.c -O0 -o wat_
* $ ./wat_
* 1
* 0
* wat
*/
Copy link

ghost commented Aug 16, 2012

loop fragment:

{{{

0000000100000edd jmp 0x100000ef7
0000000100000edf leaq 0x0000007d(%rip),%rax
0000000100000ee6 movq %rax,%rdi
0000000100000ee9 callq 0x100000f24
0000000100000eee movl 0xdc(%rbp),%eax
0000000100000ef1 addl $0x01,%eax
0000000100000ef4 movl %eax,0xdc(%rbp)
0000000100000ef7 movl 0xdc(%rbp),%eax
0000000100000efa movslq %eax,%rax
0000000100000efd cmpq %rcx,%rax
0000000100000f00 jb 0x100000edf
}}}

Copy link

ghost commented Aug 16, 2012

sorry, forgot how to do github


0000000100000edd    jmp 0x100000ef7
0000000100000edf    leaq    0x0000007d(%rip),%rax
0000000100000ee6    movq    %rax,%rdi
0000000100000ee9    callq   0x100000f24
0000000100000eee    movl    0xdc(%rbp),%eax
0000000100000ef1    addl    $0x01,%eax
0000000100000ef4    movl    %eax,0xdc(%rbp)
0000000100000ef7    movl    0xdc(%rbp),%eax
0000000100000efa    movslq  %eax,%rax
0000000100000efd    cmpq    %rcx,%rax
0000000100000f00    jb  0x100000edf

Copy link

ghost commented Aug 16, 2012

0xdc(%rbp) is the loop counter, temporarily stashed in %rax / %eax
%ecx is the loop limit, set to zero at ..ebe
branch enters at ..ef7 and loop body at ..edf
cmpq at ..efd sets CF 0 and ZF 0, jb at ..f00 should NOT branch. does this particular compilation emit the "wat"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment