News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how to save array values

Started by ilian007, December 06, 2009, 07:12:25 PM

Previous topic - Next topic

ilian007

Hi I am trying to save array values for re-use.

for example if I have array:

MyArr DW 100 Dup (?)

I fill that array with digits 1,14,67,84,22,4,5,6, and etc ......

I want to save it use it (change all its values) and then call it back (with saved values) use it again and so on ..

any ideas ??

Tight_Coder_Ex

There is lots of different ways you could do this, but the one I would choose is to create a duplicate array on the stack and then copy all the values from data segment and then just continually re-copy.


        push ebp
        mov  ebp, esp
        sub   esp, 400

        mov  edi, esp
        mov  esi, MyArr
        mov  ecx, 100
       rep    movsd
       leave

As incomeplete as this code is, but you probably get the idea



FORTRANS

Hi,

   Well, probably the easiest thing to do is declare a second
array and copy to and from that.  But you could push the
array onto the stack, and pop them off later.  You could
try to reuse some memory that you used earlier and now
have no real use for as a scratch (temporary) array.  Or
you could play with the stack pointer to give you memory
in the stack segment to create a scratch area.  Or you can
try writing and reading to/from a file.

Regards,

Steve N.

P.S.  While writing this another post was made, so redundant
information.  SRN

dedndave

saving on the stack is ok if you have a lot of stack space
but, if you know ahead of time you are going to make a copy, define another array the same size in the uninitialized data segment
make sure DS and ES are both in DGROUP (@DATA), then...

        cld
        mov     si,offset ArrayOriginal
        mov     di,offset ArrayCopy
        mov     cx,sizeof ArrayOriginal
        rep     movsb

rep movsb is pretty fast

EDIT - the reason i mention the stack size is, i see a lot of the programs in here have 256 or 512 byte stacks - lol

ilian007

dedndave I have sent you my code.  I have trouble there because after fisrt call of bublesort1 the array is not same anymore for next procedures. can you take a look ?

ilian007

Quote from: dedndave on December 06, 2009, 07:35:24 PM
saving on the stack is ok if you have a lot of stack space


space and eficiency is not a problem :) just to make it work

ilian007

here is my code. If I call each of my procedures bubblesort1 bubblesort2 and bubblesort3 separately everything is ok. I want to call them one after another.... The problem is each procedure cause change of array unsorted ....

dedndave

here is what i suggest
while you are building the first array, go ahead and build the second one also
that way, no need to copy it - you have the value in AX and the index in SI, so........

START:
;;;;;;;;;;;;;;;;;; MOV BX,OFFSET UNSORTED ;set BX as base for unsorted
   
RAND_NUM:
   MOV  CX,100   ;get 100 random numbers seed 5555h
        MOV sI,0
CREATE_ARR:
   
   push CX
        MOV  AX,UPPER    ;MOVE UPPER IN AX REGISTER 9999
        PUSH AX         ; PUSH AX VALUE INTO THE STACK
   MOV  AX,LOWER    ;MOVE LOWER IN AX REGISTER 0
   PUSH AX      ;PUSH AX VALUE INTO THE STACK
   
   CALL RAND   ;CALL PROC RANDOM
;;;;;;;;;;;;;;;;; MOV [BX+sI],AX   ;add rand numbers to array unsorted
        MOV  unsorted[SI],AX        ;first array
        MOV  unsorted2[SI],AX       ;second array
   ADD SI,2
   
   pop cx
   loop create_arr  ;loop 100 times to get 100 rand numb

but you need to fix something there, too - lol
you are building arrays with 100 dwords
and then, sorting bytes
i think you really want arrays with 100 bytes (or 50 dwords)

MichaelW

Assuming you have a way to reseed the random number generator, why not just reseed it and initialize the array before each pass, no copies required.
eschew obfuscation

ilian007

Michael I tried what you said but I had problems with the print then... my program is mess if I touch something then I am messing something else. the DednDave suggestion works, but now printing is weird ... dont know why trying to fix it. Printing only for one of the arrays is weird(bub3) let me post here what i've got

ilian007

cant get it ... if I use unsorted or unsorted2 for bubblesort3 it prints o,k but with unsorted3 it doesnt :(( unsorted unsorted2 unsorted3 should be same ...

dedndave

i know why it does that - lol
the PUTDEC and PUTDEC$ routines use the BH register to format output
if BH is 0, it just displays the number
if BH is not 0, it right-justifies the number in a 6-character field
so, just before your call to PUTDEC/PUTDEC$, set BH to what you want (0 in this case)
if you are using the BX register for something else, you will want to PUSH and POP BX
i don't think you are

the number of swaps and compares looks a little high - let me look at it

ilian007

yeah bh,0 works :) I looked at if I have bh,1 somewhere before asking you but didnt see it. Why with unsorted and unsorted1 it works without bh,0 but with unsorted3 doesnt ... 

however it works with mov bh,0 :))

thanks to everybody :P

dedndave

i am guessing your third sort routine uses BX or BH for something
yup - lol - you use it to address the array

i wouldn't call it done, yet - lol
it needs some clean-up
i am looking at it

dedndave

ok - i have not changed any of your code in this file
instead, i merely wrote comments about the changes i would make
read through it and see what you think