Home > Software design >  Having trouble printing strings in MIPS using (qtSPIM)
Having trouble printing strings in MIPS using (qtSPIM)

Time:07-08

Basically whenever I print after a certain section of code, in this case the loop, the output in SPIM's console is either an empty character (looks like a hollow square) or an empty string (several spaces). Need some help figuring out what I'm missing...

.data

arrayY: .word 1, 2, 3, 4, 5                # array of 5 elements
arrayZ: .word -5, -4, -3, -2, -1                # array of 5 elements
arrayX: .word 20                            # init array 5 elements
var1: .asciiz "X: "
var2: .asciiz ", "

.text
.globl main

main:
    addi $a2, $zero, 5      # init n
    addi $s0, $zero, 2      # init s
    addi $s1 $zero, 1       # init t

    addi $sp, $sp, -12
    sw $s1, 4($sp)          # push to stack
    sw $s0, 0($sp)
    jal AVA



AVA:
    li $v0, 0
    li $v1, 0
    addi $t1, $a2, -1
    addi $t1, $zero, 20
    addi $t0, $zero, 0
    LOOP:
        beq $t0, $t1, exit
        lw $t2, arrayY($t0)
        lw $t3, arrayZ($t0)
        subu $t4, $zero, $t3
        add $t5, $t4, $t2 
        addi $t3, $zero, 2
        mult $t3, $s0
        add $t6, $t3, $s1

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

        add $t7, $t5, $t6
        sw $t7, arrayX($t0)

        addi $t0, $t0, 4
        j LOOP
    exit:
    addi $t0, $zero, 0
    move $a0, $0
    move $v0, $0
    li $v0, 4
    la $a0, var1
    syscall
    LOOP1:
        beq $t0, $t1, exit1
        lw $t2, arrayY($t0)
        move $a0, $t2
        li $v0, 1
        syscall
        addi $t0, $t0, 4
        j LOOP1
    exit1:






    
    li $v0, 10          # exit
    syscall
    

Expected output :

X: 12345

Actual output :

     12345

If I print the string earlier in the code, before both loops it prints the string as expected. I'm new to MIPS and thought maybe I had to reset the registers after the loop or something but that didn't do anything differently.

CodePudding user response:

So, do this at the beginning:

.data
.globl arrayX
.globl arrayY
.globl arrayZ
.globl arrayX
.globl var1
.globl var2

arrayY: .word 1, 2, 3, 4, 5                # array of 5 elements
arrayZ: .word -5, -4, -3, -2, -1                # array of 5 elements
arrayX: .word 20                            # init array 5 elements
var1: .asciiz "X: "
var2: .asciiz ", "

Then use Simulator menu -> Display Symbols.  Do this menu item before the first single step — these symbols don't move during execution, so get to know your data layout before the first instruction knowing they won't change from there.

Using that information you can track where all your data items are and how big they are, what comes after what, etc...  Then you can make more sense of who is overwriting what, and perhaps why!

  • Related