2016-02-28 27 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再次 它的工作原理!非常感谢你们! –