Asian_Panda
Asian_Panda

Reputation: 3

Why are my different lines of string printing out in one block instead of individual lines in MIPS MARS?

I'm writing a program using MIPS MARS assembly that does different math calculations and I'm having trouble understanding why my string values in the .data section are printing out as a block in the beginning of the program instead of individual lines. I'm also having issues with getting the values to print out next to the correct statement.

 .data
   NL:  .asciiz "\n" #NL=new line varible kinda name
   addition: .ascii "The value of a + b = \n"
   subtraction: .ascii "The value of a - b = \n "
   prob_3: .ascii "The value of (a + b) - 8 =  \n"
   prob_4: .ascii "The value of (a + b) - (c + d) =  \n"
   prob_5: .ascii "The value of ((a + b) + (d - c) + 17 =  \n"

.text

  li $s0, 8
  li $s1, 8
  li $s2, 16
  li $s3, 8

  la $a0, addition 
  li $v0, 4 
  syscall 
  add $t1, $s0, $s1
  li $v0, 1
  add $a0, $t1, $zero
  syscall

  la $a0, NL
  li $v0, 4
  syscall

  la $a0, subtraction 
  li $v0, 4 
  syscall 
  sub $t2, $s0, $s1
  li $v0, 1
  sub $a0, $t2, $zero
  syscall

  la $a0, NL
  li $v0, 4
  syscall

  la $a0, prob_3 
  li $v0, 4
  syscall 
  subi $t3, $t1, 8
  li $v0, 1
  sub $a0, $t3, $zero
  syscall

  la $a0, NL
  li $v0, 4
  syscall

  la $a0, prob_4 
  li $v0, 4  
  syscall 
  add $t4, $s2, $s3
  sub $t5, $t1, $t4
  li $v0, 1
  sub $a0, $t5, $zero
  syscall

  la $a0, NL
  li $v0, 4
  syscall

  la $a0, prob_5
  li $v0, 4  
  syscall 
  sub $t6, $s3, $s2
  add $t7, $t1, $t6
  addi $t8, $t7, 17
  li $v0, 1
  add $a0, $t8, $zero
  syscall

The results I'm getting:

The value of a + b = 
The value of a - b = 
The value of (a + b) - 8 =  
The value of (a + b) - (c + d) =  
The value of ((a + b) + (d - c) + 17 =  
16
The value of a - b = 
The value of (a + b) - 8 =  
The value of (a + b) - (c + d) =  
The value of ((a + b) + (d - c) + 17 =  
0
The value of (a + b) - 8 =  
The value of (a + b) - (c + d) =  
The value of ((a + b) + (d - c) + 17 =  
8
The value of (a + b) - (c + d) =  
The value of ((a + b) + (d - c) + 17 =  
-8
The value of ((a + b) + (d - c) + 17 =  
25

and the results i'm trying to get:

The value of a + b = 16
The value of a - b = 0 
The value of (a + b) - 8 = 8  
The value of (a + b) - (c + d) = -8
The value of ((a + b) + (d - c) + 17 = 25

Can someone help me figure this out?

Upvotes: 0

Views: 1665

Answers (2)

Asian_Panda
Asian_Panda

Reputation: 3

Updated corrected code:

.data
    NL:  .asciiz "\n" #NL=new line varible kinda name
    prob_1: .asciiz "The value of a + b = "
    prob_2: .asciiz "The value of a - b =  "
    prob_3: .asciiz "The value of (a + b) - 8 =  "
    prob_4: .asciiz "The value of (a + b) - (c + d) =  "
    prob_5: .asciiz "The value of ((a + b) + (d - c) + 17 =  "

.text

  li $s0, 8
  li $s1, 8
  li $s2, 16
  li $s3, 8

  la $a0, prob_1 
  li $v0, 4 
  syscall 
  add $t1, $s0, $s1
  li $v0, 1
  add $a0, $t1, $zero
  syscall

  la $a0, NL
  li $v0, 4
  syscall

  la $a0, prob_2 
  li $v0, 4 
  syscall 
  sub $t2, $s0, $s1
  li $v0, 1
  sub $a0, $t2, $zero
  syscall

  la $a0, NL
  li $v0, 4
  syscall

  la $a0, prob_3 
  li $v0, 4
  syscall 
  subi $t3, $t1, 8
  li $v0, 1
  sub $a0, $t3, $zero
  syscall

  la $a0, NL
  li $v0, 4
  syscall

  la $a0, prob_4 
  li $v0, 4  
  syscall 
  add $t4, $s2, $s3
  sub $t5, $t1, $t4
  li $v0, 1
  sub $a0, $t5, $zero
  syscall

  la $a0, NL
  li $v0, 4
  syscall

  la $a0, prob_5
  li $v0, 4  
  syscall 
  sub $t6, $s3, $s2
  add $t7, $t1, $t6
  addi $t8, $t7, 17
  li $v0, 1
  add $a0, $t8, $zero
  syscall

Upvotes: 0

Tyler Kindy
Tyler Kindy

Reputation: 89

You shouldn't have the newline characters \n at the end of the strings defined in your .data segment. The newline pushes future output to the next line, so the number you print out following the string gets placed on the line after it.

You should also be using null-terminated strings (.asciiz) for those strings. That's why you're getting all the statements printing out at once; the code doesn't know when to stop printing out because there's no termination character.

Upvotes: 3

Related Questions