简体   繁体   中英

Copying an array to another array as reversed and display in Assembly

I am trying to copy an array to another array, but the order of elements should be reversed and displayed. My program file compiles but does not run and causes a Windows stop error each time (Windows dialog box).
I'm not sure what I'm doing wrong. Please advise.

INCLUDE Irvine32.inc

.data
sarray DWORD 500h, 400h, 300h, 200h, 100h
darray DWORD 5 DUP(?)

.code
main PROC

    mov eax, 0
    mov esi, OFFSET sarray ;move first element address to esi
    mov edi, OFFSET darray + SIZEOF darray - TYPE darray  ;move last element address to edi
    mov ecx, LENGTHOF sarray  ;sets the counter in the reverseLoop

  reverseLoop:
    mov eax,sarray[esi]     ;move 1st sarray element to eax
    mov darray[edi],eax     ;move 1st sarray element as last element on darray
    call WriteInt
    call Crlf
    add esi, TYPE sarray
    sub edi, TYPE darray
    call WriteInt
    loop reverseLoop

    exit
main ENDP

END main

Edit : My problem was that esi and edi had the address already added ( OFFSET sarray ) but in the loop I added the start again ( mov eax, sarray[esi] ).

After the fix, I had to add another loop to print the second darray (new one)

My problem was that esi and edi with the address already added OFFSET sarray , but in the loop I added the start again ( mov eax, sarray[esi] ).

After fixing this, I also had to add another loop to print the second array (the reversed one).

Here is the working code:

INCLUDE Irvine32.inc

.data
sarray DWORD 500h, 400h, 300h, 200h, 100h
darray DWORD 5 DUP(?)

.code
main PROC

    mov esi, OFFSET sarray                                ;move first element address to esi
    mov edi, OFFSET darray + SIZEOF darray - TYPE darray  ;move last element address to edi
    mov ecx, LENGTHOF sarray                              ;sets the counter in the reverseLoop

reverseLoop:
    mov eax,[esi]       ;move 1st sarray element to eax
    mov [edi],eax       ;move 1st sarray element as last element to the darray  
    add esi, TYPE sarray
    sub edi, TYPE darray
    loop reverseLoop
    call clrscr

    mov esi, OFFSET darray
    mov ecx, LENGTHOF darray
    mov ebx, 0

printerLoop:
    mov eax,[esi]
    call WriteHex           
    call Crlf
    add esi, TYPE darray
    loop printerLoop

    call Crlf

    exit
main ENDP

END main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM