In: Computer Science
Please do both questions in MASM and complete these two in two separate files.
3. Summing the Gaps between Array Values
Write a program with an indexed addressing that calculates the sum of all the gaps between successive array elements. The array elements are doublewords, sequenced in nonde- creasing order. So, for example, the array {0, 2, 5, 9, 10} has gaps of 2, 3, 4, and 1, whose sum equals 10.
4. Copying a Word Array to a DoubleWord array
Write a program that uses a loop to copy all the elements from an unsigned Word (16-bit) array into an unsigned doubleword (32-bit) array.
Write a program with an indexed addressing that calculates the sum of all the gaps between successive array elements. The array elements are doublewords, sequenced in nonde- creasing order. So, for example, the array {0, 2, 5, 9, 10} has gaps of 2, 3, 4, and 1, whose sum equals 10.
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO,
dwExitCode:DWORD
.data
array DWORD 0,2,5,9,10
//initializing the
array
result DWORD 0
//initializing
the result element
.code
main
PROC // main process
starts
mov ECX, LENGTHOF array
//store the length of array in ECX
mov ESI, OFFSET
array
//this stores the address of the array in ESI
L1:
MOV
EAX,[ESI]
//move value of ESI to EAX
MOV EBX,[ESI+4]
//add 4 to ESI and move the value to EBX
SUB
EBX,EAX
//subtracting the value of EAX from EBX and storing the result
in EBX
ADD
result,EBX
// adding EBX with result and storing the value in
result
ADD ESI, TYPE
array
// moves pointer to next array element
Loop
L1
//loop the whole process
INVOKE
ExitProcess,0
main
ENDP //end
process
END
main //end of
program
Write a program that uses a loop to copy all the elements from an unsigned Word (16-bit) array into an unsigned doubleword (32-bit) array.
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO,
dwExitCode:DWORD
.data
array WORD
0,2,5,9,10
//initializing the array
newArray DWORD LENGTHOF
array DUP(?)
.code
main
PROC // main process
starts
mov ECX, LENGTHOF
array
//store the length of array in ECX
mov ESI, OFFSET
array
//this stores the address of the array in ESI
mov EDI, OFFSET
newArray
//this stores the address of the new array in
EDI
L1:
MOV
EAX,0
//move value of 0 to EAX
MOV
AX,[ESI]
//move value of ESI to EAX
MOV [EDI], EAX
//move adress of EAX to EDI
ADD ESI, TYPE
array
// moves pointer to next array element
ADD EDI, TYPE
newArray
// moves pointer to next newarray element
Loop
L1
//loop the whole process
INVOKE
ExitProcess,0
main
ENDP //end
process
END main
//end of
program