MIPS构造循环 [英] MIPS Constructing Loops

查看:94
本文介绍了MIPS构造循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

将第一个整数顺序存储在s0

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

请求串联的整数数量

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

在s1中存储整数的数量

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

请求整数的偏移量

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

在s2中存储整数的偏移量

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

设置计数器

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.

推荐答案

我不确定您在设置计数器"部分要做什么,因为s1已包含系列成员的数目.

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

s0包含当前元素.

您所需要的(其他信息)是清除current_sum,假设为s3:

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

因此,对于您的第一个示例,在第一次循环迭代之前,s0-s3将被设置为[5, 3, 4, 0].这就是您所需要的所有东西(世界状态"),您将要设置的任何其他值可能都是某个特定子任务的临时值,例如显示一些值和类似内容,但是只要核心计算成功,这四个值就代表了您的全部需求,其他一切都可以基于它们.

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.

如果s1已经小于1(<=0测试),则报告错误的输入.

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

然后执行循环算法:

  • 将当前元素(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)
  • 显示", "
  • 跳到第一步
  • 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:逻辑将涉及打印行尾,求和文本描述,当前总和值(s3),另一新行以及要求重复y/n.

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.

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

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

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

exit_loop,核心值是[13, 0, 4, 27],代码将显示27作为序列和.

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

这篇关于MIPS构造循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆