将Hi和Lo读入两个临时寄存器

时间:2016-02-25 07:53:46

标签: assembly registry mips swap

我试图制作一个宏,将HI和LO的值存储到两个临时寄存器然后交换它们但我想知道如何存储它们我使用mfhi和mflo来保存它们吗?如果是这样我将如何交换它们?

.macro print_str($arg)
        li $v0, 4 #System call code for print_str
        la $a0, $arg #Address of the string to print
        syscall     # print the string
        .end_macro

#macro : print_int
#usage : rint_int(<val>)                
.macro print_int($arg)
        li $v0, 1   # system call code for print_int
        li $a0, $arg   # integer to print
        syscall     # print the integer
        .end_macro 

.macro read_int($reg)
    li $v0, 5 #system call code to read_int
    syscall
    move $reg, $v0
    .end_macro


.macro print_reg_int($reg)
    li $v0, 1
    move $a0, $reg
    syscall
    .end_macro

.macro swap_hi_lo($temp1, $temp2)
    mtlo $a0
    mflo $temp1
    mthi $a0
    mfhi $temp2




.macro print_hi_lo($strHi, $strEqual, $strComma, $strLo)
       li $v0, 4
       la $a0, $strHi  #Address of the string to print
       syscall
       li $v0, 4
       la $a0, $strEqual    #Address of the string to print
       syscall
       li $v0, 4
       la $a0, $strComma #Address of the string to print
       syscall


 .macro exit
    li  $v0, 10 
    syscall
    .end_macro

1 个答案:

答案 0 :(得分:0)

考虑到你最有可能参加同一个CS课程。我想我可以帮你解决一下这些代码。阅读评论!下面的代码说明。

.macro swap_hi_lo($temp1, $temp2)
mfhi $temp1 #Set address of $temp1 to address of value stored in Hi
mflo $temp2 #Set address of $temp2 to address of value stored in Lo
move $t2, $temp1 #Move $temp1 to a temporary address for manipulation
move $t3, $temp2 #Move $temp2 to a temporary address for manipulation
mthi $t3 #Swap value of Hi with our Lo value
mtlo $t2 #Swap value of Lo value with our Hi value

syscall
.end_macro

说明:

  • 我们将$ temp1和$ temp2的值设置为hi和lo值的值,因为测试文件中提供的参数本身是空的临时值(因为我必须在一小时后学习很难)
  • 接下来,我们将$ temp1和$ temp2的值移动到两个临时变量,这样我们就可以交换这两个值而不会发生任何奇怪的重叠。
  • 最后,我们将HI和LO值分配给相应的临时值。

另一个提示:

当您为第二个宏打印HI和LO的值时,请使用mfhi和mflo。如果您需要更多帮助,请随时询问!

相关问题