I have a code where I find the sum of 10 numbers from the numbers
table. How can I find the correct sum of every other number? Do you need to somehow work with lodsb
to load eax
every second number into EAX? Or do you still need to do something with the add instruction?
cld
mov esi, OFFSET numbers ; in `numbers` i have 10 numbers
mov edi, esi
mov ecx, 10
myLoop:
push ecx
lodsb ; here i add each digit in turn to the `eax` register
add sum, eax ; here i summarize the numbers
pop ecx
loop myLoop
mov eax, sum
CodePudding user response:
I have a code where I find the sum of 10 numbers from the
numbers
table.
An array of bytes
Assuming your code is correct for summing these 10 numbers, the only thing missing is zeroing the EAX
register beforehand. That's not something that LODSB
is going to do!
mov esi, OFFSET numbers
mov ecx, 10
xor eax, eax
myLoop:
lodsb
add sum, eax
loop myLoop
mov eax, sum
Only summing every second byte
mov esi, OFFSET numbers
mov ecx, 10 / 2 ; Half
xor eax, eax
myLoop:
inc esi ; Skip (or use an extra LODSB here)
lodsb
add sum, eax
dec ecx
jnz myLoop
mov eax, sum
An array of dwords
The LODSB
instruction does not load the whole number. Use LODSD
.
mov esi, OFFSET numbers
mov ecx, 10
myLoop:
lodsd
add sum, eax
loop myLoop
mov eax, sum
Only summing every second dword
mov esi, OFFSET numbers
mov ecx, 10 / 2 ; Half
myLoop:
add esi, 4 ; Skip (or use an extra LODSD here)
lodsd
add sum, eax
dec ecx
jnz myLoop
mov eax, sum
Similar to what @Jester suggested.
mov ecx, (10 / 2) - 1
myLoop:
add eax, [numbers ecx * 8 4]
dec ecx
jns myLoop