Go to Post Ohh the [strike]humanity[/strike] robotity! - artdutra04 [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 26-10-2003, 16:21
ScottWolchok ScottWolchok is offline
Pwnt.
#0217 (ThunderChickens)
Team Role: Programmer
 
Join Date: Oct 2003
Rookie Year: 2004
Location: Utica, MI
Posts: 23
ScottWolchok will become famous soon enoughScottWolchok will become famous soon enough
Send a message via AIM to ScottWolchok
Weird Stuff in ifi_startup.c

I was poking through the default code in an effort to determine if we could program the Master PIC as well as the User PIC, and I found this odd stuff thither.
Code:
void
_entry (void)
{
_asm goto _startup _endasm

}
#pragma code _startup_scn
void
_startup (void)
{
  _asm
    // Initialize the stack pointer
    lfsr 1, _stack lfsr 2, _stack clrf TBLPTRU, 0 // 1st silicon doesn't do this o
n POR
    bcf  FPFLAGS,RND,0 // Initialize rounding flag for floating point libs
    
    // initialize the flash memory access configuration. this is harmless
    // for non-flash devices, so we do it on all parts.
    bsf 0xa6, 7, 0
    bcf 0xa6, 6, 0
  _endasm 

loop:

        Clear_Memory();              
  _do_cinit ();
  // Call the user's main routine
  main ();

  goto loop;
}                               /* end _startup() */
Mmkay, so I look at this and I'm really lost. First of all, what does the underscore at the start of all these functions signify? Second, isn't assembler in C done like a function call? Thirdly, is that entry function even necessary? And last, I thought C programs started with the main function, but this code calls it instead... @_@
  #2   Spotlight this post!  
Unread 26-10-2003, 17:15
FotoPlasma FotoPlasma is offline
\: |
no team
 
Join Date: Jul 2001
Rookie Year: 2001
Location: San Jose
Posts: 1,900
FotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond repute
Send a message via AIM to FotoPlasma
First of all, "thither", in modern English, translates to "to there".

Sorry, I just like to nitpick English. More to the point...

In Microchip's PIC C, assembly language programming is done using "_asm" as a starting point, and "_endasm" as an endpoint. Anything between these two points is interpreted as assembly language programming. As for the functionality of the code you're referencing, I have no idea. A cursory inspection leads me to believe, as you do, that the _entry function isn't necessary, but I didn't write the stuff. In the _startup function, the lines between the "_asm" and "_endasm" lines start with the PIC instruction mnemonics, and are followed by arguments. BCF and BSF are "bit clear following" and "bit set following", respectively, and LFSR is "Move literal (12-bit) 2nd word to FSRx1st word". The entire instruction set is well-documented in the PIC18F8520 datasheet.

I'm sorry that I don't know the specifics, and can't be very helpful.
__________________
I played hacky sack with Andy Baker.

2001-2004: Team 258, The Sea Dawgs
2005: Team 1693, The Robo Lobos
  #3   Spotlight this post!  
Unread 27-10-2003, 12:42
Paladino Paladino is offline
Registered User
#0147 (Deep Thunder)
 
Join Date: Nov 2002
Location: St. Petersburg FL
Posts: 9
Paladino is an unknown quantity at this point
You are correct that in c most programs start with a main(), but that assumes that the Operating systems is present. In embedded systems with no OS _entry is the address that the CPU jumps to at power-up. The entry point usually contains processor initialization code. Since this code must run without the CPU being fully setup the developer usually will use ASM as the c runtime libraries are not available yet. You will notice that this code sets up the stack pointer which points to the memory location that stores function arguments and local variables, initializes floating point, configures memory and then calls _do_cinit () which initializes the c runtime so that c code can be used. The jump to main() is where the c code starts.


Rich

Last edited by Paladino : 27-10-2003 at 12:56.
  #4   Spotlight this post!  
Unread 28-10-2003, 14:47
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: Weird Stuff in ifi_startup.c

Quote:
Originally posted by ScottWolchok
First of all, what does the underscore at the start of all these functions signify?
Scott

The underscore in the function name is usually used to signify an internal library function. This is a design decision that provides a few benefits to the end programmer.

The first is that it is easy to identify the functions that are provided.

The second is that the library authors are able to use meaningful names within their functions, yet they don't take valid function names away. For example, in your example above, if the library used entry() instead of _entry(), the end programmer may not be allowed to write a function named entry.

Hope this makes sense
  #5   Spotlight this post!  
Unread 22-08-2004, 15:21
Tom Saxton's Avatar
Tom Saxton Tom Saxton is offline
Registered User
no team (Issaquah Robotics Society)
Team Role: Mentor
 
Join Date: Dec 2003
Rookie Year: 2003
Location: Sammamish, WA
Posts: 98
Tom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud of
Re: Weird Stuff in ifi_startup.c

Quote:
Originally Posted by ScottWolchok
Mmkay, so I look at this and I'm really lost. First of all, what does the underscore at the start of all these functions signify? Second, isn't assembler in C done like a function call? Thirdly, is that entry function even necessary? And last, I thought C programs started with the main function, but this code calls it instead... @_@
Although I like to avoid broad generalizations, I believe *every* C program begins with code that runs before "main". There's a bunch of stuff that has to happen before "main" gets called. For example, globals variables have to be initialized (either with explicit values, or with zeros if none are specified) which may involve copying values from ROM (or an executable file) to RAM. Things are even more complex if running under a full OS, where the addresses of functions in external libraries have to be found and written into the program's RAM image or in-memory tables.

Doing this work is very dependent on the architecture, so special code has to be written for each platform (Macintosh, Linux, Windows, Edu RC, etc), generally in assembly. This is done by some combination of the OS folks and the compiler support programmers since some is specific to the OS and some is specific to the language being used.

Thankfully, C programmers generally don't need to know anything about that preamble code except in very unusual circumstances.
__________________
Tom Saxton
http://www.idleloop.com/
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird Al MattK Chit-Chat 7 10-11-2002 00:33
Team 84 stuff... evulish Robot Showcase 6 24-07-2002 00:41
What does everyone like to do besides FIRST stuff?? Katie_269 Chit-Chat 45 17-07-2002 17:01
the weird feeling.. archiver 2000 3 23-06-2002 23:08
who can we buy stuff from?!?! archiver 2001 7 23-06-2002 23:04


All times are GMT -5. The time now is 13:38.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi