2015-02-10 45 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