查找数组中的间隙总和,汇编语言
Find sum of the gaps in array, assembly language
我在这个问题上陷入了疯狂。我们应该找到数组中间隙的总和。我已经编写了代码来查找数组的总和,但不知道如何找到间隙的总和。这就是我所拥有的。所以差距是 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
我对刷新我的程序集的愿望为零...但是为了让您入门,下面是我在 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
求差距之和的漫长道路:
.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
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
我在这个问题上陷入了疯狂。我们应该找到数组中间隙的总和。我已经编写了代码来查找数组的总和,但不知道如何找到间隙的总和。这就是我所拥有的。所以差距是 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
我对刷新我的程序集的愿望为零...但是为了让您入门,下面是我在 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
求差距之和的漫长道路:
.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
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