提示用户输入整数并使用汇编语言添加到数组

时间:2015-10-08 01:27:15

标签: assembly x86 masm irvine32

对于我的x86汇编语言类,我们得到了一个赋值,其中我们需要接收12个整数并将它们存储在一个名为Autumn的数组中,然后显示它们。我的问题是为什么使用这段代码:

INCLUDE Irvine32.inc
.data
Prompt1  BYTE "Please input a value:", 0
Autumn   WORD 12 DUP(?) 

.code
main PROC
    mov esi, OFFSET Autumn
    mov ecx, LENGTHOF Autumn
    call PromptForIntegers

    mov edi, OFFSET Autumn
    mov ecx, LENGTHOF Autumn

L2:
    mov eax, [edi]
    add edi, TYPE Autumn
    call WriteInt               ;Displays value in EAX
    call Crlf                   ;Moves to next output line
    loop L2

exit
main ENDP

;-----------------------------------------------------
PromptForIntegers PROC USES ecx edx esi
;
; Prompts the user for an arbitrary number of integers 
; and inserts the integers into an array.
; Receives: ESI points to the array, ECX = array size
; Returns:  nothing
;-----------------------------------------------------

    mov edx,OFFSET prompt1  

L1: call    WriteString     ; display string
    call    ReadInt         ; read integer into EAX
    call    Crlf            ; go to next output line
    mov [esi],eax           ; store in array
    add esi,TYPE SWORD      ; next integer
    loop    L1

    ret
PromptForIntegers ENDP
END main

当我将整数1 - 12输入数组时。打印出来:

+131073
+196610
+262147
+327684
+393221
+458758
+524295
+589832
+655369
+720906
+786443
+12

唯一正确的输出是+12。其余的应该是数字1-11。

编辑:通过将mov eax, [edi]中的mov ax, [edi]更改为L2

,让它适用于正数

0 个答案:

没有答案