2015-02-10 46 views
1

我一直在研究计算GCD和LCM的程序,然后将它们打印出来,但注意到我花了一半的代码来打印内容。我已经受够了组织通过保存文本块为:如何缩短MIPS打印语句

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

当然,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汇编程序或其他语言,则语法可能稍有不同。

+0

太棒了!这确实在MARS中起作用。不幸的是,我们的教授要求最终提交以MIPS运行。在100行的最后长度上这并不算太坏,但是可以肯定的是丢失一些填充,哈哈。 – 2015-02-10 19:08:21