Hi all,

I am at a lost and do not know where to start from. Here is what I have to do

"Given the data below, you are to design an IA-32 assembly language program to sort array X by the number of binary 0's in each member and save the sorted array in Y. You can use any sorting method and sort the array in ascending or descending order.

.data
X SWORD -2000, 1000, 3000, -4000
Y SWORD 4 DUP (?)"

If anybody could give me some sort of direction to help me get started then that would help greatly.

Thank you very much.

This is the function which calculates number of 0-bits in 16-bit number:

``````GetNumberOfZeros16:
mov     eax,0
mov     ecx,16
mCountZeros:
test    dx,1
jnz     mSkip
inc     eax
mSkip:
shr     dx,1
loop    mCountZeros

ret``````

. Let me know if you need more help.

To use this function, move the number to dx register:

``````mov     dx,1011101110111011b
call    GetNumberOfZeros16``````

. This example will return 4 (in eax).

All 5 Replies

This is the function which calculates number of 0-bits in 16-bit number:

``````GetNumberOfZeros16:
mov     eax,0
mov     ecx,16
mCountZeros:
test    dx,1
jnz     mSkip
inc     eax
mSkip:
shr     dx,1
loop    mCountZeros

ret``````

. Let me know if you need more help.

To use this function, move the number to dx register:

``````mov     dx,1011101110111011b
call    GetNumberOfZeros16``````

. This example will return 4 (in eax).

So my code should look something like this?

``````.data
X SWORD -2000, 1000, 3000, -4000
Y SWORD 4 dup(?)

.code
start:

mov eax, offset X
mov ecx, offset Y

GetNumber of zeros16:
mov eax,0
mov ecx,16
mCountZeros:
test dx,1
jnz mSkip
inc eac
mSkip:
shr dx,1
loop mCountZeros
ret
mov dx, X
call GetNumberOfZeros16

invoke exitProcess,0
end start``````

Your code should look something like this :):

``````include 'win32ax.inc'

.data
X       dw -2000,1000,3000,-4000
qbX     dw \$-X

.code
start:
mov     esi,X
mov     ecx,0
mov     cx,[qbX]
shr     ecx,1

mBubbleSort_Main:
dec     ecx
cmp     ecx,0
jle     mBubbleSort_Ex
push    ecx
push    esi

mov     bl,0
mBubbleSort_One:
mov     dx,word [esi]
push    ecx
call    GetNumberOfZeros16
pop     ecx
push    eax
mov     dx,word [esi+2]
push    ecx
call    GetNumberOfZeros16
pop     ecx
pop     edx

cmp     edx,eax
jle     mBubbleSort_SkipR
mov     bl,1
mov     ax,word [esi]
mov     dx,word [esi+2]
mov     [esi],dx
mov     [esi+2],ax
mBubbleSort_SkipR:
loop    mBubbleSort_One

pop     esi
pop     ecx
cmp     bl,0
je      mBubbleSort_Ex
jmp     mBubbleSort_Main
mBubbleSort_Ex:

invoke  ExitProcess,0

GetNumberOfZeros16:
mov     eax,0
mov     ecx,16
mCountZeros:
test    dx,1
jnz     mSkip
inc     eax
mSkip:
shr     dx,1
loop    mCountZeros

ret

.end start``````

.

Ok. Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.