-1

I am facing a problem trying to print out an array of 100 characters in ten lines (ten characters per line) in order to form a square shaped text using Assembly language MIPS. the code is printing the first ten characters right and then error start.

I really appreciate any help or advice

here is the code :

#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

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

and here is my code :

    #########################################
### 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 ###
#########################################
LazerDance
  • 177
  • 1
  • 8
  • what is *"and then error start"*? – Ped7g Mar 28 '18 at 18:59
  • Tried in MARS, and the code does not print first ten characters, it overwrites first 4 chars of board definition with zero, then it maybe outputs something (MARS doesn't show anything), then it will crash for obvious reasons. While on contrary your code doesn't make any obvious sense, add some comments ahead of each group of instructions, what was their purpose, for example why do you store value in `$s0` over original board definition. Show some understanding of what you want to do and how, then it will be possible to suggest fixes to code, at this moment just delete it and start again. – Ped7g Mar 28 '18 at 19:17
  • BTW, who's author of `print:` subroutine? If your lector, then there's something very wrong about your course (but I guess it's some student's work... which shouldn't have passed into hands of beginner, so your lector is still guilty either way). – Ped7g Mar 28 '18 at 19:39

1 Answers1

1

Start from a clean approach. Prototype it in a higher level language. Here's a C function to start from:

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;
        }
    }
}
Craig Estey
  • 30,627
  • 4
  • 24
  • 48