C
I think I'll respond. The point is that the wording is:Find the sum of the components of the mass that match the values
indexes.The task is not solved in principle. It's just about finding the sum of whole numbers from zero to zero. length And we don't need a body. The obvious, and often in the head, is the cycle: xor eax, eax
xor edx, edx
mov ecx, array_size-1 ; нужно пояснять -1?
sum1:
add eax, ecx
adc edx, 0
loop sum1
; результат в eax:edx
But we can do without the cycles, as proposed by @Ni55aN, count the amount of arithmetic progress:It's important! We have to make sure we get the numerator. Clear Number, otherwise this approach won't work. This is the case, but if this approach is used for other progress, there may be options:mov edx, array_size
; значение последнего элемента массива равно
; его размеру-1 (по условию: первый элемент - 0,
; второй - 1, третий - 2, и т.д.)
; а первый добавлять не надо, он и так 0
mov eax, array_size-1
mul edx
sar edx, 1
rcr eax, 1
; результат в eax:edx
Well, if the wording of the assignment is different, for example:Find the sum of the components of the mass that match
indexes(difference in several letters, but it's substantial), it's just, we're going through the mass, we're comparing the element with its index, and we're matching the amount.Well, the last thing is checking all three options:; ----------------------------------
section .data
array dd 0, 1, 2, 3, 4, 5, 6
asize equ ($ - array)/4
s1 db 'Direct array scanning: %llu', 0Ah, 0
s2 db 'With cycle: %llu', 0Ah, 0
s3 db 'Arithmetic progression: %llu', 0Ah, 0
; ----------------------------------
section .text
extern printf
global main
; ----------------------------------
main:
; полный проход по массиву:
cld
mov esi, array
xor ecx, ecx
xor ebx, ebx
xor edx, edx
sum1:
lodsd
add ebx, eax
adc edx, 0
sum2:
inc ecx
cmp ecx, asize
jne sum1
push edx
push ebx
push s1
call printf
add esp, 12
; считаем сумму в цикле:
xor eax, eax
xor edx, edx
mov ecx, asize-1
sum3:
add eax, ecx
adc edx, 0
loop sum3
push edx
push eax
push s2
call printf
add esp, 12
; считаем сумму прогрессии:
mov edx, asize
;mov eax, [array+((asize-1)*4)]
mov eax, asize-1
mul edx
sar edx, 1
rcr eax, 1
push edx
push eax
push s3
call printf
add esp, 12
ret
; ----------------------------------