Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

assembly - MIPS Constructing Loops

Okay, so this might be a really silly question but I don't quite have the hang of Assembly yet. I have to write a program that calculates the summation of a series of numbers. It should behave like so:

Enter the first integer in the series: 5
Enter the number of integers in the series: 3
Enter the offset between integers in the series: 4

The series is: 5, 9, 13.
The summation of the series is 27. 

Would you like to calculate another summation (Y/N)? y

Enter the first integer in the series: 4
Enter the number of integers in the series: 5
Enter the offset between integers in the series: 27

The series is 4, 31, 58, 85, 112.
The summation of the series is 290.

Would you like to calculate another summation (Y/N)? Y

Enter the first integer in the series: -16
Enter the number of integers in the series: -22
There must be a positive number of integers in the series.

Would you like to calculate another summation (Y/N)? n

This is what I have so far:

li $v0, 4   #put 4 in as main parameter in v0
la $a0, Q1  #syscall will print string query 1
syscall

Store first integer in series in s0

li $v0, 5   #put 5 in as main parameter in v0
syscall     #syscall will read integer from Q1
move $s0, $v0   #move integer in v0 to s0

Request number of integers in series

li $v0, 4   #put 4 in as main parameter in v0
la $a0, Q2  #syscall will print string query 2
syscall

Store number of integers in series in s1

li $v0, 5   #put 5 in as main parameter in v0
syscall     #syscall will read integer from Q2
move $s1, $v0   #move integer in v0 to s1

Request offset of integers

li $v0, 4   #put 4 in as main parameter in v0
la $a0, Q3  #syscall will print string query 3
syscall

Store offset of integers in s2

li $v0, 5   #put 5 in as main parameter in v0
syscall     #syscall will read integer from Q3
move $s2, $v0   #move integer in v0 to s1

Set counter

li $s3, 1   #Set counter to zero
li $t0, 1   #iterator count is in t0

I'm just curious as to where to start my loop? And how exactly would I go about printing the entire series? Any advice would be greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not sure what you do in "set counter" part, as the s1 already contains number of series members.

And s0 contains current element.

All you need (additional information) is to clear current_sum, let's say as s3:

add $s3, $zero, $zero   ; or "move $s3, $zero" if you prefer the pseudo-ops

So for your first example s0-s3 will be set to [5, 3, 4, 0] before first loop iteration. This is everything ("world state") you need, any other value you will set up is probably some temporary for particular sub-task like displaying some value and similar, but as long as the core computation goes, these four values represent all you need, and everything else can be based upon them.

If s1 is already less than 1 (<=0 test), report wrong input.

Then the loop algorithm:

  • add current element (s0) to current sum (s3) ; summing up all numbers
  • display current element (s0)
  • ; handle the correct count of series' members
  • decrement counter s1
  • if s1 is zero, then goto exit_loop
  • ; preparing for next loop iteration
  • add offset (s2) to current element (s0)
  • display ", "
  • jump to first step

exit_loop: logic will involve printing end of line, summation text description, current sum value (s3), another new line(s) and asking for repeat y/n.

For example that 4 core values will evolve during iterations like this:

  • [5, 3, 4, 0] => displays "5, " + all the updating of values
  • [9, 2, 4, 5] => displays "9, " + all the updating of values
  • [13, 1, 4, 14] => modifies s3 to 27, displays "13", --s1, jumps to exit_loop

At exit_loop the core values are [13, 0, 4, 27], code will display 27 as series sum.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...