ASSEMBLY - need help to print out each value from an array in this code. Not sur
ID: 3601056 • Letter: A
Question
ASSEMBLY - need help to print out each value from an array in this code. Not sure why i'm getting error. Please help.
.data
applesSold SDWORD 8, 6, 7, 5, 3 ; initialized array - total value = 29 will be stored as EAX=0000001D
orangesSold SDWORD 1, 9, 4, 2 ; initialized array - total value = 16 will be stored as EBX=00000010
totalApplesSold SDWORD ? ; uninitialized
totalOrangesSold SDWORD ? ; uninitialized
totalFruitsSold SDWORD ? ; uninitialized
totalApplesString SBYTE 'Total apples sold: ', 0
totalOrangesString SBYTE 'Total oranges sold: ', 0
totalFruitSold SBYTE 'Fruit sold: ', 0
totalFruitString SBYTE 'Total fruit sold: ', 0
.code
main PROC
MOV EAX, 0 ; clear all regs by setting to 0
MOV EBX, 0
MOV ECX, 0
MOV EDI, 0
MOV EAX, totalApplesSold ; move source to destination
MOV EDI, OFFSET applesSold ; address of applesSold array
MOV ECX, LENGTHOF applesSold ; loop counter
MOV EAX,0 ; zero the accumulator
SUM_L1:
ADD EAX, [EDI] ; add an integer
ADD EDI, TYPE applesSold ; point to next integer
LOOP SUM_L1 ; repeat until EAX = 0
MOV EBX, totalOrangesSold ; move source to destination
MOV EDI, OFFSET orangesSold ; address of orangesSold array
MOV ECX, LENGTHOF orangesSold ; loop counter
MOV EBX,0 ; zero the accumulator
SUM_L2:
ADD EBX, [EDI] ; add an integer
ADD EDI, TYPE orangesSold ; point to next integer
LOOP SUM_L2 ; repeat until BX = 0
MOV ECX, totalFruitsSold ; store values of both arrays
ADD ECX, EAX ; add values of instruction operands of the same size from source to destination
ADD ECX, EBX ; add values of instruction operands of the same size from source to destination
;CALL dumpRegs ; calls dumpregs to display values of regs
CALL displayTotals
exit
main ENDP
displayTotals PROC uses eax edx
mov eax, totalApplesSold ; load eax with total apples to display
mov edx, offset totalApplesString ; load edx with address of total apple string
call writeString ; display "label"
call writeInt ; display total apples (showing sign)
call crlf ;
neg applesSold+4
mov eax, applesSold+4
mov eax, totalOrangesSold ; load eax with total oranges to display
mov edx, offset totalOrangesString ; load edx with address of total orange string
call writeString ; display "label"
call writeInt ; display total oranges (showing sign)
call crlf
mov eax, totalFruitSold ; load eax with total fruit to display
mov edx, offset totalFruitString ; load edx with address of total fruit string
call writeString ; display "label"
call writeInt ; display total fruit (showing sign)
call crlf
ret
displayTotals ENDP
END main
Explanation / Answer
ex;-
.data
.align 2
list: .space 40 #an array of 10 ints
blank: .asciiz " "
mess: .asciiz "The values in the array are "
.globl main
.text
main:
jal read
li $v0, 4
la $a0, mess
syscall
jal print
jal total
jal average
lw $a0, 0($sp) #load avererage from stack
li $t0, 1 #print it
syscall #causes ERROR here ############
b done #end program
#reads in 10 digits into array
read:
li $v0, 5 #prompts for int
syscall
sw $v0, list($t0) #stores int in array
addi $t0, $t0, 4 #holds the number 4
blt $t0, 40, read #repeats until array is full
jr $ra #return to caller
#prints array
print:
lw $a0, list($t1) #loads int from array
li $v0, 1 #print int
syscall
li $v0, 4 #if I take this block out it runs ########
la $a0, blank #
syscall #
addi, $t1, $t1, 4 #counter
blt $t1, 40, print #repeats until array it full
jr $ra #return to caller
#adds the total of all array elements
total:
lw $t3, list($t2) #gets ints from array
add $s0, $s0, $t3 #adds number to running total
addi $t2, $t2, 4 #counter
blt $t2, 40, total #repeat until all ints are added
addi $sp, $sp, -4 #allocate space on array
sw $s0, 0($sp) #store total on stack
jr $ra #return to caller
#divides the total by 10, for the average
average:
lw $a0, 0($sp) #gets the total from the stack
addi $t4, $t4, 10 #10 to divide by
div $a0, $t4
mflo $t5
sw $t5, 0($sp) #store aveg in stack
jr $ra