I have written a simple code using inline assembly to compare the individual bytes of two lists (X and Y) and to store the higher byte of each list in a third list (LARGER). The code is as follows (implemented on VC++ 2008 Express edition):
I believe that the code is correct. When I execute this program without any dynamic memory allocation i.e. I initalize the contents of the lists before compiling (and don't use calloc and scanf), the code works perfectly. But in the current form, the output is simply the contents of list Y.
For example, if each list is 4 bytes long i.e. N=4 and the contents of the lists are
X: 'z', 'x', 'c', 'v'
Y:'b','k','l',' p'
then after execution the list LARGER should contain 'z','x','l','v' .
However, with the current code the list LARGER contains 'b','k','l','p' i.e. the contents of list Y.
Any advice?
Code:
#include <stdio.h>
#include <malloc.h>
int main()
{
char *X,*Y,*LARGER;
int i;
int N;
printf("Please enter the number of bytes in each list: ");
scanf_s(" %d",&N,1);
X = (char*)calloc(N,1);
Y = (char*)calloc(N,1);
LARGER = (char*)calloc(N,1);
printf("Enter the bytes for list X\n");
for(i=0;i<N;i++)
scanf_s(" %c",&X[i],1);
printf("Enter the bytes for list Y\n");
for(i=0;i<N;i++)
scanf_s(" %c",&Y[i],1);
_asm {
lea ebx, X;
lea edx, Y;
mov esi, N;
lea ecx, LARGER;
find: mov al, byte ptr[ebx + esi - 1];
cmp al, byte ptr[edx + esi - 1];
jbe secnd;
mov byte ptr[ecx + esi - 1], al;
jmp done;
secnd: mov al, byte ptr[edx + esi - 1];
mov byte ptr[ecx + esi - 1], al;
done: dec esi;
jne find;
}
printf("The larger numbers are\n");
for(i=0;i<N;i++)
{
printf("%c\n",LARGER[i]);
}
system("PAUSE");
return 0;
}
For example, if each list is 4 bytes long i.e. N=4 and the contents of the lists are
X: 'z', 'x', 'c', 'v'
Y:'b','k','l',' p'
then after execution the list LARGER should contain 'z','x','l','v' .
However, with the current code the list LARGER contains 'b','k','l','p' i.e. the contents of list Y.
Any advice?
Comment