繁体   English   中英

如何缩短MIPS打印语句

[英]How to Shorten MIPS Print Statements

我一直在研究一个计算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行,而且效率极低。 必须有更好的方法,对吗?

当然,为其定义一个宏

    .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汇编器或其他工具,则语法可能会略有不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM