Sorting Integers in Assembly -


project create bubble sort algorithm in assembly sort given list of integers. i've got ascending down , output correct extent. seems when combining order of numbers gets mixed up, here's mean:

10 -20 5 12 30 -5 -22 55 52 0

number of integer = 10 ascend_or_descend = 1

0 5 10 12 30 52 55 -22 -20 -5

here's code including main method given test:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <io.h>  int sorter (int* list, int count, int opcode) { __asm { mov eax, 0          ; 0 out result mov ebx, opcode     ; move opcode ebx comparison  mov ecx, count; //outerloop counter cmp ecx, 1; //check length   jle return;    outerloop:    mov edi, list; //move in list        push ecx;      mov ecx, count;     dec ecx; //decrement inner loop innerloop:    mov eax, [edi]; //first value in list    cmp ebx, 1;     je  ascending;     cmp eax, [edi+4]; //next int 4 bits away    jb  swap;     jmp continue;  ascending:    cmp eax, [edi+4]; //again compare next int    ja  swap; //if no swap    jmp continue;  swap:    mov edx, [edi+4]; //move temp register    mov [edi+4], eax; //move stored value there    mov [edi], edx; //return temp value list continue:    add edi, 4; //increment move next int    loop innerloop;     pop ecx; //reset counter    loop outerloop;  return:;  }  }   int main(int argc, char** argv) { int numlist[1000], asc; file *fptr;  int = 0;  if (argc != 3) {     printf("usage: %s filename asc_des\n", argv[0]);     return 1; }  asc = atoi (argv[2]); asc = (asc == 1) ? 1 : 2;  printf("\n");  fptr = fopen((argv[1]), "rtc"); if (fptr == null)   printf( "file %s not opened\n",argv[1] ); else {   /* set pointer beginning of file: */   fseek( fptr, 0l, seek_set );    /* read data file: */   while ( fscanf(fptr, "%d", &numlist[i]) != eof )   {       printf( "%d ", numlist[i] );       i++;   }    printf( "\n\nnumber of integer = %d\n", );   printf( "ascend_or_descend = %d\n\n", asc );   fclose( fptr );   }    sorter( numlist, i, asc);    (int j = 0; j < i; j++)       printf( "%d ", numlist[j] );    printf("\n");    return 0;   } 

from intel's manual:

the terms “above” , “below” associated cf flag , refer relation between 2 unsigned integer values. terms “greater” , “less” associated sf , of flags , refer relation between 2 signed integer values

by using jb , ja you're treating result of comparison result of unsigned comparison, hence why signed numbers end @ end of sorted array (e.g. -22 when interpreted unsigned 32-bit value 4294967274).

you should using jl instead of jb, , jg instead of ja.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

python - How to remove the Xframe Options header in django? -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -