在MIPS中打印整数后的下一行

时间:2017-02-08 21:25:20

标签: assembly mips

我尝试在打印整数后在新行中打印字符串,但在我的程序中,它打印在同一行,我不知道如何解决它。这是我的代码:

struct number {
    unsigned char *digits; 
    size_t length; 
};

// the function returns if biggerNumber is trully bigger than lesserNumber;
// if it returns false, the computed result is wrong (and the original value was lost, you better fix that!)
bool subtractionTwoBinaryNumbers(number* biggerNumber, number* lesserNumber) 
{
    for (size_t i = biggerNumber->length - 1; i >= 0; i--) 
    {
        if (biggerNumber->digits[i] < lesserNumber->digits[i])
        {
            if (i > 1)
            {
                // I'll carry an unit from the higher digit, in order to do the substraction.
                // as in: 12-9 = 0(12)-0(9) = 0(3)-0(0) = 3
                unsigned char aux = -1; // -1 actually means 0b11111111
                aux -= lesserNumber->digits[i];
                biggerNumber->digits[i] += aux;
                --(biggerNumber->digits[i-1]);  
            }
            else
                return false;
        }
        else
            biggerNumber->digits[i] -= lesserNumber->digits[i];
    }

    return true;
}

这是我的输出:

.data                           
.asciiz "My Name\n"                 # 0-10
.asciiz "Enter the number of times to repeat "      # 11-47
.asciiz "Enter the first number "           # 48-71
.asciiz "Enter the second number "          # 72-96
.asciiz "The sum is "                   # 97-108
.asciiz "First number is greater than the second\n" # 109
.globl main
.text
main:
    lui $ra, 0x1001     # init
    lui $a0, 0x1001     # init
    ori $a0, $a0, 0     # init
    ori $s1, $s1, 1
    addi $t4, $zero, 1  # to be used in loop
    ori $v0, $zero, 4   # to print string
    jal print       # print name    
    addi $a0, $a0, 11   # Enter num to repeat
    ori $v0, $zero, 4   # to print string
    jal print       # 
    jal read        #
    add $t0, $v0, $zero # stores amount to repeat in $t0
repeat:             # to be repeated by what the user inputted
    addi $a0, $a0, 48   # Enter first num
    ori $v0, $zero, 4   # to print string
    jal print       #
    jal read        #
    add $t1, $v0, $zero # stores first num input in $t1
    addi $a0, $a0, 72   # Enter second num
    ori $v0, $zero, 4   # to print string
    jal print       #
    jal read        #
    add $t2, $v0, $zero # stores second num input in $t2
    jal eval        #
    addi $t4 $t4, 1     # adds 1
    slt $t3, $t0, $t4   # set $t3 1 if $t0 < $t4
    beq $t3, $s0, repeat    # if $t3 = $s0 (0) then repeat
    ori $v0, $zero, 10  # to exit program
    jal print       # exits program

print:              # syscall function
    syscall         # calls system
    beq $a2, $zero, return  # if $a2 = 0 then returns early
    add $a0, $a1, $zero # puts the integer in $a1 to $a0 to be print
    ori $v0, $zero, 1   # to print integer
    syscall         # prints integer
    j return        #
return:             # returns to last jal instruction
    sub $a2, $a2, $a2   # clears $a2
    sub $a0, $a0, $a0   # clears $a0
    lui $a0, 0x1001     # re-init
    jr $ra
read:               # reads the input of the user
    addi $v0, $zero, 5  # to read input
    syscall         # reads user input
    j return
sum:                # calculates the sum
    add $a1, $t1, $t2   # integer to be print
    sub $a2, $a2, $a2   # clears $a2
    addi $a2, $zero, 1  # 1 to $a2 to print integer
    ori $a0, $a0, 97    # to print the sum is string
    ori $v0, $zero, 4   # to print string
    j print
eval:               # evaluates to go to sum or print error
    slt $s2, $t2, $t1   # $t2 < $t1
    beq $zero, $s2, sum # if $s2 is 0 then goto sum
    ori $a0, $a0, 109   # prints first num greater than second
    ori $v0, $zero, 4   # to print string
    j print

显然,我想打印字符串&#39;输入第一个数字&#39;在&#39;总和为10&#39;之后的新行中。字符串,&#39;总和是&#39;首先打印,然后打印整数,&#39; 10&#39;之后打印。在整数&#39; 10&#39;之后打印,我希望下一个字符串在新行或下一行打印。我不知道如何正确地做到这一点。有人能帮帮我吗?

0 个答案:

没有答案