Create a procedure that returns the sum of all elements in an array that fall wi
ID: 3813464 • Letter: C
Question
Create a procedure that returns the sum of all elements in an array that fall within a given range [a, b] inclusive on both ends.
Create a program that tests your procedure at least twice.
Inputs to your procedure:
Pointer to a signed doubleword array
Size of the array
Value for a (start point in the array)
Value for b (end point in the array)
Return the sum in the EAX register
Preserve all other register values and replace them to their previous states prior to returning from the procedure.
Using Irvine32.inc in microprocessor assembly language for Intel x86 processor. Please provide output screen.
Explanation / Answer
Program sumArr.asm:
; This program traverses the whole array to find sum of all
; the elements within a given range
INCLUDE Irvine32.inc
SPACE = 32
.data
str1 BYTE “The sum of array elements within range 30 to
60 is: ”, 0
str2 BYTE “The sum of array elements within range 65 to
100 is: ”, 0
str3 BYTE “The array elements are: ”, 0
arrSize DWORD 10
arr1 SDWORD -1,20,32,34,60,33,77,87,100,55
.code
main PROC
; calls the procedures
call Clrscr ; clears the screen
; displays the array elements
call display
call Crlf
call Crlf
; add integers in array within the range 30 to 60
mov esi,OFFSET arr1 ; pass the address of array
mov ecx,arrSize ; pass the count of elements
mov ebx,30 ; pass the value of j
; (lower limit)
mov edx,60 ; pass the value of k
(upper limit)
call sumArray ; gets the sum in EAX
mov edx,OFFSET str1
call WriteString ; writes the string str1
call WriteDec ; writes the value in EAX
call Crlf
; add integers in array within the range 65 to 100
mov esi,OFFSET arr1 ; pass the address of array
mov ecx,arrSize ; pass the count of elements
mov ebx,65 ; pass the value of j
(lower limit)
mov edx,100 ; pass the value of k
(upper limit)
call sumArray ; gets the sum in EAX
mov edx,OFFSET str2
call WriteString ; writes the string str2
call WriteDec ; writes the value in EAX
call Crlf
exit
main ENDP
sumArray PROC USES ecx esi ebx edx
; adds the array elements within a specified range
; Receives: ESI=address of array, ECX=array count, EBX,
; EDX=value of j & k
; Returns: EAX= sum of elements within range j to k
mov eax,0 ; initialize the sum to 0
Label1:
cmp arr1[esi*4],ebx ; check if array element >= j
;(lower limit)
jb Label2
cmp arr1[esi*4],edx ; check if array element <= k
;(upper limit)
ja Label2
add eax,arr1[esi*4] ; add array integer in sum
; total
Label2:
inc esi ; point to next array element
loop Label1 ; continue the loop
ret
sumArray ENDP
display PROC USES esi ecx edx
; displays array elements
; Receives: Nothing
; Returns: Nothing
mov edx,OFFSET str3
call WriteString ; writes the string str3
call Crlf
mov esi,0 ; stores starting array index
mov ecx,arrSize ; runs the loop for 10 times
Label1:
mov eax, arr1[esi*4] ; store array integer in EAX
call WriteDec ; writes the array element
mov al,SPACE
call WriteChar ; leaves a space
inc esi ; point to next array element
loop Label1 ; continue traversing
ret
display ENDP
END main
Note:Here the name of program file is sumArr.asm; the program contains comments starting with a semicolon (;) symbol.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.