将数组复制到另一个数组,并在Assembly中显示

时间:2017-06-20 21:52:32

标签: arrays assembly x86 irvine32

我正在尝试将数组复制到另一个数组,但是元素的顺序应该反转并显示。我的程序文件编译但不运行并且每次都会导致Windows停止错误(Windows对话框) 我不确定我做错了什么。请指教。

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

修改:我的问题是esiedi已添加地址(OFFSET sarray),但在循环中我再次添加了开头({ {1}})。

修复后,我不得不添加另一个循环来打印第二个 darray (新的)

1 个答案:

答案 0 :(得分:0)

我的问题是esiedi地址已添加OFFSET sarray,但在循环中我再次添加了开头(mov eax, sarray[esi])。

修好后,我还必须添加另一个循环来打印第二个数组(反向的)。

以下是工作代码:

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