Created
October 25, 2015 12:10
-
-
Save akx/056a5a154bd5b10c9151 to your computer and use it in GitHub Desktop.
https://computers-are-fast.github.io/ cache example w/ asm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1c1 | |
| < .file "c1.c" | |
| --- | |
| > .file "c2.c" | |
| 16d15 | |
| < pushl %edi | |
| 28a28 | |
| > xorl %ecx, %ecx | |
| 31,32d30 | |
| < movl %eax, %ecx | |
| < leal (%eax,%ebx), %edi | |
| 34c32 | |
| < jle L5 | |
| --- | |
| > jle L4 | |
| 43,44c41,42 | |
| < movb %dl, -1(%ecx) | |
| < cmpl %edi, %ecx | |
| --- | |
| > cmpl %ebx, %ecx | |
| > movb %dl, (%esi,%edx) | |
| 46c44 | |
| < L5: | |
| --- | |
| > L4: | |
| 58c56 | |
| < leal -12(%ebp), %esp | |
| --- | |
| > leal -8(%ebp), %esp | |
| 62d59 | |
| < popl %edi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> | |
| // Number to guess: How big of an array (in bytes) | |
| // can we allocate and fill in a second? | |
| // this is intentionally more complicated than it needs to be | |
| // so that it matches the out-of-order version :) | |
| int main(int argc, char **argv) { | |
| int NUMBER, i; | |
| NUMBER = atoi(argv[1]); | |
| char* array = malloc(NUMBER); | |
| int j = 1; | |
| for (i = 0; i < NUMBER; ++i) { | |
| j = j * 2; | |
| if (j > NUMBER) { | |
| j = j - NUMBER; | |
| } | |
| array[i] = j; | |
| } | |
| printf("%d", array[NUMBER / 7]); | |
| // so that -O2 doesn't optimize out the loop | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .file "c1.c" | |
| .def ___main; .scl 2; .type 32; .endef | |
| .section .rdata,"dr" | |
| LC0: | |
| .ascii "%d\0" | |
| .section .text.unlikely,"x" | |
| LCOLDB1: | |
| .section .text.startup,"x" | |
| LHOTB1: | |
| .p2align 4,,15 | |
| .globl _main | |
| .def _main; .scl 2; .type 32; .endef | |
| _main: | |
| pushl %ebp | |
| movl %esp, %ebp | |
| pushl %edi | |
| pushl %esi | |
| pushl %ebx | |
| andl $-16, %esp | |
| subl $16, %esp | |
| call ___main | |
| movl 12(%ebp), %eax | |
| movl 4(%eax), %eax | |
| movl %eax, (%esp) | |
| call _atoi | |
| movl %eax, %ebx | |
| movl %eax, (%esp) | |
| call _malloc | |
| testl %ebx, %ebx | |
| movl %eax, %esi | |
| movl %eax, %ecx | |
| leal (%eax,%ebx), %edi | |
| movl $1, %edx | |
| jle L5 | |
| .p2align 4,,10 | |
| L6: | |
| addl %edx, %edx | |
| movl %edx, %eax | |
| subl %ebx, %eax | |
| cmpl %edx, %ebx | |
| cmovl %eax, %edx | |
| addl $1, %ecx | |
| movb %dl, -1(%ecx) | |
| cmpl %edi, %ecx | |
| jne L6 | |
| L5: | |
| movl %ebx, %eax | |
| movl $-1840700269, %edx | |
| movl $LC0, (%esp) | |
| imull %edx | |
| addl %ebx, %edx | |
| sarl $31, %ebx | |
| sarl $2, %edx | |
| subl %ebx, %edx | |
| movsbl (%esi,%edx), %eax | |
| movl %eax, 4(%esp) | |
| call _printf | |
| leal -12(%ebp), %esp | |
| xorl %eax, %eax | |
| popl %ebx | |
| popl %esi | |
| popl %edi | |
| popl %ebp | |
| ret | |
| .section .text.unlikely,"x" | |
| LCOLDE1: | |
| .section .text.startup,"x" | |
| LHOTE1: | |
| .ident "GCC: (i686-posix-sjlj-rev2, Built by MinGW-W64 project) 4.9.2" | |
| .def _atoi; .scl 2; .type 32; .endef | |
| .def _malloc; .scl 2; .type 32; .endef | |
| .def _printf; .scl 2; .type 32; .endef |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> | |
| // Number to guess: How big of an array (in bytes) | |
| // can we allocate and fill with 5s in a second? | |
| // The catch: We do it out of order instead of in order. | |
| int main(int argc, char **argv) { | |
| int NUMBER, i; | |
| NUMBER = atoi(argv[1]); | |
| char* array = malloc(NUMBER); | |
| int j = 1; | |
| for (i = 0; i < NUMBER; ++i) { | |
| j = j * 2; | |
| if (j > NUMBER) { | |
| j = j - NUMBER; | |
| } | |
| array[j] = j; | |
| } | |
| printf("%d", array[NUMBER / 7]); | |
| // so that -O2 doesn't optimize out the loop | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .file "c2.c" | |
| .def ___main; .scl 2; .type 32; .endef | |
| .section .rdata,"dr" | |
| LC0: | |
| .ascii "%d\0" | |
| .section .text.unlikely,"x" | |
| LCOLDB1: | |
| .section .text.startup,"x" | |
| LHOTB1: | |
| .p2align 4,,15 | |
| .globl _main | |
| .def _main; .scl 2; .type 32; .endef | |
| _main: | |
| pushl %ebp | |
| movl %esp, %ebp | |
| pushl %esi | |
| pushl %ebx | |
| andl $-16, %esp | |
| subl $16, %esp | |
| call ___main | |
| movl 12(%ebp), %eax | |
| movl 4(%eax), %eax | |
| movl %eax, (%esp) | |
| call _atoi | |
| movl %eax, %ebx | |
| movl %eax, (%esp) | |
| call _malloc | |
| xorl %ecx, %ecx | |
| testl %ebx, %ebx | |
| movl %eax, %esi | |
| movl $1, %edx | |
| jle L4 | |
| .p2align 4,,10 | |
| L6: | |
| addl %edx, %edx | |
| movl %edx, %eax | |
| subl %ebx, %eax | |
| cmpl %edx, %ebx | |
| cmovl %eax, %edx | |
| addl $1, %ecx | |
| cmpl %ebx, %ecx | |
| movb %dl, (%esi,%edx) | |
| jne L6 | |
| L4: | |
| movl %ebx, %eax | |
| movl $-1840700269, %edx | |
| movl $LC0, (%esp) | |
| imull %edx | |
| addl %ebx, %edx | |
| sarl $31, %ebx | |
| sarl $2, %edx | |
| subl %ebx, %edx | |
| movsbl (%esi,%edx), %eax | |
| movl %eax, 4(%esp) | |
| call _printf | |
| leal -8(%ebp), %esp | |
| xorl %eax, %eax | |
| popl %ebx | |
| popl %esi | |
| popl %ebp | |
| ret | |
| .section .text.unlikely,"x" | |
| LCOLDE1: | |
| .section .text.startup,"x" | |
| LHOTE1: | |
| .ident "GCC: (i686-posix-sjlj-rev2, Built by MinGW-W64 project) 4.9.2" | |
| .def _atoi; .scl 2; .type 32; .endef | |
| .def _malloc; .scl 2; .type 32; .endef | |
| .def _printf; .scl 2; .type 32; .endef |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment