从一个字符添加整数循环退出 [英] Exit from Adding Integers Loop with a Character

查看:129
本文介绍了从一个字符添加整数循环退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试,直到用户输入的字符写入来自用户所总结整数MIPS code*。

I'm trying to write the MIPS code of summing integers taken from user until user enters the character " * ".

输出会喜欢1 4 3 *总计:8

Output will like "1 4 3 * Total: 8"

我写了code终止循环条件,当用户输入-1,而不是*。我试着写'*'的语法,而不是-1,但它不给的总和。如何从在用户进入*字符的循环退出?这是我的第一个问题,这里是我的工作code为-1。

I wrote the code termination loop condition when user enters " -1 " instead of " * ". I tried writing '*' syntax instead of -1 but it doesn't give the sum. How can i exit from the loop when user enters " * " character? This was my first question and here is my working code for " -1 ".

 # $s0=Sum, $s1=Checkvalue 

         .data                    # Data memory area 
 prompt: .asciiz "Total: " 
         .text                    # Code area

 main:  
         move   $s0, $zero        # Sum is made "0"
         li     $s1, -1           # Checkvalue is made "-1"

 loop:   bne    $s0, $s0, exit    # Infinite loop is described
         li     $v0, 5            # Read integer into $v0
         syscall                  # Make the syscall read_int

         move   $t0, $v0          # Move the integer read into $t0

         beq    $t0, $s1, exit    # If the read value is equal "-1", exit
         add    $s0, $t0, $s0     # Add the read integer to sum
         j      loop              # Return loop

 exit:
         li     $v0, 4            # Syscall to print string
         la     $a0, prompt
         syscall

         move   $a0, $s0          # Syscall to print string
         li     $v0, 1
         syscall

         li     $v0, 10           # Syscall to exit
         syscall

 # Reference: www.es.ele.tue.nl/~heco/courses/ProcDesign/SPIM.ppt

我的第二个问题是,我使用了系统调用,是我的使用适用于stdin和stdout,系统功能?

My second question is that I used "syscall", is my usage suitable for stdin and stdout, system functions?

感谢您这么多。

推荐答案

您正在阅读像 1 整数,并且很明显 * 不是一个整数。要退出循环,你必须弄清楚如何读取字符和整数。如果你的号码只有一个数字的长,可以读取所有输入的字符,并转换为整数作为必要的。否则,你将不得不在字符串读取并转换为整数。

You are reading integers like -1, and it's clear * is not an integer. To exit the loop, you will have to figure out how to read characters and integers. If your numbers are only one digit long, you can read all input as characters and convert to integers as necessary. Otherwise, you will have to read in strings and convert to integers.

至于你的第二个问题,你是在使用系统调用标准输入输出系统的功能是正确的。

As for your second question, you are correct in using syscall for standard input and output system functions.

这篇关于从一个字符添加整数循环退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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