The program sums the elements of an array stored in memory. It reads the length of the array from memory into the BX register. It then uses a loop to read each element into the AL register, add it to the running sum in the AX register, and increment the SI pointer. After the loop, it displays the sum stored in AX.
Write a MIPS assembly language program to sum the elements of an array.docx
1. Write a MIPS assembly language program to sum the elements of an array. The array is stored in
memory and contains 32-bit integers. The length of the array is stored in a byte in memory. Use
the template provided below. Only use instructions from the MIPS Core Instruction Set. Grading
Rubric: Reads array length and at least one element of the array. Has a properly structured loop
and reads all elements of the array correctly. Calculates the sum properly. Writes the sum to the
last element of the array. Uses a minimum number of assembly language instructions.
Solution
Code:
.MODEL SMALL
.STACK 100H
.DATA
PROMPT DB 'The Array elements are : $'
RESULT DB 0DH,0AH,'The Sum of the Array is = $'
ARRAY DB 10,20,30,40,50,60,70,80,90,100
.CODE
MAIN PROC
MOV AX, @DATA // initialize DS
MOV DS, AX
MOV BX, 10 // set BX=10
LEA DX, PROMPT // load and display the string PROMPT
MOV AH, 9
INT 21H
MOV CX, BX // set CX=BX
LEA SI, ARRAY // set SI=offset address of ARRAY
2. @LOOP: // loop label
XOR AH, AH // clear AH
MOV AL, [SI] // set AX=[SI]
CALL OUTDEC // call the procedure OUTDEC
INC SI // set SI=SI+1
MOV AH, 2 // set output function
MOV DL, 20H // set DL=20H
INT 21H // print a character
LOOP @LOOP // jump to label @LOOP while CX!=0
LEA DX, RESULT // load and display the string RESULT
MOV AH, 9
INT 21H
LEA SI, ARRAY // set SI=offset address of ARRAY
CALL SUM // call the procedure SUM
CALL OUTDEC // call the procedure OUTDEC
MOV AH, 4CH // return control to DOS
INT 21H
MAIN ENDP
SUM PROC
PUSH CX //push CX onto the STACK
PUSH DX // push DX onto the STACK
XOR AX, AX //clear AX
XOR DX, DX // clear DX
MOV CX, BX // set CX=BX
@SUM: // loop label
MOV DL, [SI] // set DL=[SI]
ADD AX, DX //set AX=AX+DX
INC SI //set SI=SI+1
LOOP @SUM //jump to label @SUM while CX!=0
POP DX // pop a value from STACK into DX
POP CX // pop a value from STACK into CX
RET // return control to the calling procedure
SUM ENDP