Search code examples
debuggingassemblymipsmars-simulator

Why is it removing first two words after loop runs? MIPS Mars Assembly


doing a project for university in MIPS Assembly.

I am doing a project and am storing 5 city names in a word called "array_cidades" For each city, I'm asking the user a int temperature, and checking if it is odd or even. I am using 3 arrays actually, and one index ($t0) to store and access each element.

My question is, why after this code runs, if I loop through my "array_cidades", it won't show me cidade_1 and cidade_2, like they have gone missing and I don't understand why...

.data

    array_temperaturas: .space 20 
    array_cidades: .word cidade_1, cidade_2, cidade_3, cidade_4, cidade_5
    array_even_odd: .word 20
    cidade_1: .asciiz "Porto - "
    cidade_2: .asciiz "Lisboa - "
    cidade_3: .asciiz "Faro - "
    cidade_4: .asciiz "Coimbra - "
    cidade_5: .asciiz "Viseu - "
    media: .asciiz "\nMedia: "
    minimo: .asciiz "\nMinimo: "
    maximo: .asciiz "\nMaximo: "
    nova_linha: .asciiz "\n"
    hashtag: .asciiz "#"
    estrela: .asciiz "*"
.globl main
.text
    main:
    # Index array = 0
    addi $t0, $zero, 0
    
    
    
    #For each temperature, gonna save it in the array, and save if it's odd or even
    foreach:
        bgt $t0, 16, limpar_index # Check if index > 16 (because 5 cities at [0,4,8,12,16])
        
        # Get city name to $t7
        lw $t7, array_cidades($t0)
        
        # Print city name at $t7
            li $v0, 4
            la $a0, ($t7)
            syscall
            
            # Get user temperature
            li $v0, 5
            syscall
            
            # Save user input in array of temperatures
        move $t6, $v0
        sw $t6, array_temperaturas($t0) 
        
        # Check if user input is odd or even, stores in $t5
        andi $t5 , $t6 , 0x0001 
        sw $t5, array_even_odd($t0) # Saves it in the array
            
            # Prints new line
            li $v0, 4
            la $a0, nova_linha
            syscall
                # Adds 4 to the array index (get next element)
                addi $t0, $t0, 4 # Index incrementado por 4 (proximo valor)
            j foreach

So, after this code runs it will branch to limpar_index tag. If i loop through, and print array_cidades, the first and second elements won't show up...

limpar_index:
   addi $t0, $zero, 0 # Index = 0

   test_start: # Printing the array_cidades
      bgt $t0, 16, text_exit
      
      # Print city
      li $v0, 4
      lw $a0, array_cidades($t0)
      syscall
        
      addi $t0, $t0, 4 #Increments array
        
      j test_start
        
text_exit: (...)

Any ideas why it's happening?


Solution

  • The first 2 names are overwritten by 0x00000000 or 0x00000001.

    array_even_odd: .word 20
    

    means to allocate 4 bytes and put a value 20 there.

    You may want to use

    array_even_odd: .space 20
    

    to allocate a 20-byte (5 4-byte elements) space.