jeudi 23 juillet 2015

Fill an Array with Random Values in a Range Assembly

I'm in an Assembly class and we are doing an exercise where we need to pass parameters on the stack in order to generate n random numbers in a specific range.

The problems that I'm having: 1. My random numbers are not in the range that I am specifying. 2. When I return this array and attempt to sort it and display it again, the array is populated with different values.

I'm thinking these problems are somehow related to the way I'm passing my parameters.

TITLE Program 4         (Project04.asm)

; Program Description:
; Author:
; Date Created:
; Last Modification Date:

INCLUDE Irvine32.inc

.data
intro_1 BYTE    "Composite Numbers          Programmed by ",0
prompt_1 BYTE   "This program generates random numbers in the range [100 .. 999],",0
prompt_2 BYTE   "displays the original list, sorts the list, and calculates the ",0
prompt_3 BYTE   "median value. Finally, it displays the list sorted in descending order.",0
prompt_4 BYTE   "How many numbers should be generated? [10 .. 200]: ",0
space3  BYTE    "   ",0
errorMessage BYTE   "Invalid input ",0
userNum DWORD   ?
min     =   10
max     =   200
lo      =   100
hi      =   999
array   DWORD   max DUP(?)

.code
main PROC
call    Introduction
push    OFFSET userNum
call    getData
;call   Randomize
push OFFSET array
push userNum
call fillArray1
push OFFSET array
push userNum
call displayList
call crlf
call crlf
push OFFSET array
push userNum
call sortList
;push OFFSET array
push userNum 
call displayList
exit
main ENDP




;Introduce the program

    Introduction PROC
        mov edx, OFFSET intro_1
        call    WriteString
        call    crlf
        mov edx, OFFSET prompt_1
        call    WriteString
        call    crlf
        mov edx, OFFSET prompt_2
        call    WriteString
        call    crlf
        mov edx, OFFSET prompt_3
        call    WriteString
        call    crlf    
        call    crlf
        ret
    Introduction ENDP



;Get Data

    getData PROC

    getUserData:
        push ebp
        mov ebp, esp
        mov edx, OFFSET prompt_4
        call    WriteString
        call    ReadInt
        mov     userNum, eax
        ;call   crlf

    ;validate
        cmp eax, max
        ja  error1
        cmp eax, min
        jb  error1
        pop ebp
        ret
        error1:
            mov edx, OFFSET errorMessage
            call    WriteString
            call    crlf
            jmp getUserData

    getData ENDP

;Fill array

    fillArray1 PROC
        fillArray:
            push    ebp
            mov ebp,esp
            pushad          ; save registers
            mov esi,[ebp+12]    ; offset of array
            mov ecx,[ebp+8] ; array size
            cmp ecx,0       ; ECX == 0?
            je  L2          ; yes: skip over loop
            mov edx, hi 
            sub edx, lo

        L1:

            mov eax, edx
            call    RandomRange ; from the link library
            add eax, lo
            mov [esi], eax
            add esi, 4
            loop    L1

        L2: 
            popad           ; restore registers
            pop ebp
            ret 8           ; clean up the stack

    fillArray1 ENDP

;Sort List

    sortList PROC
        push    ebp
        mov ebp, esp
        mov ecx, [ebp + 8]
        dec ecx         ; decrement count by 1

        L1:
            push ecx            ; save outer loop count
            mov esi, [ebp + 12]     ; point to first value

        L2: 
            mov eax,[esi]       ; get array value
            cmp [esi+4],eax ; compare a pair of values
            jge L3          ; if [esi] <= [edi], don't exch
            xchg eax,[esi+4]    ; exchange the pair
            mov [esi],eax

        L3: 
            add esi,4       ; move both pointers forward
            loop L2     ; inner loop

            pop ecx     ; retrieve outer loop count
            loop L1     ; else repeat outer loop

        L4 :
            pop ebp
            ret 8  
    sortList ENDP

;Display Median

    displayMedian PROC

    displayMedian ENDP

;Display list

I think my return value may be off in my fillArray procedure? Any help is much appreciated!




Aucun commentaire:

Enregistrer un commentaire