alisa
alisa

Reputation: 1

I have to multiply the digits as well as adding them.,

.data

enterMsg1: .asciiz "Please enter the last four digits of your student id \n"
enterMsg2: .asciiz "Press enter between each digit \n"
enterMsg3: .asciiz "Enter next digit \n"
TotalDig4: .asciiz "The total of the digits is: "
.text

# output the initial instruction text to the console
addi $v0, $zero, 4
la $a0, enterMsg1
syscall

# output the second instruction text to the console
addi $v0, $zero, 4
la $a0, enterMsg2
syscall


# read an integer from keyboard input and store the input in $s0 for the total
addi $v0, $zero, 5
syscall
# store the input in $s0
add $s0, $zero, $v0


# output the text asking for the next digit to the console
# then receive the input,
jal receiveInputs
 #add to total ($s0)
add $s0, $s0, $v0

# then receive the input,
jal receiveInputs
 #add to total ($s0)
add $s0, $s0, $v0

# then receive the input,
jal receiveInputs
 #add to total ($s0)
add $s0, $s0, $v0

# output the total text to the console
addi $v0, $zero, 4
la $a0, TotalDig4
syscall


add $a0, $s0, $zero
addi $v0, $zero, 1
syscall


addi $v0, $zero, 10
syscall

receiveInputs:
# receive the input, add to total ($s0)
addi $v0, $zero, 4
la $a0, enterMsg3
syscall

addi $v0, $zero, 5
syscall

jr $ra

Loop:
add $s0, $zero, $s0
addi $a0, $zero, 1
syscall

# output a space 
addi $v0, $zero, 10
addi $a0, $zero, 0x20
syscall

addi $s0, $s0, -1


# end the program
addi $v0, $zero, 10

Upvotes: 0

Views: 32

Answers (1)

gproxy
gproxy

Reputation: 11

After adding the first digit to $s0, on the next line add that same first digit from $v0 to $s1 which will hold your product.

Then after getting each digit from the function receiveInputs use (mul $s1, $s1, $v0) to multiply the digits.

Finally print the result from $s1 just as you would for the sum.

Upvotes: 1

Related Questions