如何打印数组的内容?

时间:2017-09-22 18:38:54

标签: arrays assembly masm

我尝试使用汇编语言打印数组的内容,如下所示。

我可以编译代码,但我无法运行它 我该如何修复代码以打印数组的内容?

TITLE arrayFill_example (arrayFill_ex.asm)

INCLUDE Irvine32.inc

.data
count = 5
array       DWORD        count DUP(?)
arraySize = ($ - array) / 4

.code
; saves the general-purpose registers, retrieves the parameters, and fills the array
ArrayFill   PROC
            push        ebp
            mov         ebp,esp
            pushad                              ; save registers
            mov         esi,[ebp+12]            ; offset of array
            mov         ecx,[ebp+8]             ; array length
            cmp         ecx,0                   ; ECX == 0?
            je          L2                      ; yes: skip over loop

            L1:
            mov         eax,10000h              ; get random 0-FFFFh
            call        RandomRange             ; from the link library
            mov         [esi],ax                ; insert value in array
            add         esi,TYPE WORD           ; move to next element
            loop L1

            L2: popad                           ; restore registers
            pop         ebp
            ret         8                       ; clean up the stack
ArrayFill ENDP

main        PROC
            push        OFFSET array            ; passed by reference
            push        count                   ; passed by value
            call        ArrayFill

            ; for showing array contents
            mov         eax, 0
            mov         esi, array
            mov         ecx, arraySize

            L1:
            mov         eax, array[esi * TYPE array]
            call        WriteInt
            call        Crlf
            add         esi, 4
            loop L1

exit
main        ENDP
END main

具体来说,这部分对我不起作用......

; for showing array contents
        mov         eax, 0
        mov         esi, array
        mov         ecx, arraySize

        L1:
        mov         eax, array[esi * TYPE array]
        call        WriteInt
        call        Crlf
        add         esi, 4
        loop L1

1 个答案:

答案 0 :(得分:1)

问题1

array       DWORD        count DUP(?)

使用此定义,数组包含 dwords 。但是你的程序只使用单词填充数组:

mov         [esi],ax                ; insert value in array
add         esi,TYPE WORD           ; move to next element

更好的写作:

mov  [esi], eax              ; insert value in array
add  esi, 4                  ; move to next element

问题2

mov         esi, array
...
mov         eax, array[esi * TYPE array]

这些行冗余地指代数组。这会产生错误!接下来是一个很好的解决方案:

mov  esi, OFFSET array
mov  ecx, arraySize

L1:
mov  eax, [esi]
call WriteInt
call Crlf
add  esi, 4
loop L1