Why is my program failing to load a null value into the final index of an array?


I'm working on a Memory Mapped I/O problem using MIPS/QtSPIM for one of my classes. The goal is to read characters from the keyboard into a fixed length buffer (size n) pointed to by $a0. If the buffer reaches index n-1 or a linefeed character is entered, the final step is to append a null character to the end of the array as you would see in a null-terminated string and exit the program. I have the input working correctly, but when I attempt to add the null character in the final step, nothing is happening. I've tried hardcoding a random character into the last index and even that's not working, so I feel like I'm missing something basic syntax-wise. Here's what I have so far:

CONS_RECEIVER_CONTROL       =   0xffff0000
CONS_RECEIVER_DATA          =   0xffff0004
CONS_DISPLAY_CONTROL        =   0xffff0008
CONS_DISPLAY_BUFFER         =   0xffff000c
CONS_READY_MASK             =   0x00000001
ISO_LF                      =   10
SYS_PRINT_CHAR              =   11
        .globl main

        li        $t0, CONS_RECEIVER_CONTROL
        la        $a0, input_buffer
        li        $a1, 9
        li        $t1, 0              # counter
        li        $t4, 0

        # wait for character to be input
        lw        $v0, ($t0)
        andi      $v0, $v0, CONS_READY_MASK
        beqz      $v0, key_wait

        lbu       $t2, CONS_RECEIVER_DATA
        beq       $t2, ISO_LF, exit

        # print current character

        lw        $v0, 8($t0)
        andi      $v0, $v0, 0x01
        beq       $v0, $zero, write_poll
        addu      $t3, $a0, $t1               # set byte offset in buffer
        sb        $t2, ($t3)
        sw        $t2, 12($t0)
        addi      $t1, $t1, 1
        beq       $t1, $a1, exit
        j         key_wait

        addu      $t3, $a0, $t1
        sb        $t4, ($t3)
        li        $v0, 10


input_buffer: .space 10
asked on Stack Overflow Nov 9, 2019 by rogue_wombat

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0