OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 8:15 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Passing arguments between functions
PostPosted: Sun Feb 22, 2015 4:26 am 
Offline

Joined: Sun Feb 22, 2015 4:21 am
Posts: 4
Hi,

i habe the following Problem:

Im passing a function an argumten as 0x03F8 and get it back on the other end as 25.

Has anyone an idea what im doning wrong?

void main()
{
/* You would add commands after here */

/* ...and leave this loop in. There is an endless loop in
* 'start.asm' also, if you accidentally delete this next line */

init_video();
unsigned short ComPort = COM1;
serialInit(ComPort);

serialWrite(ComPort, 0x41);
puts("Hello Chat!!!");
for (;;);
}

void serialInit(unsigned short ComPort)
{
unsigned char config;
ComPort = 0x03F8;

//disable interrupts
outportb(INTERRUPT_ENABLE_PORT(ComPort), 0x00);

//boud rate divisor
outportb(LINE_CONTROL_PORT(ComPort), ENABLE_DLAB);
outportb(DLAB_LOW_BYTE_PORT(ComPort), BAUD_RATE_DIVISOR & 0x00FF);
outportb(DLAB_HIGH_BYTE_PORT(ComPort), BAUD_RATE_DIVISOR & 0xFF00);

config = 0x03; //length of 8 bits, no parity bit, one stop bit and break control disabled.
outportb(LINE_CONTROL_PORT(ComPort), config);

config = 0xC7; //FIFO Buffer
outportb(FIFO_CONTROL_PORT(ComPort), config);

config = 0x03; //RTS=1 DTS=1
outportb(MODEM_CONTROL_PORT(ComPort), config);
}

GDB Output:

(gdb) c
Continuing.

Breakpoint 1, main () at main.c:60
60 {
(gdb) n
66 init_video();
(gdb)
67 unsigned short ComPort = COM1;
(gdb)
68 serialInit(ComPort);
(gdb) p /x ComPort
$1 = 0x3f8
(gdb) c
Continuing.

Breakpoint 2, serialInit (ComPort=25) at serial.c:18
18 {

Thanks for any answer!


Top
 Profile  
 
 Post subject: Re: Passing arguments between functions
PostPosted: Sun Feb 22, 2015 5:27 am 
Offline
Member
Member
User avatar

Joined: Wed Dec 01, 2010 3:41 am
Posts: 1761
Location: Hong Kong
You have two different variables both named ComPort in different scope.
What's the value of COM1?


Top
 Profile  
 
 Post subject: Re: Passing arguments between functions
PostPosted: Sun Feb 22, 2015 5:43 am 
Offline

Joined: Sun Feb 22, 2015 4:21 am
Posts: 4
#define COM1 0x3F8

I dont see it where i have the mistake...

In the main() function i declare a unsigned short ComPort = COM1 which gets the right value as is see in the debugger.
Than i use serialInit(ComPort) and when i stop there with the debugger i get ComPort = 25(dec). The serialInit expects a unsigned short as well.
(in serialInit ComPort = 0x03F8; i use only to make it work for testing)
I dont know what im doing wrong...

I use the same varaible ComPort in the main function to pass it to serialWrite where it works. There the char 'A' is coming in as ' '...


Top
 Profile  
 
 Post subject: Re: Passing arguments between functions
PostPosted: Mon Feb 23, 2015 3:39 am 
Offline
Member
Member

Joined: Tue Feb 26, 2008 10:47 am
Posts: 89
Location: Sweden
Since the value of ComPort passed to serialInit is unused, it is quite possible that the compiler optimizes it away in the call.
Do you get the same problem if you remove the line
Code:
ComPort = 0x03F8;
?

_________________
Blawg


Top
 Profile  
 
 Post subject: Re: Passing arguments between functions
PostPosted: Mon Feb 23, 2015 7:58 am 
Offline

Joined: Sun Feb 22, 2015 4:21 am
Posts: 4
I have no idea,

but it seems to me that only gdb is showing me the wrong content of the variable. The code is working even if i'm removing the line to set the variable in serialInit.

Thanks for your help!


Top
 Profile  
 
 Post subject: Re: Passing arguments between functions
PostPosted: Mon Feb 23, 2015 8:08 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
IOW, everything's working except gdb isn't showing the expected values of your variables. I think if you specify lower optimization level (e.g. -O2 instead of -O2), you may get from gdb what you want. It's a relatively common problem with optimization vs debuggability.


Top
 Profile  
 
 Post subject: Re: Passing arguments between functions
PostPosted: Mon Feb 23, 2015 3:24 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 21, 2011 9:47 pm
Posts: 286
Location: Tustin, CA USA
Do you have a prototype for
Code:
void serialInit(unsigned short ComPort)
above the call in main()?

_________________
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber


Top
 Profile  
 
 Post subject: Re: Passing arguments between functions
PostPosted: Mon Feb 23, 2015 11:40 pm 
Offline

Joined: Sun Feb 22, 2015 4:21 am
Posts: 4
Ok, i found the following out:

Independent of the optimization i can see the correct number if i get the adress of ComPort first, and than watch the memory contents of 2 bytes at that adress.

Is there a simpler way in gdb to see an unsigned short? With the print command?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 46 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