Passing arguments between functions

Programming, for all ages and all languages.
Post Reply
Helmut1978
Posts: 4
Joined: Sun Feb 22, 2015 4:21 am

Passing arguments between functions

Post by Helmut1978 »

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!
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Passing arguments between functions

Post by bluemoon »

You have two different variables both named ComPort in different scope.
What's the value of COM1?
Helmut1978
Posts: 4
Joined: Sun Feb 22, 2015 4:21 am

Re: Passing arguments between functions

Post by Helmut1978 »

#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 ' '...
thomasloven
Member
Member
Posts: 89
Joined: Tue Feb 26, 2008 10:47 am
Location: Sweden

Re: Passing arguments between functions

Post by thomasloven »

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: Select all

ComPort = 0x03F8;
?
Helmut1978
Posts: 4
Joined: Sun Feb 22, 2015 4:21 am

Re: Passing arguments between functions

Post by Helmut1978 »

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!
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: Passing arguments between functions

Post by alexfru »

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.
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: Passing arguments between functions

Post by eryjus »

Do you have a prototype for

Code: Select all

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
Helmut1978
Posts: 4
Joined: Sun Feb 22, 2015 4:21 am

Re: Passing arguments between functions

Post by Helmut1978 »

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?
Post Reply