如何缩短MIPS打印语句

时间:2015-02-10 18:31:37

标签: assembly mips

我一直在研究计算GCD和LCM的程序,然后将它们打印出来,但是我注意到我花了一半的代码只打印东西。我通过将文本块保存为:

来组织它
Ask_Input_1:
.asciiz "Enter first integer n1: "
Ask_Input_2:
.asciiz "Enter second integer n2"
GCD_Out:
.asciiz "The greatest common divisor of : "
LCM_Out:
.asciiz "The least common multiple of "
AND:
.asciiz " and "
IS:
.asciiz " is "

然后用:

打印它们
la, $a0, GCD_Out
li $v0, 4
syscall                 #print statement
la, $a0, ($s0)
li $v0, 1
syscall                 #print first number
la $a0, AND
li $v0, 4
syscall                 #print and
la $a0, ($s1)
li $v0, 1               #print second number
la $a0, IS
li $v0, 4
syscall                 #print is

每个函数大约需要10行,并且效率极低。必须有更好的方法,对吧?

1 个答案:

答案 0 :(得分:1)

当然,define a macro为它:

    .macro print_str (%str)
    .data
myLabel: .asciiz %str
    .text
    li $v0, 4
    la $a0, myLabel
    syscall
    .end_macro

.text
.globl main
main:

    print_str("Enter first integer n1: ")
    print_str("Enter second integer n2: ")
    print_str("The greatest common divisor of : ")

    li $v0,10
    syscall

SPIM似乎不喜欢那个宏,但它在MARS中运行良好。如果你正在使用GNU汇编程序或其他东西,语法可能会略有不同。