简体   繁体   中英

Issue with loading and storing values in MIPS

I am trying to get user input 3 times, add them together, and print them out. However I want to learn how to use the.data segment and was wondering where I am going wrong? I get the input values, and then when i go to print the result i get some huge number instead of 15...

#Name: LoadStore.asm
#Author: Logan Patterson
#Date: 02-12-2021
#Purpose: Ask the uses for numbers and store them

.data
    val1: .word 
    val2: .word
    val3: .word
    results: .word
    prompt: .asciiz "Please Enter the first number"
    prompt2: .asciiz "Please enter the second number"
    prompt3: .asciiz "Please enter the third number"
    result: .asciiz "The result is: "
    
    
.text
main:
    li $v0, 4 #To issue a print string call
    la $a0, prompt  #Load the prompt to print
    syscall
    
    li $v0, 5   #To issue a sycall to read int
    syscall
    sw $v0, val1    #Move value into val1
    
    li $v0, 4 #To issue a print string call
    la $a0, prompt2 #Load the prompt to print
    syscall
    
    li $v0, 5   #To issue a sycall to read int
    syscall
    sw $v0, val2    #Move value into val2
    
    li $v0, 4 #To issue a print string call
    la $a0, prompt3 #Load the prompt to print
    syscall
    
    li $v0, 5   #To issue a sycall to read int
    syscall
    sw $v0, val3    #Move value into val3
    
    lw $s0, val1
    lw $s1, val2
    lw $s2, val3
    
    add $s3, $s0, $s1
    add $s3, $s3, $s2
    sw $s3, results
    
    li $v0, 4
    la $a0, result
    syscall
    
    li $v0, 1
    la $a0, results
    syscall
    
    li $v0, 10
    syscall
    
    

I have modified the code. It is running perfectly fine.

.data
    val1:   .word  0
    val2:   .word  0
    val3:   .word  0
    results:    .word  0
    prompt:     .asciiz "Please Enter the first number: "
    prompt2:    .asciiz "Please enter the second number: "
    prompt3:    .asciiz "Please enter the third number: "
    resultValue:    .asciiz "The result is: "
    
    
.text
main:
    li $v0, 4           #To issue a print string call
    la $a0, prompt      #Load the prompt to print
    syscall
    
    li $v0, 5           #To issue a sycall to read int
    syscall
    sw $v0, val1        #Move value into val1
    
    li $v0, 4       #To issue a print string call
    la $a0, prompt2     #Load the prompt to print
    syscall
    
    li $v0, 5        #To issue a sycall to read int
    syscall
    sw $v0, val2     #Move value into val2
    
    li $v0, 4       #To issue a print string call
    la $a0, prompt3     #Load the prompt to print
    syscall
    
    li $v0, 5   #To issue a sycall to read int
    syscall
    sw $v0, val3    #Move value into val3
    
    lw $s0, val1
    lw $s1, val2
    lw $s2, val3

    li $v0, 4
    la $a0, resultValue
    syscall
    
    add $s3, $s0, $s1
    add $s3, $s3, $s2
    sw $s3, results
   
    li $v0, 1
    lw $a0, results
    syscall
    
    li $v0, 10
    syscall

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM