Char[] only works inside function.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
binsky3333
Posts: 14
Joined: Sat Jan 01, 2011 12:34 am

Char[] only works inside function.

Post by binsky3333 »

Hi guys, im trying to get keyboard inout working. The IRQ works and everything my problem is that...

Code: Select all

unsigned char kbdus[128] =
{
    0,  0, '1', '2', '3', '4', '5', '6', '7', '8',	/* 9 */
  '9', '0', '-', '=', '\b',	/* Backspace */
  '\t',			/* Tab */
  'q', 'w', 'e', 'r',	/* 19 */
  't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',	/* Enter key */
    0,			/* 29   - Control */
  'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',	/* 39 */
 '\'', '`',   0,		/* Left shift */
 '\\', 'z', 'x', 'c', 'v', 'b', 'n',			/* 49 */
  'm', ',', '.', '/',   0,				/* Right shift */
  '*',
    0,	/* Alt */
  ' ',	/* Space bar */
    0,	/* Caps lock */
    0,	/* 59 - F1 key ... > */
    0,   0,   0,   0,   0,   0,   0,   0,
    0,	/* < ... F10 */
    0,	/* 69 - Num lock*/
    0,	/* Scroll Lock */
    0,	/* Home key */
    0,	/* Up Arrow */
    0,	/* Page Up */
  '-',
    0,	/* Left Arrow */
    0,
    0,	/* Right Arrow */
  '+',
    0,	/* 79 - End key*/
    0,	/* Down Arrow */
    0,	/* Page Down */
    0,	/* Insert Key */
    0,	/* Delete Key */
    0,   0,   0,
    0,	/* F11 Key */
    0,	/* F12 Key */
    0,	/* All other keys are undefined */
};
Only works when it is inside the function:

Code: Select all

void keyboard_handler(struct regs *r)
{
    unsigned char scancode;

    /* Read from the keyboard's data buffer */
    scancode = inportb(0x60);

    /* If the top bit of the byte we read from the keyboard is
    *  set, that means that a key has just been released */
    if (scancode & 0x80)
    {
        /* You can use this one to see if the user released the
        *  shift, alt, or control keys... */
    }
    else
    {
        print_character(kbdus[scancode]);
    }
}
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Char[] only works inside function.

Post by xenos »

Sounds like a linker problem. Could you post your linker script?
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
binsky3333
Posts: 14
Joined: Sat Jan 01, 2011 12:34 am

Re: Char[] only works inside function.

Post by binsky3333 »

Code: Select all

ENTRY (loader)

SECTIONS{
    . = 0x00100000;

    .text :{
        *(.text)
    }

    .rodata ALIGN (0x1000) : {
        *(.rodata)
    }

    .data ALIGN (0x1000) : {
        *(.data)
    }

    .bss : {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
}
Tosi
Member
Member
Posts: 255
Joined: Tue Jun 15, 2010 9:27 am
Location: Flyover State, United States
Contact:

Re: Char[] only works inside function.

Post by Tosi »

What do you mean that it "only works" inside the function?
What compiler/linker errors does it give you if it's outside of that?
If it compiles fine, what happens when you try to run it?
binsky3333
Posts: 14
Joined: Sat Jan 01, 2011 12:34 am

Re: Char[] only works inside function.

Post by binsky3333 »

It compiles, no errors or warnings. If its outside, the text doesn't display.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Char[] only works inside function.

Post by neon »

Hello,

What bootloader are you using? What debugging steps have you already performed? Have you verified that your .bss section is where its expected to be?
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: Char[] only works inside function.

Post by Chandra »

Can you give an example of outside one(which doesnt work) to make things more clear?
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
prasadjvv
Posts: 5
Joined: Thu Jan 06, 2011 9:48 am

Re: Char[] only works inside function.

Post by prasadjvv »

There are chances for the interrupt service routine to change the DS (data segment) register.
if you keep kbdus[] as global variable, contents of DS register might be different in your interrupt handler compared to the original so that it accesses wrong address (because of segment address) for your variable kbdus.

If you are sure that the DS register contains the same contents as the original in your interrupt handler, no need to bother about this.
jadam
Posts: 4
Joined: Mon Dec 27, 2010 3:41 am

Re: Char[] only works inside function.

Post by jadam »

binsky3333 wrote:It compiles, no errors or warnings. If its outside, the text doesn't display.
Just a guess, but it can't be that you load too few sectors to memory and the end of your binary image is not present? I had similar issue around the keyboard handler and I had to increase the number of sectors loaded by the bootloader... Maybe if you reposition your kbdus[] into a function something else is not loaded if it is really the problem what I think of...
Post Reply