为什么我的不同字符串行在一个块中打印而不是MIPS MARS中的单独行?

时间:2016-09-13 17:09:32

标签: assembly mips mars

我正在使用MIPS MARS程序集编写程序,该程序执行不同的数学计算,并且我无法理解为什么.data部分中的字符串值在程序开头打印为块而不是单独的线条。我也在将值打印到正确的语句旁边时遇到问题。

 .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 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

结果我试图获得:

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

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:3)

您的\n细分中定义的字符串末尾不应包含换行符.data。换行符将未来的输出推送到下一行,因此您在字符串后面打印的数字将被放置在其后的行上。

您还应该对这些字符串使用以null结尾的字符串(.asciiz)。这就是为什么你要立刻打印所有的声明;代码不知道何时停止打印,因为没有终止字符。

答案 1 :(得分:0)

更新了更正后的代码:

.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