【问题标题】:Assembly mips branch error装配 mips 分支错误
【发布时间】:2016-06-04 05:22:51
【问题描述】:

我正在尝试使用 mips 程序集,但发现分支机制存在问题。在我的代码的这个“短”部分中,但是错误所在的位置(我认为)。不管我输入什么数字,我都会跳到func1。 有人能帮我吗? 谢谢!

代码:

.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

【问题讨论】:

  • 如果syscall$v0 == 6read_float,你为什么期望($v0) 设置为有效地址?
  • 这是做什么的:la $t0, ($v0)? Why are you using syscall 6 (read_float) when the options are 1, 2, and 3 (which are all integers)?
  • 这正是错误所在。谢谢!

标签: assembly branch mips


【解决方案1】:

您需要使用li $v0, 5 来读取整数。这是一个可以满足您要求的示例。

.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  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多