assembly - Fill an array with random values -


i'm in assembly language class , doing exercise need pass parameters on stack in order generate n random numbers in specific range.

the problems i'm having:
1. random numbers not in range specifying.
2. when return array , attempt sort , display again, array populated different values.

i'm thinking these problems somehow related way i'm passing parameters.

title program 4         (project04.asm)  include irvine32.inc  .data intro_1 byte    "composite numbers          programmed ",0 prompt_1 byte   "this program generates random numbers in range [100 .. 999],",0 prompt_2 byte   "displays original list, sorts list, , calculates ",0 prompt_3 byte   "median value. finally, displays list sorted in descending order.",0 prompt_4 byte   "how many numbers should 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       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 on loop             mov edx, hi              sub edx, lo          l1:             mov eax, edx             call    randomrange ; link library             add eax, lo             mov [esi], eax             add esi, 4             loop    l1          l2:              popad           ; restore registers             pop ebp             ret 8           ; clean stack      fillarray1 endp  ;sort list      sortlist proc         push    ebp         mov ebp, esp         mov ecx, [ebp + 8]         dec ecx         ; decrement count 1          l1:             push ecx            ; save outer loop count             mov esi, [ebp + 12]     ; point first value          l2:              mov eax,[esi]       ; array value             cmp [esi+4],eax ; compare pair of values             jge l3          ; if [esi] <= [edi], don't exch             xchg eax,[esi+4]    ; exchange 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 

i think return value may off in fillarray procedure?

the displaylist procedure:

;display list      displaylist proc         push    ebp         mov ebp, esp         mov ecx, [ebp + 8]         jecxz   farewell         mov esi, [ebp + 12]         ;set counter linebreak      print:         mov eax, [ebx]         call writedec         dec esi         jnz spaces      linebreak:         call crlf         mov esi, 10         jmp next      spaces:         cmp esi, 0          ;if there has been ten elements, go next line         je  linebreak         mov edx, offset space3         call writestring      next:         add ebx, 4         loop print       pop ebp     ret 8      displaylist endp 

the getdata procedure accepts reference param:

;get data      getdata proc         push ebp         mov ebp, esp         pushad         mov esi, [ebp + 8] ;move address of usernum      getuserdata:         mov edx, offset prompt_4         call    writestring         call    readint         mov     usernum, eax         mov     esi, usernum    ;move usernum address         ;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 

when made change, after enter number of random numbers printed, program crashes.

fillarray , sortlist working properly, have tested them.

getdata has bug (big 1 indeed) in error1 path, after printing error message jump getuserdata again creates stack frame.
move creation above getuserdata label, below getdata.

introduction should work (not tested tough).

the main procedure has issues:

call    introduction            ;ok  push    offset usernum call    getdata                 ;getdata takes no params                                 ;either update getdata (don't forget ret 04h)                                 ;or remove push   ;call   randomize  push offset array push usernum call fillarray1                 ;this should work  push offset array push usernum call displaylist                ;don't know, no code displaylist  call crlf call crlf push offset array push usernum call sortlist                   ;this should work  ;push offset array              ;<-- commented push usernum  call displaylist                ;!! not passing array display                                 ;that's why see different values 

it seems forget update main after refactoring others functions.


follow up

the displaylist procedure seems wrong because:

  1. you never initialize ebx
  2. esi holds array pointer seems use counter
  3. farewell not defined anywhere

i think new displaylist may work (please, please, please, note haven't compiled it, don't have nasm, nor planning installing it)

 ;array  ;number of elements  ;elements per line     displaylist proc         push    ebp         mov ebp, esp          push esi         push ecx         push edx          mov esi, [ebp + 16]      ;array         mov ecx, [ebp + 12]      ;items in array      preprint:         mov edx, [ebp + 8]       ;counter linebreaks      print:         jecxz end          lodsd         call writedec            ;print items          mov edx, offset space3         call writestring         ;print space          dec ecx         dec edx         jnz print          call crlf     jmp preprint      end:         pop edx         pop ecx         pop esi          pop ebp         ret 12      displaylist endp 

note new displaylist takes 3 arguments:

push offset array push usernum push 10              ;items per line call displaylist 

if want hardcode items per line , remove past parameters, edit function, remember update ret 12 , [ebp + xx] references!


in new getdata forget popad before pop ebp , replace ret ret 04h.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -