如何在mips中使用bne?

时间:2017-02-04 22:23:58

标签: conditional mips

所以我试图像我的代码中的语句一样跳转到标签。从我的理解bne是不等于去指定标签。但问题是我的打印报表没有得到任何输出。我用错了吗?我为丑陋的代码道歉,这是我第一次使用mips。

# Arthor: Christian Soto
#
# Simple conditional statements

.data
 MSG: .asciiz "Copying from alpha to charlie.  Before: "
 string2: .asciiz " After: " 
 string3: .asciiz " alpha was the same as bravo."
 string4: .asciiz "alpha was the same as charlie."
 string5: .asciiz "bravo was the same as charlie."
 string6: .asciiz "\n"
 string7: .asciiz "alpha-bravo-charlie="
 my_var: .word 1
 copy:    .word 2
 dups: .word 1  
 alpha: .word 2
 charlie: .word 2
 bravo: .word 2

.text

.globl studentMain
  studentMain:
    addiu $sp, $sp, -24 # allocate stack space -- default of 24 here
    sw $fp, 0($sp) # save caller’s frame pointer
    sw $ra, 4($sp) # save return address 
    addiu $fp, $sp, 20 # setup main’s frame pointer

  #Load vaiables

  #set $s0 = copy
  la $t0, copy
  lw $s0, 0($t0)

  #set $s1 = dups
  la $t0, dups
  lw $s1, 0($t0)

  #set $s2 = subtract
  #la $t0, subtract
  #lw $s2, 0($t0)

  #set $s3 = print
  #la $t0, print
  #lw $s3, 0($t0)

  #set $s4 = alpha
  la $t0, alpha
  lw $s4, 0($t0)

  #set $s5 = bravo
  la $t0, bravo
  lw $s5, 0($t0)

  #set $s6 = charlie
  la $t0, charlie
  lw $s6, 0($t0)

  #set $s7 = my_var
  la $t0, my_var
  lw $s7, 0($t0)

  #Task 1: copy
  bne $s7, $s0, DO_DUPS #if they do not equal then do dups

  li $v0, 4
  la $a0, MSG
  syscall

  la $t0, charlie   #t0 = &charlie
  lw $t0, 0($t0)    #t0 = charlie

  li $v0, 1
  la $a0, ($t0)
  syscall

  li $v0, 4
  la $a0, string2
  syscall

  la $t1, alpha  #t1 = $alpha
  lw $t2, 0($t1)  #t2 = alpha
  add $s6, $zero, $t2  #s6 = t2

  li $v0, 1
  la $a0, ($s6)
  syscall
  #Task 2: Dups
  DO_DUPS: 
    bne $s7, $s1, DO_SUBTRACT #if they do not equal then do subtract

    bne $s4, $s5, DO_NEXT #compare alpha and bravo else print message
    li $v0, 4
    la $a0, string3

DO_NEXT:      
  bne $s4, $s6, DO_NEXT2 #compare alpha and charlie else print message
  li $v0, 4
  la $a0, string4

DO_NEXT2:
  bne $s5, $s6, DO_SUBTRACT #compare charlie and bravo else print message
  li $v0, 4
  la $a0, string5

  DO_SUBTRACT:     
    #Task 3: Subtract


  #Task 4: Print

  j DONE  
   DONE:   # Epilogue for main -- restore stack & frame pointers and return
   lw    $ra, 4($sp)     # get return address from stack
   lw    $fp, 0($sp)     # restore the caller's frame pointer
   addiu $sp, $sp, 24    # restore the caller's stack pointer
   jr    $ra             # return to caller's code

1 个答案:

答案 0 :(得分:0)

我忘了添加一个系统调用来查看结果,但我现在修复了