Page 1 of 1

Passing arguments between functions

Posted: Sun Feb 22, 2015 4:26 am
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!

Re: Passing arguments between functions

Posted: Sun Feb 22, 2015 5:27 am
by bluemoon
You have two different variables both named ComPort in different scope.
What's the value of COM1?

Re: Passing arguments between functions

Posted: Sun Feb 22, 2015 5:43 am
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 ' '...

Re: Passing arguments between functions

Posted: Mon Feb 23, 2015 3:39 am
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;
?

Re: Passing arguments between functions

Posted: Mon Feb 23, 2015 7:58 am
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!

Re: Passing arguments between functions

Posted: Mon Feb 23, 2015 8:08 am
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.

Re: Passing arguments between functions

Posted: Mon Feb 23, 2015 3:24 pm
by eryjus
Do you have a prototype for

Code: Select all

void serialInit(unsigned short ComPort)
above the call in main()?

Re: Passing arguments between functions

Posted: Mon Feb 23, 2015 11:40 pm
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?