Page 1 of 1

Baking Pi: Transfer to C code

Posted: Wed Sep 25, 2013 2:07 pm
by weshmek
Hi,

I just finished OK02 in the Baking Pi tutorial. I can now make the LED blink on and off.
So I wrote an assembly program "blink" to make the LED blink x times, where x is the value passed in r0.

I also tried transferring control to C code, since this is obviously a good step towards increased productivity.
My _start looks like this:

Code: Select all

.section init
.globl _start
_start:
b kmain
kmain looks like this:

Code: Select all

int kmain()
{
 blink(fibonacci(5));
}
fibonacci looks like this:

Code: Select all

int fibonacci(int x)
{
 if (x == 0)
 {
  return 1;
 }
 else if (x == 1)
 {
  return 1;
 }
 else
 {
  return fibonacci(x - 1) + fibonacci(x - 2);
 }
}
if kmain calls fibonacci(0), the LED blinks once, as expected.
However, any other argument for fibonacci causes no blinks.

I'm using YAGARTO on Windows, using gcc to compile both C and assembly.

Is there anything glaringly obvious that's wrong with this code?
Is there something I should know when calling C code from assembly?

Re: Baking Pi: Transfer to C code

Posted: Wed Sep 25, 2013 2:16 pm
by dwelch67
you used a branch instead of branch link to get to kmain, so when kmain returns it could/should crash. did you setup the stack before you called C the first time? kmain() calls blink() one time I dont see any loops so maybe there is something I am missing, were you wanting to call blink more than once?

I have some long winded info on mixing C and asm and examples github.com/dwelch67/raspberrypi that you may or may not find useful for what you are doing.

David

Re: Baking Pi: Transfer to C code

Posted: Wed Sep 25, 2013 2:22 pm
by weshmek
dwelch67 wrote:you used a branch instead of branch link to get to kmain, so when kmain returns it could/should crash. did you setup the stack before you called C the first time? kmain() calls blink() one time I dont see any loops so maybe there is something I am missing, were you wanting to call blink more than once?

I have some long winded info on mixing C and asm and examples github.com/dwelch67/raspberrypi that you may or may not find useful for what you are doing.

David
I admit, I was a little kamikaze with this code. kmain shouldn't return properly. In fact, the code for blink() doesn't return properly either. But if I just have

Code: Select all

int kmain()
{
 blink(5);
}
the LED does blink 5 times. ("blink" is just my solution to OK02 with some looping instructions around it). So shouldn't

Code: Select all

blink(fibonacci(5));
at least reach blink, provided the procedures compile properly?

I just don't see how implementing proper calling convention in my handwritten assembly should affect my compiled C when it simply calls other bits of compiled C.

I'm planning on fixing _start and blink when I get home from work tonight.

Re: Baking Pi: Transfer to C code

Posted: Wed Sep 25, 2013 2:45 pm
by weshmek
dwelch67 wrote:you used a branch instead of branch link to get to kmain, so when kmain returns it could/should crash. did you setup the stack before you called C the first time? kmain() calls blink() one time I dont see any loops so maybe there is something I am missing, were you wanting to call blink more than once?

I have some long winded info on mixing C and asm and examples github.com/dwelch67/raspberrypi that you may or may not find useful for what you are doing.

David
After looking at your github examples, I think the major thing I was doing wrong was that I did not set my stack up properly.

I think I'm going to end up looking at everything on your github page!