2016-09-13 4 views
0

Ich schreibe ein Programm mit MIPS MARS-Assembly, das verschiedene mathematische Berechnungen durchführt, und ich habe Probleme zu verstehen, warum meine String-Werte im .data-Abschnitt als Block am Anfang des Programms statt einzelner Zeilen ausgedruckt werden. Ich habe auch Probleme damit, die Werte neben der korrekten Aussage auszudrucken.Warum werden in MIPS MARS meine verschiedenen Zeilen in einem Block anstatt in einzelnen Zeilen ausgedruckt?

.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 

Die Ergebnisse Ich erhalte:

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 

und die Ergebnisse i zu erhalten bin versucht:

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 

Kann mir jemand helfen, dies herauszufinden?

Antwort

3

Sie sollten die Zeilenvorschubzeichen \n nicht am Ende der Zeichenfolgen haben, die in Ihrem .data Segment definiert sind. Der Zeilenumbruch verschiebt die zukünftige Ausgabe in die nächste Zeile, sodass die Nummer, die Sie nach dem Zeichensatz ausdrucken, in die Zeile dahinter gestellt wird.

Sie sollten auch Null-terminierte Zeichenfolgen (.asciiz) für diese Zeichenfolgen verwenden. Deshalb erhalten Sie alle Anweisungen auf einmal ausdrucken; Der Code weiß nicht, wann der Ausdruck abgebrochen werden soll, da kein Abschlusszeichen vorhanden ist.

+1

Vielen Dank, dass alot geholfen! –

0

Aktualisiert korrigierten 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 
+1

Froh, dass die Dinge funktionieren. Aber ich würde einige Kommentare hinzufügen. Kommentare mit dem Algorithmus in einer Hochsprache (z. B. C) blockieren. Kommentare blockieren, die beschreiben, wie die Register verwendet werden. Und per Line-Sidebar Kommentare, die detailliert beschreiben, was der Algorithmus bei jedem Befehl macht. Für eine bessere Erklärung dessen, was ich meine, siehe meine Antwort: http://stackoverflow.com/questions/36538325/mips-linked-list/36560575#36560575 –

Verwandte Themen