用于求和整数的汇编程序

时间:2015-10-04 23:19:38

标签: assembly mips

基本上我正在写一个程序,它会添加输入数字的总和..这是我的代码,它在main下给我一个错误..如果你发布一个答案,发现什么是错的,你能帮忙吗?贴出新代码​​?谢谢..编辑:错误信息

错误spim :(解析器)标签在文件C的第5行第二次定义:/ Users / peter / Desktop / Running Sums.asm main

        Scanner keyboard = new Scanner(System.in);

    String answer;
    int inputNum = 1;
    int countOdd = 0;
    int countEven = 0;


        do{
            System.out.println("Please enter an interger. When you are finished, enter 0.");
            inputNum = keyboard.nextInt();

             if (inputNum % 2 == 0)
                 countEven++;
               if (inputNum % 2 != 0)
                   countOdd++;
        }while(inputNum != 0);


        System.out.println("Thank you. The amount of odd intergers you entered is " 
        + countOdd + ". The amount of even intergers you entered is " + countEven);

1 个答案:

答案 0 :(得分:1)

正确的工作代码是:

    .data
str: .asciiz    "Enter a integer:"
str2: .asciiz   "Sum ="
.text
main:
li $s0,0    #sum = 0
loop:
li $v0,4    #print string
la $a0,str
syscall

li $v0,5    #Take input
syscall
beq $v0,$0,done #if zero was input end and display sum

add $s0,$s0,$v0  #sum = sum + input
 j     loop     #Jump to input another number
done:
li  $v0,4       #print string
la  $a0,str2
syscall
li $v0,1        #print sum
move    $a0,$s0
syscall
.end

这本书是错的,或者你把它复制错了。