4.5.4 Summing an Integer Array There’s hardly any task more common in beginning
ID: 3736365 • Letter: 4
Question
4.5.4 Summing an Integer Array There’s hardly any task more common in beginning programming than calculating the sum of the elements in an array. In assembly language, you would follow these steps:
1. Assign the array’s address to a register that will serve as an indexed operand.
2. Initialize the loop counter to the length of the array.
3. Assign zero to the register that accumulates the sum.
4. Create a label to mark the beginning of the loop.
5. In the loop body, add a single array element to the sum.
6. Point to the next array element.
7. Use a LOOP instruction to repeat the loop.
The Array you declare in your program should contain AT LEAST TEN WORD-TYPE ELEMENTS.
THE SUM must be stored in a Memory.
THE ORIGINAL ARRAY and the SUM MUST BE PRINTED!
book: assembly language for x86 processors pg 126
Explanation / Answer
Please find my implementation.
1.Assign the array’s address to a register that will serve as an indexed operand.
2. Initialize the loop counter to the length of the array.
3. Assign zero to the register that accumulates the sum.
4. Create a label to mark the beginning of the loop.
5. In the loop body, add a single array element to the sum.
6. Point to the next array element.
7. Use a LOOP instruction to repeat the loop.
TITLE Summing an Array (SumArray.asm)
INCLUDE Irvine32.inc
.data intarray DWORD 10000h,20000h,30000h,40000h
.code main PROC
mov edi,OFFSET intarray ; 1: EDI = address of intarray
mov ecx,LENGTHOF intarray ; 2: initialize loop counter
mov eax,0 ; 3: sum = 0
L1: ; 4: mark beginning of loop
add eax,[edi] ; 5: add an integer
add edi,TYPE intarray ; 6: point to next element
loop L1 ; 7: repeat until ECX = 0
exit
main ENDP
END main
print out all the elements in the array in reverse order. Again, there should be at least 10 elements in the array.
.data
arr: .space 32
msg1: .asciiz "put in first integer"
msg2: .asciiz "put in second integer"
msg3: .asciiz "put in third integer"
msg4: .asciiz "put in fourth integer"
msg5: .asciiz "put in fifth integer"
msg6: .asciiz "put in sixth integer"
msg7: .asciiz "put in seventh integer"
msg8: .asciiz "put in eighth integer"
.globl main
.text
main:
la $s0, arr
li $v0, 4
la $a0, msg1
syscall
li $v0, 5
syscall
sw $v0, 28($s0)
li $v0, 4
la $a0, msg2
syscall
li $v0, 5
syscall
sw $v0, 24($s0)
li $v0, 4
la $a0, msg3
syscall
li $v0, 5
syscall
sw $v0, 20($s0)
li $v0, 4
la $a0, msg4
syscall
li $v0, 5
syscall
sw $v0, 16($s0)
li $v0, 4
la $a0, msg5
syscall
li $v0, 5
syscall
sw $v0, 12($s0)
li $v0, 4
la $a0, msg6
syscall
li $v0, 5
syscall
sw $v0, 8($s0)
li $v0, 4
la $a0, msg7
syscall
li $v0, 5
syscall
sw $v0, 4($s0)
li $v0, 4
la $a0, msg8
syscall
li $v0, 5
syscall
sw $v0, 0($s0)
li $v0, 1
lw $a0,0($s0)
syscall
li $v0, 1
lw $a0,4($s0)
syscall
li $v0, 1
lw $a0,8($s0)
syscall
li $v0, 1
lw $a0,12($s0)
syscall
li $v0, 1
lw $a0,16($s0)
syscall
li $v0, 1
lw $a0,20($s0)
syscall
li $v0, 1
lw $a0,24($s0)
syscall
li $v0, 1
lw $a0,28($s0)
syscall
li $v0, 10#terminate execution
syscall
Please upvote if it helped you!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.