The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: ilian007 on December 01, 2009, 11:50:05 PM

Title: Is there any easy way (simple) way to flip array
Post by: ilian007 on December 01, 2009, 11:50:05 PM
Is there any easy way (simple) way to flip array,

for example Arr1 4,13,2,8,5 to become Arr1 5,8,2,13,4

just simple command not to use whole procedure to exchange numbers .. just to change the start of the array or something .. ?


thanks
Title: Re: Is there any easy way (simple) way to flip array
Post by: dedndave on December 02, 2009, 12:49:00 AM
well - not really
i mean - there is no special instruction intended for that
you just have to write a little loop that picks up a byte at each end, swaps them, then moves toward the center for the next one
something like this...

        mov     di,offset Arr1
        mov     si,offset Arr1+sizeof Arr1-1

swap_loop:
        mov     al,[si]
        mov     ah,[di]
        mov     [di],al
        mov     [si],ah
        inc     di
        dec     si
        cmp     si,di
        ja      swap_loop

well, that's one way to do it - lol
Title: Re: Is there any easy way (simple) way to flip array
Post by: drizz on December 02, 2009, 12:53:04 AM
start reading from the end?
Title: Re: Is there any easy way (simple) way to flip array
Post by: dedndave on December 02, 2009, 12:54:57 AM
lol drizz - you are starting to sound like JJ
i am guessing it is a numeric string that he wants to display - lol
i would try building it the opposite direction to begin with   :bg
listen to me - now i am starting to sound like JJ - lol

http://www.masm32.com/board/index.php?topic=12816.msg98960#msg98960
Title: Re: Is there any easy way (simple) way to flip array
Post by: raymond on December 02, 2009, 03:38:23 AM
There's one precaution you should take with Dave's suggested procedure: either
i) make sure the ES segment register is identical to the DS segment register (DI defaults to the ES segment unless specified otherwise)
or
ii) include DS as a segment override when using the DI register, such as
mov ah,ds:[di]
mov ds:[di],al

Title: Re: Is there any easy way (simple) way to flip array
Post by: sinsi on December 02, 2009, 04:03:52 AM
QuoteDI defaults to the ES segment unless specified otherwise
Only on string ops, surely? The only one to watch for is any access to [bp] which defaults to SS.
Title: Re: Is there any easy way (simple) way to flip array
Post by: dedndave on December 02, 2009, 07:55:30 AM
DI references ES only for string instructions - for other instructions, it references DS
what sinsi said - i just wanted to clarify for MGG
Title: Re: Is there any easy way (simple) way to flip array
Post by: ilian007 on December 02, 2009, 09:50:21 PM
oh Ok I guess I will use another way to solve the problem that one seems too dificult. DednDave I am trying to code the 3rd method for Bubble sort and thinking of ways to make it possible.