Assembler Developer

Moderators: None (Apply to moderate this forum)
Number of threads: 1018
Number of posts: 1812

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Input an array, then add the values, desperate Posted by Marintha on 23 Mar 2011 at 1:30 PM
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




.data

	Prompt1: .asciiz "\n How many integers (20 or less) will you input? " #gets number of integers
	Prompt2: .asciiz "\n 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 "\n 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



Report
Re: Input an array, then add the values, desperate Posted by Bannerdog on 7 Apr 2011 at 4:44 PM

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.



Report
Re: Input an array, then add the values, desperate Posted by Bannerdog on 7 Apr 2011 at 5:00 PM


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:







 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.