Input an array, then add the values, desperate

Hey, I've been working on this code for a little over a week. I admit, I just can't understand assembly. It feels like bashing my face against broken glass. And it's due tomorrow night now.

Anyway. This needs to take the input of up to 20 integers from a person. Then it will get the sum of those integers.

The user can specify if they want to put in, 5, 8, 10, any number of integers, as long as it's less than 20.

My issue? I have some code, but it just keeps asking for more integers. I tried putting in a variable for a counter, but now it just freaks out and demands why there are actual legible words in my code.

Any help would be great, code's here. v

[hr]

[code]
.data

Prompt1: .asciiz "
How many integers (20 or less) will you input? " #gets number of integers
Prompt2: .asciiz "
Enter the integers (one per line): " #Elements of array A entered from keyboard
A: .space 80 #Array A can store 20 integers (each 4 bytes)
Output: .asciiz "
The sum of the values entered is: "
counter: .space 8



.text

main:

howMany:
li $v0, 4 #system call code for print string
la $a0, Prompt1 #load address of prompt1 into $a0
syscall #print the prompt1 message

li $v0, 5 #system call code for read integer
syscall #reads the value of N into $v0

move counter, $v0 #moves value to counter variable to free up $v0

bge counter, 20, howMany #makes sure it's less than 20 integers, or re-prompts

getNumbers:
li $v0, 4 #system call code for print string
la $a0, Prompt2 #load address of prompt2 into $a0
syscall #print the prompt2 message

li $v0, 5 #system call code for read integer
syscall #reads 1st array value into $v0

move $t1, $v0 #moves first array value to $t1 from $v0
sw $t1, A #stores value in first array in base address
la $s1, A
li counter, 1 #initializes counter to one
addi counter, counter, 1 #should add one to the counter

ble $t0, counter, getNumbers

li $v0, 4
la $a0, Output
syscall
[/code]


Comments


  • I am not familiar with this assembly language, and I don't know what $t0 is, but shouldn't you be DECREMENTING counter, rather than incrementing it?

    Or incrementing another variable to compare to it.





  • You are also reinitializing your counter every time through your loop.

    Without knowing the assembly details, and since you need to fill an array, and therefore presumably need an index, the logic should include:

    Reserve Count (or use register)

    Reserve Array


    Input Count from user;

    Index = 0; probably a register

    Loop1:

    input value
    mov Array[Index], value

    Increment Index

    cmp Index, Count
    jump if not zero to Loop1:




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion