1
#program for inputting an integer and displaying it.

.data
    prompt:  .asciiz "Enter a number: "
    message: .asciiz "\nNumber entered is: "

.text
    li $v0, 4  #prompts the user to enter an integer.
    la $a0, prompt
    syscall


    li $v0, 5   # Reads in the integer
    syscall
    #Store the integer in $t0 since we need $v0 free
    move $t0,$v0

#Display message now.

    li $v0, 4
    la $a0, message     #prints the variable message.
    syscall

#Display the integer

    li $v0, 1
    move $a0,$t0        #move the contents of $t0 into the argument register.
    syscall
ggorlen
  • 44,755
  • 7
  • 76
  • 106

1 Answers1

2

You just simply need to add a syscall 10 at the end

li $v0, 10
syscall

A syscall with 10 in the $v0 register tells the computer that the program is done. So in your case you would want to have something like

.data
    prompt:  .asciiz "Enter a number: "
    message: .asciiz "\nNumber entered is: "

.text
    li $v0, 4  #prompts the user to enter an integer.
    la $a0, prompt
    syscall


    li $v0, 5   # Reads in the integer
    syscall
    #Store the integer in $t0 since we need $v0 free
    move $t0,$v0

    li $v0, 4
    la $a0, message     #prints the variable message.
    syscall

    li $v0, 1
    move $a0,$t0        #move the contents of $t0 into the argument register.
    syscall

    li $v0, 10          # Signal end of program
    syscall

you can even just have it under a label like end. That way whenever you want to end the program you can branch to end and then finishing code will be executed

end:
    li $v0, 10
    syscall
ConnerWithAnE
  • 1,106
  • 10
  • 26
  • 1
    *under a function head* - you mean "label". If you just `beq end` for example, that's just a normal branch, not a function call. You *could* look at it as a tailcall to a noreturn function (which doesn't care about getting a return address in `$ra`), but it makes more sense to just think of it as a conditional goto to a label. – Peter Cordes Apr 14 '21 at 23:54