Created
May 3, 2016 04:25
-
-
Save Centrinia/e65ecb30cdad85592fa7a90b81a20cdf to your computer and use it in GitHub Desktop.
sort
This file contains 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 <stdio.h> | |
#include <stdlib.h> | |
void sort(int *ap, int n); | |
#if 0 | |
void sort(int *ap, int n) | |
{ | |
int bubbled; | |
int i; | |
do { | |
bubbled = 0; | |
for (i = 1; i < n; i++) { | |
if (ap[i - 1] > ap[i]) { | |
int t = ap[i - 1]; | |
ap[i - 1] = ap[i]; | |
ap[i] = t; | |
bubbled = 1; | |
} | |
} | |
} while (bubbled); | |
} | |
#endif | |
int main() | |
{ | |
int n = 10; | |
int *ap; | |
int *bp; | |
int i; | |
ap = (int *) malloc(sizeof(int) * n); | |
bp = (int *) malloc(sizeof(int) * n); | |
for (;;) { | |
for (i = 0; i < n; i++) { | |
ap[i] = rand() % 10; | |
bp[i] = ap[i]; | |
} | |
sort(ap, n); | |
for (i = 0; i < n; i++) { | |
fprintf(stderr, "ap'[%d] = %d\n", i, bp[i]); | |
} | |
for (i = 0; i < n; i++) { | |
fprintf(stderr, "ap[%d] = %d\n", i, ap[i]); | |
} | |
for (i = 1; i < n; i++) { | |
if (ap[i - 1] > ap[i]) { | |
fprintf(stderr, "ap[%d] = %d > ap[%d] = %d\n", i - 1, | |
ap[i - 1], i, ap[i]); | |
free(bp); | |
free(ap); | |
return 0; | |
} | |
} | |
} | |
free(bp); | |
free(ap); | |
return 0; | |
} |
This file contains 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
.globl sort | |
sort: | |
push %ebp | |
mov %esp, %ebp | |
pushl %esi | |
pushl %ebx | |
1: | |
xorl %ebx, %ebx | |
movl 8(%ebp), %esi | |
movl 12(%ebp), %ecx | |
leal (%esi, %ecx, 4), %esi | |
negl %ecx | |
addl $1, %ecx | |
2: | |
movl -4(%esi,%ecx,4), %eax | |
movl (%esi,%ecx,4), %edx | |
cmpl %edx, %eax | |
jle 3f | |
movl %edx, -4(%esi,%ecx,4) | |
movl %eax, (%esi,%ecx,4) | |
orl $1, %ebx | |
3: | |
addl $1, %ecx | |
jnz 2b | |
testl %ebx, %ebx | |
jnz 1b | |
popl %ebx | |
popl %esi | |
popl %ebp | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment