News:

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

Reversing an array

Started by ammadeus, August 26, 2009, 06:32:57 AM

Previous topic - Next topic

ammadeus

Hi guys,

I'm a total assembly noob way over my head with a small school project deadline lurking.
I want to reverse an array and subsequently print the reversed array.



array1 db 225 DUP("00 ")
calc_array_size:



mov AX, rows

imul cols ; (DX AX) = AX * amount of columns

mov arraysize, AX

mov AX, arraysize



init_gameboard:
mov AX, 3
imul arraysize
mov last, AX
mov AX, SEG array1 ;Put address of data segment into AX

mov DS, AX ;then into DS (we can't do this directly)

lea DI, array1 ;put address of array1 into DI

add DI, last ;move to position 50

mov AL, "$" ;put $ into AL

mov [DI], AL ;put the $ into DI at position 50




display:
;We use a loop to display the gameboard.
;First we reverse the corresponding array, because we must

make_reverse:

array2 db 225 DUP ("00 ") ;
mov CX, last

:
revpos DW ?

mov AX, last ;First we must calculate the index of the reverse array
;that is going to be filled in this step.
sub AX, CX        ;This is obtained by subtracting the last index and CX.
mov revpos, AX ;Store result in revpos.
mov AX, SEG array1 ;Now we load the gameboard in AX.

mov DS, AX ;Then into DS (we can't do this directly)

lea DI, array1 ;Put address of gameboard into DI.

add DI, CX ;Move to appropriate position. 
mov AL, [DI] ;Get value from position (could be a problem! DI + 3)
mov AX, SEG array2 ;Now load reverse gameboard in AX.
mov DS, AX ;Then into DS.
lea DI, array2 ;Put address of reverse gameboard into DI.
add DI, revpos ;Move to position in reverse gameboard.
mov [DI], AL ;Put AL in to the appriopriate position of the reverse array
dec CX ;Decrement CX 3 times, which is the size of a word.
dec CX
dec CX
jnz go

zero:
mov AX, SEG array2 ;Put address of data segment into AX

mov DS, AX ;Then into DS (we can't do this directly)

lea DI, array2 ;Put address of array2 into DI

add DI, last ;Move to last position.

mov AL, "$" ;put $ into AL

mov [DI], AL ;put the $ into DI at last position


print_gameboard:

mov AX, SEG array2 ;Put segment address of array2 into AX

mov DS, AX ;Then into DS

mov DX, OFFSET array2 ;text at DS:DX

mov AH, 09h ;print text to screen

int 21h ;Function 9h of Interrupt 21h

mov AX, 4c00h ;Function 4c00h of Int 21h


int 21h ;Return to OS



If I change array2 to array1 in print_array the display is fine.
I basically want to get the same output as when doing so.
I'm aware that the code is ugly, often unnecessary stuff is done. In that case any pointers are welcome too!

thanks