2016-06-04 8 views
1

Ich versuche, Mips-Assembly zu verwenden, und ich finde Probleme mit den Zweigmechanismen. In diesem "kurzen" Teil meines Codes, aber dort, wo der Fehler liegt (denke ich). Es spielt keine Rolle, welche Nummer ich tippe, ich werde immer zu func1 springen. Kann mir jemand helfen? Danke!Assembly mips branch error

Code:

.text 
.globl main 

main: 
.data 
param: .float 0 
val_1: .float 1 
val_2: .float 2 
val_3: .float 3 
texto: .asciiz "type 1 for func 1, 2 for func 2 and 3 for func 3: \n" 

.text 

la $a0, texto  #print 
li $v0, 4 
syscall 
li $v0, 6   #read 
syscall 
la $t0, ($v0)  #from $v0 to $t0 

beq $t0, 1, func1  #branch for func1 
beq $t0, 2, func2  #branch for func2 
beq $t0, 3, func3  #branch for func3 
j end 
+0

Wenn 'syscall' mit' $ v0 == 6 '' ist read_float', warum Sie erwarten, '($ v0)' auf eine gültige Adresse festgelegt werden? – EOF

+0

Was soll das tun: 'la $ t0, ($ v0)'? Warum benutzen Sie syscall 6 ('read_float') wenn die Optionen 1, 2 und 3 sind (was sind ganze Zahlen)? – Michael

+0

Das ist genau der Fehler. Vielen Dank! –

Antwort

0

Sie müssen li $v0, 5 verwenden, um eine ganze Zahl zu lesen. Hier ist ein Beispiel, das das tut, was Sie wollen.

.text 
.globl main 

main: 
.data 
param: .float 0 
val_1: .float 1 
val_2: .float 2 
val_3: .float 3 
texto: .asciiz "type 1 for func 1, 2 for func 2 and 3 for func 3: \n" 
texto2: .asciiz "option 1 chosen \n" 
texto3: .asciiz "option 2 chosen \n" 
texto4: .asciiz "option 3 chosen \n" 
.text 

la $a0, texto  #print 
li $v0, 4 
syscall 
li $v0, 5   #read 
syscall 
la $t0, ($v0)  #from $v0 to $t0 

beq $t0, 1, func1  #branch for func1 
beq $t0, 2, func2  #branch for func2 
beq $t0, 3, func3  #branch for func3 
j end 


func1: 
    la $a0, texto2  #print 
    li $v0,4  # syscall with v0 = 11 will print out 
    syscall   # one byte from a0 to the Run I/O window 
    j end 

func2: 
    la $a0, texto3  #print 
    li $v0,4  # syscall with v0 = 11 will print out 
    syscall   # one byte from a0 to the Run I/O window 
    j end 

func3: 
    la $a0, texto4  #print 
    li $v0,4  # syscall with v0 = 11 will print out 
    syscall   # one byte from a0 to the Run I/O window 
    j end 
end: 
    li $v0,10 
    syscall