Click to See Complete Forum and Search --> : Help !!


kevinwang128
March 28th, 2009, 12:24 PM
i am doing a major assembly assignment, one of the question is asking to convert shellqort to assembly, if anyone who is good with assembly and willing to give me a hand much appreciated !!

this is the c code:

#include <stdlib.h>
#include <string.h>

int largerThanForInt(const void *p, const void *q)
{
if(*((int*)p) > *((int*)q))
return 1;
else
return 0;
}






void shellsort(void * base, size_t num, size_t size,
int (*comparator) (const void *, const void *))
{
size_t gap, i;
int j;
char *temp, *pj, *pjgap;
temp = (char*)malloc(size);

for (gap = num/2; gap > 0; gap /=2)
for (i = gap; i < num; i++)
for (j = i - gap;
j >= 0 && comparator((char*)base+j*size, (char*)base+(j+gap)*size);
j -= gap)



{

pj = (char*)base + j * size;
pjgap = (char*)base + (j + gap) * size;

memmove(temp, pj, size);
memmove(pj, pjgap, size);
memmove(pjgap, temp, size);
}

free(temp);
}



A few comment could also help !!

thanks guys

rxbagain
March 30th, 2009, 06:05 PM
I would assume you are using DOS assembly. I made the code for shellsort but I'll let you do the code for largerThanForInt.

The code expects that DS is already set to the segment of the array and a short pointer is passed to the function. the comparison function is also a near pointer. Also, the function uses C calling convention so you have to clean the parameters after your call to shellsort


shellsort PROC base:WORD, num:WORD, nsize:WORD, comparator:WORD
LOCAL gap:WORD, i:WORD, j:WORD, pj:WORD, pjgap:WORD

push bp
mov bp, sp
sub sp, (2 * 5) ; allocate local variable storage
push si
push di ; save si, di, bp and stack pointer

; for (gap = num/2; gap > 0; gap /=2)
mov ax, [num]
shr ax, 1
mov [gap], ax ; gap = num / 2
for1:
mov ax, [gap]
jz for1_end ; if (gap == 0) break

; for (i = gap; i < num; i++)
mov [i], ax ; i = gap
for2:
mov ax, [i]
cmp ax, [num] ; if (i >= num) break
jge for2_end

; for (j = i - gap; j >= 0 && comparator((char*)base+j*size, (char*)base+(j+gap)*size); j -= gap)
sub ax, [gap]
mov [j], ax ; j = i - gap
for3:
mov ax, [j] ; if (j < 0) break
cmp ax, 0
jl for3_end

mul [nsize]
add ax, [base]
mov [pj], ax ; pj = (char*)base + j * size;

mov ax, [j]
add ax, [gap]
mul [nsize]
add ax, [base]
mov [pjgap], ax ; pjgap = (char*)base + (j + gap) * size;

; ? comparator((char*)base+j*size, (char*)base+(j+gap)*size);
push ax
push [pj]
call comparator
add sp, 4
or ax, ax ; if comparator result is 0 then break
jz for3_end

; when loop reaches this point, we switch the contents of the 2 elements
; instead of using a temporary storage, we just switch the contents byte by byte
mov si, [pj] ; use si as pj
mov di, [pjgap] ; use si as pjgap
mov cx, [nsize] ; use the nsize parameter as size
swapit:
mov al, [si] ; al = *di
xchg al, [di] ; swap (al, *si)
mov [si], al ; *si = al
inc si ; increment pointer si
inc di ; increment pointer di
loop swapit ; swap all bytes

; j -= gap in "for (j = i - gap; j >= 0 && comparator((char*)base+j*size, (char*)base+(j+gap)*size); j -= gap)"
mov ax, [j]
sub ax, [gap]
mov [j], ax ; j -= gap
jmp for3
for3_end:
; i++ in "for (i = gap; i < num; i++)"
inc [i] ; i++
jmp for2
for2_end:
; gap /= 2 in "for (gap = num/2; gap > 0; gap /=2)"
mov ax, [gap]
shr ax, 1
mov [gap], ax ; gap /= 2
jmp for1
for1_end:
pop di ; restore si, di, bp and the stack pointer
pop si
mov sp, bp
pop bp
ret
shellsort ENDP

Hope it will help you :)