2016-02-28 58 views
3

因此,這可能是一個具體問題,但我的ASM分配是創建一個10個元素的數組,將第一個元素添加到最後一個元素,並將結果放入數組的第一個元素,然後第二個元素與第九一個,並將結果在所述陣列的所述第二元件等更改edi的偏移量與更改地址處的值?

A0 + A9 ---> A0 A1 + A8 ---> A1等

相同的程序應從第10個元素中減去第一個元素,並將結果放置在第10個元素中。減去第9個元素的第2個元素,並將結果放在第9個元素中,如下所示:

這樣,如果輸入1,2,3,4,5,6,7,8,9,0作爲例子,程序輸出應該是1,11,11,11,11,1,3,5,7,-1。

我在這裏完全損失,我不知道如何來回移動edi中的OFFSET以及更改該地址的值?

INCLUDE c:\Irvine\Irvine32.inc 

ExitProcess proto,dwExitCode:dword 

.data  ;// write your data in this section 
    intarray DWORD ?,?,?,?,?,?,?,?,?,? 
    msg2 BYTE "The processed array:", 0 
    endl BYTE 0dh, 0ah, 0 
    count DWORD 0 
    x DWORD 0 
    y DWORD 0 


.code  
    main proc 
    mov eax, 0 ; zeros out the eax register 
    mov ecx, LENGTHOF intarray 
    mov edi, OFFSET intarray; 
    mov edx, OFFSET endl; moves the location of endl to edx 

L1: 
    call ReadInt ; takes user integer input for the eax register 
    mov [edi], eax; moves value from the eax register to the edi 
    add edi, TYPE DWORD; increments the address 
    Loop L1; restarts first loop 


    mov edx, OFFSET msg2 ; moves msg2 to the edx register 
    call WriteString ; Writes the value in the edx register to the screen 
    mov edx, OFFSET endl ; moves endl (line break) to the edx register 
    call WriteString ; prints the value in the edx register to the screen 


    mov ecx, LENGTHOF intarray/2 ; 
     L3: 

    Loop L3 ; restarts the loop 

    mov ecx, LENGTHOF intarray ; 
    mov edi, OFFSET intarray; 

     L4: 
      mov eax, edi; 
      call WriteInt 
      add edi, TYPE DWORD; increments the address 

     loop L4 

    invoke ExitProcess,0 
main endp 
end main 

回答

2

讀取這兩種陣列元件中的寄存器,然後在陣列的所請求的結束執行addsub
在EDI中前進指針,但ECX降低兩倍。

mov edi, OFFSET intarray 
    mov ecx, 9   ;10 elements in the array 
Again: 
    mov eax, [edi] 
    mov ebx, [edi+ecx*4] 
    add [edi], ebx 
    sub [edi+ecx*4], eax 
    add edi, 4 
    sub ecx, 2 
    jnb Again 
+0

這不是我正在用的方向,但是當我用這種方式嘗試時,我至少得到了實數,而不是地址。但輸出對我來說並不正確,用1,2,3,4,5,6,7,8,9,0我得到1,5,1,4,5,6,7,8,9, - 1 –

+1

當你使用它時,你將ecx放大4倍,所以你應該只將它減少2(或者初始化爲9 * 4'並且移除有效地址中的* 4',這可能會更容易讀書)。否則,循環只運行2次迭代(ecx = 9和ecx = 1)。 @ Io-stream:如果你沒有自己解決這個問題,試試吧。 –

+0

好的!因此,我的最後的代碼是 MOV EDI,intarray OFFSET MOV ECX,9;所述陣列 再在10個元件: MOV EAX,[EDI] MOV EBX,[EDI + ECX * 4] 添加[EDI],EBX sub [edi + ecx * 4],eax add edi,4 sub ecx,2 jnb再次 它的工作原理!非常感謝你們! –