The MASM Forum

Miscellaneous => Irvine Book Questions. => Topic started by: xcbart on April 27, 2019, 02:23:41 PM

Title: How to sort counted(occurrence) numbers in increasing order?
Post by: xcbart on April 27, 2019, 02:23:41 PM
I have been given this question to create 50 random numbers from 1 to 15 and count how many times each occurred. I need to print them out in increasing order, but I couldn't. So far, I can only print out the numbers randomly with an increasing counter next to them. Any idea how I can print them out in order?

Thank you


INCLUDE Irvine32.inc
.data
blank byte " ",0

count byte 16 dup (0)
    dates dword 50 dup (?)
.code
main PROC
call randomize
mov edx, offset blank
    mov edi,0   ;count index
mov ecx,50
printnum:

    mov eax,15
    call randomRange   
    add eax,1
    call writedec
        mov dates[edi],eax;

        add edi,4;
    mov esi, eax
    inc count[esi]
        call writestring
        movzx eax,count[esi]
        call writedec
    call writestring
        mov eax,ecx

loop printnum

call crlf

exit
main ENDP
end main
Title: Re: How to sort counted(occurrence) numbers in increasing order?
Post by: aw27 on April 27, 2019, 03:32:53 PM
Google for bubble sort numbers MASM
Title: Re: How to sort counted(occurrence) numbers in increasing order?
Post by: jimg on April 28, 2019, 04:42:58 AM
How would you do it with a pencil and the list of numbers on a piece of paper?
Do it that way.
The point of the exercise is to figure out which instructions to use to do the same thing manually.
Title: Re: How to sort counted(occurrence) numbers in increasing order?
Post by: Vortex on April 28, 2019, 06:38:24 AM
Hi xcbart,

You can use the qsort function supplied by the MS VC run-time library. As an exercise, try to study the rand() function, the pseudorandom number generator :

https://docs.microsoft.com/en-us/previous-versions/398ax69y(v%3Dvs.140)

include qsort.inc

CompareProc PROTO C :DWORD,:DWORD

NUMB_OF_ELEMENTS equ 10

.data

numbers     dd 29,12,32,65,58,7,12,11,11,97
format1     db '%d',13,10,0

.code

start:

    invoke  crt_qsort,ADDR numbers,\
            NUMB_OF_ELEMENTS,\
            SIZEOF DWORD,ADDR CompareProc

    call    PrintArray

    invoke  ExitProcess,0
   

CompareProc PROC C arg1:DWORD,arg2:DWORD

    mov     ecx,arg1
    mov     edx,arg2
    mov     eax,DWORD PTR [ecx]
    sub     eax,DWORD PTR [edx]
    ret

CompareProc ENDP


PrintArray  PROC uses esi ebx

    mov     ebx,NUMB_OF_ELEMENTS
    mov     esi,OFFSET numbers
@@:
    invoke  crt_printf,ADDR format1,\
            DWORD PTR [esi]

    add     esi,4
    dec     ebx
    jnz     @b
    ret

PrintArray ENDP


END start