找出数组,汇编语言中的差距总和

时间:2015-04-12 19:38:55

标签: arrays assembly

我在这个问题上疯了似的。我们应该找到数组中间隙的总和。我已经编写了代码来查找数组的总和但不知道如何找到间隙的总和。这就是我所拥有的。所以差距是2,3,4,1,总和应该是10。

.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword

.data
    array DWORD 0,2,5,9,10  
.code
main proc
    mov edi,OFFSET array
    mov ecx,LENGTHOF array
    mov eax,0

L1:
    add eax,[edi]
    add edi,TYPE array
    loop L1

    invoke ExitProcess,0
main endp
end main

3 个答案:

答案 0 :(得分:4)

寻找差距总和的漫长道路:

.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword

.data
    array DWORD 0,2,5,9,10  
.code
main proc
    mov esi,OFFSET array
    mov ecx,LENGTHOF array
    mov ebx,0                ; Start sum at 0
    cld                      ; Clear the direction bit (look up the LODS instruction!)
    lodsd                    ; Get the first value to start
    mov edx, eax             ; Save "previous" element read in edx
    dec ecx                  ; Decrement the loop counter 'cuz we read one
L1:
    lodsd
    sub ebx, edx             ; sum = sum + (this element - prev element)
    add ebx, eax             ; or, sum = sum - prev, then sum = sum + this
    mov edx, eax             ; save previous element in edx
    loop L1

    mov eax, ebx             ; put the result in eax
    invoke ExitProcess,0
main endp
end main

如果你需要做很长时间,但不允许使用lodsd,那么我将其留作练习,将你的循环转换为不使用lodsd

简短的方法是认识到差距的总和是:

(a[1] - a[0]) + (a[2] - a[1]) + ... + (a[N] - a[N-1])

假设您没有采用差异的绝对值,那就是:a[N] - a[0]。这可以计算如下(注意:我不确定这里的MASM语法是否有数组+偏移,所以你可能需要稍微修改一下):

.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword

.data
    array DWORD 0,2,5,9,10  
.code
main proc
    mov ebx, LENGTHOF array
    dec ebx                  ; last element is at length-1 index
    shl ebx, 2               ; length * 4 for size of DWORD
    mov eax, [array + ebx]   ; read last element
    sub eax, [array]         ; subtract first element

    invoke ExitProcess,0
main endp
end main

答案 1 :(得分:2)

我没有想要刷新我的装配......但是为了让你开始,这就是我在C中的表现(这被称为高级PDP-11汇编IIRC)

int array[5] = [0,2,5,9,10]
int sum = 0;
int length = 5;

for (i = 0; i < length - 1; i++)
{
   sum += (array[i+1]-array[i]);
}
//sum is now equal to 10, 2 + 3 + 4 + 1

答案 2 :(得分:-2)

Include Irvine32.inc

.data

array dword 0,2,5,7,9]              ; Array initializatiom

Sum dword 0                         ; variable containing sum

counter dword ?

.code

main proc

mov eax,0                         ; eax=0 because no garbage value is shown

mov esi,offset array              ; esi caontain array 1st index address
mov ecx,lengthof array-1          ; loop is repeated 4 times as two no are compared

  l1:

            add eax,[esi+4]                   ; eax have 2nd value of array
            sub eax,[esi]                     ; sub array 2nd and first value

            add esi,type array                ; Esi have next array value after each alteration 

            add sum,eax                       ; Sum is stored after each alteration

  loop l1

call writeint

    exit
main ENDP
END main