181k views
4 votes
using spim, write and test an adding machine program that repeatedly reads in integers and adds them into a running sum. the program should stop when it gets an input that is 0, printing out the sum at that point. use the spim system calls described on pages a-43 and a-45.

1 Answer

2 votes

Final answer:

The student needs to write a MIPS assembly program using SPIM to add integers until 0 is input and then print the sum.

Step-by-step explanation:

The student is asking for assistance in writing a program using SPIM, which is a simulator for the MIPS assembly language. The program should function as an adding machine that repeatedly reads in integers, adds them to a running sum, and stops when a 0 is input, at which point it prints out the sum. This task requires understanding of MIPS assembly language and the system calls for input and output as specified on pages a-43 and a-45 of the relevant textbook.

To complete this task, the student should utilize the SPIM system call for reading integers (syscall 5) and the system call for printing integers (syscall 1). The program will likely utilize a loop to continuously read numbers and an if statement to check if the read number is 0, which is the condition to terminate the loop and print out the running sum.

To create an adding machine program using SPIM, you can use the SPIM system calls to read integers and add them to a running sum.

Here is a sample code that implements this:

.data prompt: .asciiz "Enter an integer: " sum: .word 0 .text main: li $v0, 4 la $a0, prompt syscall loop: li $v0, 5 syscall beq $v0, $zero, exit lw $t0, sum add $t0, $t0, $v0 sw $t0, sum j loop exit: li $v0, 1 lw $a0, sum syscall li $v0, 10 syscall

This program uses a loop to repeatedly ask the user for an integer value, adds it to a running sum, and continues until the user enters 0. At that point, it prints out the sum and terminates.

User Phwt
by
7.8k points