MIPS组装中的空心方形

时间:2018-11-19 03:18:22

标签: assembly mips mars-simulator

我在使用以下MIPS汇编代码时遇到麻烦,我想做的是获取以下C代码,并在MIPS汇编(MARS 4.5)中创建它。

    /* Input number of rows from user */
    printf("Enter number of rows: ");
    scanf("%d", &N);

    /* Iterate over each row */
    for(i=1; i<=N; i++)
    {
        /* Iterate over each column */
        for(j=1; j<=N; j++)
        {
            if(i==1 || i==N || j==1 || j==N)
            {
                /* Print star for 1st, Nth row and column */
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }

        /* Move to the next line/row */
        printf("\n");
    }

    return 0;
}

到目前为止,这是我为MIPS代码所完成的工作。我看不到了解如何实现IF语句,这样我就可以得到如下输出:

Enter number of rows: 5 
*****
*   *
*   *
*   *
*****

MIPS代码:     主要:

#prompt1 and read character
    li $v0, 4
    la $a0, prompt1
    syscall

#Getting user's input as text
    li $v0, 8
    la $a0, userInput
    li $a1, 2   
    syscall

#prompt newline
    li $v0, 4
    la $a0, newline
    syscall

#prompt and read int
    li $v0, 4
    la $a0, prompt
    syscall


#read int and store in $t0
    li $v0, 5
    syscall
    move $t0, $v0 #store n in $t0
    move $t1, $v0 #store n in $t1

    bge $v0, 3, proceed #if input is 0 or more, goto proceed otherwise display error message and goto exit              
    li $v0, 4
    la $a0, errormsg
    syscall             

b exit

proceed:
#print n
    li $v0, 1
    move $a0, $t0   
    syscall
#prompt newline
    li $v0, 4
    la $a0, newline

for1:
    beq $t3, $t0, end_for1
    addi $t3, $t3, 1 # Increment counter
    li $t4, 0   #reseting j to 0 after each iteration of the for loop

######################## Inner loop          
for2:
    beq $t4, $t1, end_for2
    addi $t4, $t4, 1 # Increment counter
    b for2  
end_for2:
######################## Inner loop 
    b for1
end_for1:   


exit:
    li $v0, 10  
    syscall

.end main

0 个答案:

没有答案