Uninitialised address in MIPS assembly (Basic)

Lex
# Reads 10 numbers into an array
# printing 0 if they are in non-decreasing order
# or 1 otherwise.

# Constants
ARRAY_LEN = 10

main:

scan_loop__init:
    li  $t0, 0              #   i = 0;
scan_loop__cond:
    bge $t0, ARRAY_LEN, scan_loop__end  # while (i < ARRAY_LEN) {
scan_loop__body:
    mul $t1, $t0, 4         #   calculate &numbers[i] == numbers + 4 * i
    la  $t2, numbers        #
    add $t2, $t2, $t1       #

    li  $v0, 5              #   syscall 5: read_int
    syscall                 #   
    sw  $v0, ($t2)          #   scanf("%d", &numbers[i]);

    addi    $t0, $t0, 1     #   i++;
    j   scan_loop__cond     #   loops whole thing
scan_loop__end:
    li  $t3, 0              #   int swapped = 0;
    li  $t0, 1              #   i = 1;
check_condition:
    bgt $t0, ARRAY_LEN, print_finish

    mul $t1, $t0, 4         #   multiplying
    la  $t2, numbers        #   loading first address of numbers
    add $t2, $t2, $t1
    lw  $t4, ($t2)          #   load numbers[i]

    sub $t2, $t2, 4
    lw  $t5, ($t2)          #   load numbers[i - 1]

    add $t0, $t0, 1         #   i++

    blt $t4, $t5, change    #   changes value when x < y
    j check_condition
change:                     #   loop
    li  $t3, 1
    j check_condition
print_finish:
    move $a0, $t3           #   printf("%d", $t3)
    li  $v0, 1              #   syscall 1: print_int
    syscall

    li  $v0, 11             #   syscall 11: print_char
    li  $a0, '\n'           #   printf("%c", '\n');
    syscall

    li  $v0, 0
    jr  $ra                 #   return 0;

    .data
numbers:
    .word   0:ARRAY_LEN     #   int numbers[ARRAY_LEN] = {0};

I am getting an error saying that $t4 and $t5 is uninitialized, therefore not able to compare the two values (blt $t4, $t5, change). I looked through my past lecture examples and exercises, but cannot find a reason why $t4 and $t5 is getting uninitialized, where I tried to explain most of the lines through comments. Can anyone help where I'm getting my code wrong? Thanks

Erik Eidt

This error is remarkably friendly.  No real computer will tell you when memory is uninitialized.  Further, even detecting this situation will only apply to small programs as larger programs will reuse and repurpose already used memory, which can defeat the detection of uninitialized memory that the simulator is doing.  None the less, kudos to the simulator.  (Some environments will detect released memory so that uninitialized memory that was released and later repurposed can also be detected.)

Your primary loop is initializing array elements 0 through 9, yet the rest of the program consumes array elements 1 through 10.  On index 10, it will be accessing uninitialized memory.

Your exit condition for the second loop is > rather than >= as with the first loop.  You should have been able to see this during single step debugging, especially if you're looking at the memory addresses being computed and contents there (which is a normal part of detailed debugging).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related