Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and printing an integer in mips

My program is suppose to read an integer and print it back to the user but every time it just prints 268501230 no matter what is entered. Any help would be appreciated.

.data
prompt2: .asciiz "Please enter value: "
array1: .space 40
array2: .space 40
buffer: .space 4
.text

main: 

#Prints the prompt2 string
li $v0, 4
la $a0, prompt2 
syscall 

#reads one integer from user and saves in t0
li $v0, 5
la $t0, buffer
syscall

li $v0, 1       
li $t0, 5       # $integer to print
syscall         

exitProgram:    li $v0, 10  # system call to
    syscall         # terminate program
like image 788
user2837034 Avatar asked Nov 02 '13 23:11

user2837034


People also ask

What is an integer in MIPS?

a character requires 1 byte of storage. an integer requires 1 word (4 bytes) of storage.

What syscall code would you use to print an integer?

Luckily, both of these operations can be done with a syscall. Looking again in table from lecture 7, we see that syscall 5 can be used to read an integer into register $v0, and and syscall 1 can be used to print out the integer stored in $a0.


2 Answers

This is how I will write a program to get an integer input and to print it out

.data

     text:  .asciiz "Enter a number: "

.text

 main:
    # Printing out the text
    li $v0, 4
    la $a0, text
    syscall

    # Getting user input
    li $v0, 5
    syscall

    # Moving the integer input to another register
    move $t0, $v0

    # Printing out the number
    li $v0, 1
    move $a0, $t0
    syscall

    # End Program
    li $v0, 10
    syscall
like image 191
aspire29 Avatar answered Sep 27 '22 20:09

aspire29


#reads one integer from user and saves in t0
li $v0, 5
la $t0, buffer
syscall

That's not how syscall 5 works. The integer is returned in $v0, so the code ought to be something like:

li $v0,5
syscall
move $t0,$v0

li $v0, 1       
li $t0, 5       # $integer to print
syscall 

You're using the wrong register here as well. The integer to print should go into $a0, not $t0.

Here's a list of syscalls and the registers they use.

like image 24
Michael Avatar answered Sep 27 '22 19:09

Michael