mips程序集在一个新行中打印数组中的每十个字符

时间:2018-03-28 17:33:46

标签: assembly mips mips32

我在尝试打印10行(每行10个字符)的100个字符的数组时遇到问题,以便使用汇编语言MIPS形成方形文本。 代码正在打印前十个字符然后错误开始。

我非常感谢任何帮助或建议

这是代码:

#v20180326# 
# Prints string to stdout with length $a1
#
# $a0 -> string
# $a1 -> length
#
print:
# save s-Regs
addi $sp, $sp, -8
sw $s0, 0($sp)
sw $s1, 4($sp)

# save params
move $s0, $a0
move $s1, $a1

li $s1, 15

# alloc buffer (with double word alignment)
addi $s1, $s1, 1
andi $t0, $s1, 7
li $t1, 8
sub $t1, $t1, $t0
andi $t1, $t1, 7
add $s1, $s1, $t1
sub $sp, $sp, $s1

# copy string
li $t0, 0
loop_copy:
bge $t0, $a1, end_copy

add $t1, $s0, $t0
lb $t2, 0($t1)
add $t3, $sp, $t0
sb $t2, 0($t3)

addi $t0, $t0, 1
j loop_copy

end_copy:
# null terminated
add $t3, $sp, $t0
sb $zero, 0($t3)

# print buffer
move $a0, $sp
li $v0, 4
syscall

# free buffer
add $sp, $sp, $s1

# restore s-Regs
lw $s0, 0($sp)
lw $s1, 4($sp)
addi $sp, $sp, 8
jr $ra

#
# Prints a single linefeed (\n) to stdout
#

print_lf:
li $a0, 10
li $v0, 11
syscall

jr $ra

#########################################
### ATTENTION: DO NOT CHANGE THE FOLLOWING LINE !!
### sed-hook ###
### Implement your solution below
#########################################

#
# Writes the board nicely line by line
#

write_board:
### TODO Implement your solution here ###


#########################################
### Implement your solution above
### ATTENTION: DO NOT CHANGE THE FOLLOWING LINE !!
### sed-hook ###
#########################################

# data
.data
size:
.word       10

#   board:          .asciiz     "oo  x xx o x  - o o  x  x  xxx x  -  oo   o             
- o -  oo o  o- xxxx  x -    o  -   xx    -     ooo - xx"
#   board:          .asciiz     "                                                                                                        
"
board:
.asciiz     "    x xx   x  x      x  x  xxx x  x         x   x         x     
xxxx  x x       x   xx    x         x xx"

#########################################

# main
.text
.globl main

main:
# save regs
addi $sp, $sp, -4
sw $ra, 0($sp)

# call function
jal write_board

# restore regs
lw $ra, 0($sp)
addi $sp, $sp, 4

jr $ra

#########################################

这是我的代码:

    #########################################
### ATTENTION: DO NOT CHANGE THE FOLLOWING LINE !!
### sed-hook ###
### Implement your solution below
#########################################

#
# Writes the board nicely line by line
#

write_board:
### TODO Implement your solution here ###

addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)    

addi $t0, $zero, 0

for:
la $a0, board
lw $a1, size
sw $s0, board($t0)
addi $t0, $t0, 4
bgt $t0, 99, exit
jal print
jal print_lf
j for  

exit:
li $v0, 10
syscall

lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra

#########################################
### Implement your solution above
### ATTENTION: DO NOT CHANGE THE FOLLOWING LINE !!
### sed-hook ###
#########################################

1 个答案:

答案 0 :(得分:1)

从干净的方法开始。用更高级别的语言对其进行原型化。这是一个从以下开始的C函数:

void
write_board(void)
{
    int chrcnt;
    int val;
    char *bp;

    bp = board;
    chrcnt = 0;

    while (1) {
        // fetch the next char
        val = *bp++;
        if (val == 0)
            break;

        // output it
        putchar(val);

        // count number of chars on line
        chrcnt += 1;

        // output newline when limit reached
        if (chrcnt == 10) {
            putchar('\n');
            chrcnt = 0;
        }
    }
}