Chapter 4
Data Transfers, Addressing, and Arithmetic
Assembly Language Programming Exercise
Problem # 3:
Write a program with a loop and indexed addressing that calculates the sum of all the gaps between successive array elements. The array elements are doublewords, sequenced in non decreasing order.
So, for example, the array {0, 2, 5, 9, 10} has gaps of 2, 3, 4, and 1, whose sum equals 10.
Solution:
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
array DWORD 0,2,5,9,10
result DWORD 0
.code
main PROC
mov ecx, LENGTHOF array
mov ESI, OFFSET array
L1:
MOV EAX,[ESI]
MOV EBX,[ESI+4]
SUB EBX,EAX
ADD result,EBX
ADD ESI, TYPE array
Loop L1
INVOKE ExitProcess,0
main ENDP
END main
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
array DWORD 0,2,5,9,10
result DWORD 0
.code
main PROC
mov ecx, LENGTHOF array
mov ESI, OFFSET array
L1:
MOV EAX,[ESI]
MOV EBX,[ESI+4]
SUB EBX,EAX
ADD result,EBX
ADD ESI, TYPE array
Loop L1
INVOKE ExitProcess,0
main ENDP
END main
Previous Post:
Exchanging Pairs of Array Values
Next Post: