'Custom print function has undesired behaviour

I'm making my own x86 OS using the i386-elf cross-compiler and linker and nasm to compile asm files. The OS itself runs with qemu. That being said, I made a custom print function but ran into a problem. Every time I access memory (either through the [] operator or by dereferencing a pointer) and call my print function afterwards, it leaves 8 blank spaces and then prints normally. Print code:

void printv(char *str, ...)
{
    unsigned int tmp_cursor = get_cursor_position();
    cursor_position.x = (unsigned short)(tmp_cursor >> 16);
    cursor_position.y = (unsigned short)tmp_cursor;
    char buffer[12];

    va_list list_ptr;
    va_start(list_ptr, str);
    
    unsigned int i = 0;
    for (char *ptr = str; *ptr != '\0'; ptr++)
    {    
        switch (*ptr)
        {
        case '%':
            cursor_position.y += (cursor_position.x + i) / 80;
            cursor_position.x = (cursor_position.x + i) % 80;            
            update_cursor(cursor_position.x, cursor_position.y);
            i = 0;
            switch (*(ptr + 1))
            {
            case 'c':
                buffer[0] = (char)va_arg(list_ptr, int);
                buffer[1] = '\0';
                printv(buffer);
                ptr++;
                break;
            case 's':
                printv(va_arg(list_ptr, char *));
                ptr++;
                break;
            case 'i':
            case 'd':                
                int_to_str(va_arg(list_ptr, int), buffer, 10);
                printv(buffer);
                ptr++;
                break;         
            default:     
                *(char*)(0xb8000 + (cursor_position.x + i + cursor_position.y * 80) * 2) = *ptr;
                i++;       
                break;
            }  
            break;          
        case '\n':
            i = 0;
            cursor_position.x = 0;
            cursor_position.y++;
            break;
        case '\t':
            cursor_position.y += (cursor_position.x + i) / 80;
            cursor_position.x = (cursor_position.x + i) % 80;                    
            update_cursor(cursor_position.x, cursor_position.y);
            i = 0;
            cursor_position.x += TAB_SPACE - cursor_position.x % TAB_SPACE - 1;        
            break;
        default:
            *(char *)(0xb8000 +(cursor_position.x + i + cursor_position.y * 80) * 2) = *ptr;
            i++;
            break;
        }     
    }

    va_end(list_ptr);
    memset(buffer, '\0', 12);
    cursor_position.y += (cursor_position.x + i) / 80;
    cursor_position.x = (cursor_position.x + i) % 80;  
    update_cursor(cursor_position.x, cursor_position.y);
}

Call example:

    printv("Starting PonchOS!\n");
    char str[12];
    for (int i = 0; i < 11; i++)
    {
        str[i] = 'a' + i;
    }
    str[11] = '\0';
    
    printv("Testtesttesttesttest");

Output:

enter image description here

As you can see, it prints fine before any memory access, but after that, it leaves those white spaces. Any ideas as to why this happens?

Edit:
Implementing @chqrlie 's changes, some issues have been fixed, although spacing problems persist.
Code:

printv("Starting PonchOS!\n");
printv("%c\n", 'C');
printv("%i", 128);
printv("%s", "string");

Output: Undesired spacing



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source