MIPS 构造循环

MIPS Constructing Loops

好的,所以这可能是一个非常愚蠢的问题,但我还不太了解 Assembly。我必须编写一个程序来计算一系列数字的总和。它应该像这样:

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

这是我目前拥有的:

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

将系列中的第一个整数存储到 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

请求整数个数

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

在 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

请求整数偏移量

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

在 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

设置计数器

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

我只是想知道从哪里开始我的循环?我将如何打印整个系列?任何建议将不胜感激。

我不确定你在 "set counter" 部分做了什么,因为 s1 已经包含了系列成员的数量。

并且 s0 包含当前元素。

所有你需要的(附加信息)是清除 current_sum,比方说 s3:

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

因此,对于您的第一个示例,s0-s3 将在第一次循环迭代之前设置为 [5, 3, 4, 0]。这就是你需要的一切 ("world state"),你将设置的任何其他值可能是一些临时的特定值 sub-task 比如显示一些值和类似的,但只要核心计算进行,这四个值代表你所需要的一切,其他一切都可以基于它们。

如果s1已经小于1<=0测试),报错。

然后循环算法:

  • 将当前元素 (s0) 添加到当前总和 (s3) ; summing up all numbers
  • 显示当前元素(s0)
  • ; handle the correct count of series' members
  • 递减计数器s1
  • 如果 s1 为零,则转到 exit_loop
  • ; preparing for next loop iteration
  • 将偏移量 (s2) 添加到当前元素 (s0)
  • 显示", "
  • 跳转到第一步

exit_loop: 逻辑将涉及打印行尾、求和文本描述、当前求和值 (s3)、另一行并要求重复 y/n.

例如,4 个核心价值观将在这样的迭代过程中演变:

  • [5, 3, 4, 0] => 显示 "5, " + 所有值的更新
  • [9, 2, 4, 5] => 显示 "9, " + 所有值的更新
  • [13, 1, 4, 14] => 将s3修改为27,显示"13"--s1,跳转到exit_loop

exit_loop 处,核心值是 [13, 0, 4, 27],代码将显示 27 作为系列总和。