Error in : invalid program counter value: 0x00000000 suming up inputs

0

I am trying to write a program that asks the user for 6 numbers then adds it all up as an output but I everything I enter all my inputs i get an error saying:

Error in : invalid program counter value: 0x00000000

Go: execution terminated with errors.

I don't know how to fix it and I don't know what line to fix. Thanks!

.data
# user input number for addition
msg1: .asciiz "Enter first number : "   
num1: .word 1
msg2: .asciiz "Enter second number : "
num2: .word 1
msg3: .asciiz "Enter third number : "
num3: .word 1
msg4: .asciiz "Enter fourth number : "
num4: .word 1
msg5: .asciiz "Enter fifth number : "
num5: .word 1
msg6: .asciiz "Enter sixth number : "
num6: .word 1

.text
addi $sp,$sp,-20    # stack to save data

li $v0,4
la $a0,msg1     # enter first number
syscall
li $v0,5
la $a0,num1     # read number
syscall
sw $a0,0($sp)       # push to sp

li $v0,4
la $a0,msg2
syscall
li $v0,5
la $a0,num2
syscall
sw $a0,4($sp)

li $v0,4
la $a0,msg3
syscall
li $v0,5
la $a0,num3
syscall
sw $a0,8($sp)

li $v0,4
la $a0,msg4
syscall
li $v0,5
la $a0,num4
syscall
sw $a0,12($sp)

li $v0,4
la $a0,msg5
syscall
li $v0,5
la $a0,num5
syscall
sw $a0,16($sp)

li $v0,4
la $a0,msg6
syscall
li $v0,5
la $a0,num6
syscall



li $t0, 6        # initialize counter
adds:            # addition loop
beqz $t0, exit       # counter zero exit
lw $a1,0($sp)
add $a0,$a0,$a1      # add numbers
addi $sp,$sp,4       # increment sp
addi $t0,$t0,-1      # decrement count
j adds           # loop
li $v0,1
syscall
exit:jr $ra
li $v0,10
syscall
assembly
mips
mars-simulator
asked on Stack Overflow Dec 27, 2018 by Darren Cheng

1 Answer

0

The calls for reading the integer:

li $v0,5
la $a0,num1     # read number

Are not correct - you only need to set v0 to 5 and do the syscall - the read number will then be in v0 - which you need to do something with - not a0 like you are currently using.

You are iterating the loop 6 times and adding 4 to sp - ie: adding 24, but you allocated 20 in space on the stack, so now when anything else usng the stack what the are getting it not what they think

You don't have a main label, so it may not even be running your code.

And the syscall 10 never happens as you are getting to the exit, which jumps back to ja.

If you run the code in the debugger nand single step it, you will see what it is doing.

answered on Stack Overflow Jan 3, 2019 by lostbard

User contributions licensed under CC BY-SA 3.0