簡體   English   中英

如何一一顯示數組編號(Irvine 32 library Visual Studio 2019 ASM)

[英]How to display array numbers one by one (Irvine 32 library Visual Studio 2019 ASM)

我需要將每個手動輸入數組一個一個地寫入屏幕,但是當我嘗試寫入第一個數組 (0) 時,屏幕上的輸出為 5。 我還需要將第五個數組的值交換到第二個數組,我相信我已經正確完成了。 當程序運行時,它告訴我輸入我的數字,所以我輸入了 1、2、3、4、5、6,但我的輸出是 5? 它應該是 1。這是我的代碼:


ExitProcess proto,dwExitCode:dword

.data       ;// write your data in this section

myArray WORD 6 DUP(? )
prompt BYTE "Enter a number", 0dh, 0ah, 0
prompt1 BYTE "The number at position 0 is ", 0dh, 0ah, 0
prompt2 BYTE "The number at position 1 is ", 0dh, 0ah, 0
prompt3 BYTE "The number at position 2 is ", 0dh, 0ah, 0
prompt4 BYTE "The number at position 3 is ", 0dh, 0ah, 0
prompt5 BYTE "The number at position 4 is ", 0dh, 0ah, 0
prompt6 BYTE "The number at position 5 is ", 0dh, 0ah, 0



.code       ;// write your program here
main proc




    mov edx, OFFSET prompt
    call WriteString
    call ReadInt
    mov myArray[0], ax

    call WriteString
    call ReadInt
    mov myArray[1 * SIZEOF WORD], ax

    call WriteString
    call ReadInt
    mov myArray[2 * SIZEOF WORD], ax

    call WriteString
    call ReadInt
    mov myArray[3 * SIZEOF WORD], ax

    call WriteString
    call ReadInt
    mov myArray[4 * SIZEOF WORD], ax

    call WriteString
    call ReadInt
    mov myArray[5 * SIZEOF WORD], ax

    mov ax, myArray[4 * SIZEOF WORD]
    xchg myArray[1], ax
    movzx eax, myArray[1]
    call WriteDec
    call Crlf


    mov edx, OFFSET prompt1
    call WriteString

    mov myArray [0 * SIZEOF WORD], ax
    call WriteDec
    call Crlf 

當我使用mov myArray [0 * SIZEOF WORD], ax我得到“位置 0 處的數字是 5”。 但它應該是 1。當我使用mov ax, myArray[0]而不是mov myArray [0 * SIZEOF WORD], ax我得到“位置 0 處的數字是 1281”。 我很困惑,為什么將第一個數組寫入屏幕的不同方式會有不同的輸出? 請幫我將第一個數組正確寫入屏幕。

您為其輸入了 6 個值的數組將保存這 12 個字節(以小端格式存儲的 6 個字):

1,0,2,0,3,0,4,0,5,0,6,0  array
^   ^   ^   ^   ^   ^
0   1   2   3   4   5    index

當任務是將第五個數組元素與第二個數組元素交換時,您正確使用了數組元素索引 4 和 1,因為所有索引都從零開始。
現在匯編編程中的數組尋址不使用元素索引,而是依賴於元素偏移量 這就是您將索引乘以SIZEOF WORD 太糟糕了,您在與第二個數組元素交談時忘了寫這個比例因子。
這是發生的事情:

mov ax, myArray[4 * SIZEOF WORD]讀取偏移量 8 處的字,因此AX變為 0005h。

1,0,2,0,3,0,4,0,5,0,6,0  array
  ^             ^
  1             8        offset

xchg myArray[1], ax在偏移量 1 處寫入AX從而覆蓋第一個和第二個數組元素的一部分

1,5,0,0,3,0,4,0,5,0,6,0  array
  ^             ^
  1             8        offset

movzx eax, myArray[1]不會檢索第二個數組元素,因為即使 1 是正確的索引,它也不是正確的偏移量 這解釋了為什么會顯示 5。


當我使用 mov myArray [0 * SIZEOF WORD] 時,ax 我得到“位置 0 處的數字是 5”。 但它應該是1。

僅僅是因為您實際上沒有在EAX寄存器中加載第一個元素! 您錯誤地寫入了第一個元素,然后顯示了EAX發生的任何內容。 這就是值 5。

當我使用 mov ax, myArray[0] 時,我得到“位置 0 處的數字是 1281”。

這次您正確讀取了第 1 個元素,但鑒於程序的xchg部分破壞了第 1 個和第 2 個元素,您只能獲取其中的內容。

1,5,0,0,3,0,4,0,5,0,6,0  array
^
0                        offset

低字節 1 和高字節 5。它們一起產生 1281 (1 + 256 * 5)。


嘗試這個:

; exchange the fifth and second elements
mov     ax, myArray[4 * SIZEOF WORD]   ; read 5th element
mov     dx, myArray[1 * SIZEOF WORD]   ; read 2nd element
mov     myArray[4 * SIZEOF WORD], dx   ; write new 5th element
mov     myArray[1 * SIZEOF WORD], ax   ; write new 2nd element

; print the first element
mov     edx, OFFSET prompt1
call    WriteString
movzx   eax, myArray[0 * SIZEOF WORD]   ; read 1st element
call    WriteDec
call    Crlf 

請注意,這里交換 2 個元素確實是它所說的。 您的代碼僅第 5 個元素復制到第 2 個元素上!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM