OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 4:48 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: [C]Print a character
PostPosted: Thu Dec 22, 2016 11:33 am 
Offline

Joined: Tue Aug 30, 2016 9:34 am
Posts: 21
Hi !
I'm trying to print a character to the screen with this code :
Code:
#define RAMSCREEN 0xB8000 // Video address.

void printCharacter(char row, char column) // It print a character at the selected location.
{
unsigned char *res_location;
res_location = (unsigned char*)(RAMSCREEN + 2 * (row * 80 + column));
res_location[0]='W';
res_location[1]=0xd;
}

I call it with :
Code:
printCharacter(1,3);

It does not print the "W" at (1;3) but at an other place.


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Thu Dec 22, 2016 11:55 am 
Offline
Member
Member
User avatar

Joined: Mon Apr 18, 2016 9:50 am
Posts: 138
Location: New York New York
Your math is alllll screwy. Remember that the general rule for placing a value at (x, y) is
Code:
bitmap_buffer[y * bitmap_width + x]


Also, C order of operations generally follows standard PEMDAS. But if you're not sure, use parens.

_________________
The OS is P5. Don't expect it to build.


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Thu Dec 22, 2016 12:03 pm 
Offline
Member
Member
User avatar

Joined: Mon Jul 28, 2008 9:46 am
Posts: 325
Location: Ontario, Canada
Can you try the following?:

Code:
(RAMSCREEN + (2 * (row * 80 + column)))

_________________
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Thu Dec 22, 2016 12:29 pm 
Offline

Joined: Tue Aug 30, 2016 9:34 am
Posts: 21
In fact I already tried, it still doesn't work ...


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Thu Dec 22, 2016 12:33 pm 
Offline
Member
Member
User avatar

Joined: Sun Feb 09, 2014 7:11 pm
Posts: 89
Location: Within a meter of a computer
Just to make sure, what happens if you call it row and column set to 0?

_________________
"If the truth is a cruel mistress, than a lie must be a nice girl"
Working on Cardinal
Find me at #Cardinal-OS on freenode!


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Thu Dec 22, 2016 12:41 pm 
Offline

Joined: Tue Aug 30, 2016 9:34 am
Posts: 21
If I call it row and column set to 0 the location of the "W" does not change.


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Thu Dec 22, 2016 12:53 pm 
Offline
Member
Member
User avatar

Joined: Sun Feb 09, 2014 7:11 pm
Posts: 89
Location: Within a meter of a computer
Does changing the parameters in any way change anything at all?

_________________
"If the truth is a cruel mistress, than a lie must be a nice girl"
Working on Cardinal
Find me at #Cardinal-OS on freenode!


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Thu Dec 22, 2016 1:09 pm 
Offline

Joined: Tue Aug 30, 2016 9:34 am
Posts: 21
No it does not change anything.


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Thu Dec 22, 2016 3:52 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
Might I suggest that rather than trying to muck about with a pointer, you treat the buffer as two-dimensional array of a struct containing the two bytes?

Code:
typedef struct {
    uint8_t attrib;
    unsigned char glyph;
} TEXT_FRAME;

TEXT_FRAME *text_buffer = (TEXT_FRAME *) 0xb8000;

#define COLUMN_OFFSET 80

// ...

    text_buffer[(rows * COLUMN_OFFSET) + columns].glyph = 'W';
    text_buffer[(rows * COLUMN_OFFSET) + columns].attrib = 0xd;


Actually, given that there are several text modes, and you also need to bounds-check the text buffer (among other things), I would go with something more like:

Code:
#define DEFAULT_TEXT_MODE 0x03
#define DEFAULT_CODE_PAGE 437   // or set this to a page suited to your native language

struct {
    uint8_t total_rows, total_columns;
} TEXT_MODES[255] = {
    // populate this with a lookup table for the modes by mode #
};

typedef struct {
    uint16_t row, column;
} TEXT_CURSOR;

typedef struct {
    TEXT_MODE mode;
     uint16_t code_page;
    TEXT_CURSOR cursor;   
    TEXT_BUFFER buffer;
} TEXT_PAGE;

const TEXT_BUFFER buffers[] = {
    (TEXT_FRAME *) 0xB8000,
    // .. I can't seem to find the rest of the entry points for the other three
    // text pages ... anyone?
}; 

TEXT_PAGE pages[4];

void kinit_console()
{
    int i;

    for (i = 0; i < 4; i++)
    {
        pages[i].mode = DEFAULT_TEXT_MODE;
        pages[i].code_page = DEFAULT_CODE_PAGE;
        pages[i].cursor.row = 0;
        pages[i].cursor.column = 0;
        pages[i].buffer = buffers[i];
}

bool k_gotoxy(TEXT_BUFFER *page, uint8_t row, uint8_t col, bool reset_hw_cursor)
{
    if (row >= page ->text_mode.total_rows || col >= page ->text_mode.total_columns)
    {
        return false;
    }

    page ->cursor.row = row;
    page ->cursor.column = col;
    if (reset_hw_cursor)
    {
        set_vga_cursor(row, col);
    }

    return true;
}

void kputchar(TEXT_BUFFER *page, unsigned char ch)
{
    int *x = &page ->cursor.columns;
    int *y = &page ->cursor.rows;
    int x_offset = page ->text_mode.total_columns;
    int y_offset = page ->text_mode.total_rows;
    FRAME_BUFFER *buffer = page ->buffer;

    if (x <= x_offset)
    {
        x = 0;
        y++;
    }
    else
    {
        x++;
    }

    if (y <= y_offset)
    {
        // handle scrolling or clearing page as you choose
    }
   
    buffer[(y * x_offset) + x].glyph = ch;

    set_vga_cursor(x, y);
}


(Not tested code, just use this as a guide.)

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Thu Dec 22, 2016 4:00 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
Having said all that, it occurs to me I forgot to ask: just where is it writing the character to?

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Thu Dec 22, 2016 8:25 pm 
Offline
Member
Member

Joined: Mon Sep 19, 2016 5:34 am
Posts: 27
Does this will put X at the corner of your screen?
Code:
*((char*)0xB8000) = 'X';


If not, maybe you have initialized wrong GDT base address

_________________
https://github.com/irvanherz/jaura/


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Fri Dec 23, 2016 4:05 am 
Offline

Joined: Tue Aug 30, 2016 9:34 am
Posts: 21
yes it put an X.
If i keep the previous code, the W keeps showing at (0;12).


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Fri Dec 23, 2016 5:39 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
You should

1. Inspect the generated code.

2. Then single-step through it in a debugger.

It should then be fairly obvious what is going wrong.


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Wed Dec 28, 2016 6:27 am 
Offline
Member
Member
User avatar

Joined: Tue Aug 02, 2016 1:52 pm
Posts: 286
Location: East Riding of Yorkshire, UK
Schol-R-LEA wrote:
Code:
typedef struct {
    uint8_t attrib;
    unsigned char glyph;
} TEXT_FRAME;


Shouldn't the character be before the attribute byte in the struct?

_________________
com.sun.java.swing.plaf.nimbus.InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState
Compiler Development Forum


Top
 Profile  
 
 Post subject: Re: [C]Print a character
PostPosted: Wed Dec 28, 2016 6:42 am 
Offline
Member
Member
User avatar

Joined: Sun Dec 11, 2016 3:31 am
Posts: 29
Location: In your computer
zenzizenzicube wrote:
Schol-R-LEA wrote:
Code:
typedef struct {
    uint8_t attrib;
    unsigned char glyph;
} TEXT_FRAME;


Shouldn't the character be before the attribute byte in the struct?

Correct me if i'm wrong but should this have the packed attribute ?

_________________
myunix (version 3) (name suggestions are welcome!)
GPG Key fingerprint: 5ED6 D826 ACD4 3F8E D9D4 FBB2 FF0A AF5E 0812 BA9C


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 29 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group