1
votes

I'm trying to use mips assembly and I find issues with the branch mechanisms. In this "short" part of my code, but is where the error resides (I think). I't doesn't matter what number I type, will always jump to func1. Can someone help me? Thanks!

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
1
If syscall with $v0 == 6 is read_float, why do you expect ($v0) to be set to a valid adress? - EOF
What's this supposed to do: la $t0, ($v0) ? Why are you using syscall 6 (read_float) when the options are 1, 2, and 3 (which are all integers)? - Michael
That's exactly the error. Thank you! - Joao Rossetto

1 Answers

0
votes

You need to use li $v0, 5 to read an integer. Here's an example that does what you want.

.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