简体   繁体   中英

How to Shorten MIPS Print Statements

I've been working on a program that calculates a GCD and LCM and then prints them out but have noticed I spend half of my code just printing stuff. I've had it organized by saving the text blocks as:

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 "

and then printing them with:

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

This takes about 10 lines per each function, and seems super inefficient. There has to be a better way, right?

Sure, define a macro for it:

    .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 doesn't seem to like that macro, but it works fine in MARS. If you're using the GNU assembler or something else, the syntax may be slightly different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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