This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

scope of label

Is a label local or global? For instance, two functions aa1() and aa2() are in file aa. A label L is placed in aa1(). Another function bb() is located in bb file. Which function(s) can access the label L? Thank you for aswering my question.

chao.

  • Labels in a C function are "local". A branch target scope more broad than that requires setjmp/longjmp, but then you don't get to "name" your destinations quite as directly :-)

  • "Which function(s) can access the label L?"

    The short and direct answer is, aa1().

  • Thank you, Dan.

    Another question, how do I declare a variable to store the address of a label?

    chao.

  • Another question, how do I declare a variable to store the address of a label?

    You don't. There's nothing you could safely do with that value once you had it, so there's no point acquiring it in the first place.

    If you really want to write 100% spaghetti code, write assembler code, not C.

  • Another question, how do I declare a variable to store the address of a label?

    Why don't you explain what you really want to do? It sounds like you are about to attempt something very dangerous.

  • "Another question, how do I declare a variable to store the address of a label?"

    Like others have stated, it might be good to know what you are trying to do, so that we can give better answers. Lacking that however, I can only say that within the scope of a function, C does not have the means to retrieve a label's address. The closest thing that approximates this is C's switch/case construct with each "case label" being "addressable". These labels are not true addresses, but could be thought of as a sort of address abstraction, if that make sense.

  • Functions in C normally serve the purpose of a label in assembly. You don't usually jump to a label to execute a routine. You would take the code at that label, make it a function, and call that function from several places.

    If you need a "variable label", so that one call winds up in perhaps several places, then you use a function pointer (pointer to a function). Function pointers have some issues with the Keil C compile-time stack analysis that will require you to study a couple of app notes.

  • "Function pointers have some issues with the Keil C compile-time stack analysis that will require you to study a couple of app notes."

    See: http://www.8052.com/forum/read.phtml?id=47543

  • The reason of using a label is to treat it as a fixed interrupt point that is the return address for an interrupt is the same every time the interrupt firs. I think it is okay to do so since I don't need the old data produced by the main() and what it will do after the interrupt is serviced. The interrupt gives the newest information I need.

    The project I am working on has a feature that is both main() and ISR are pretty much doing the same job. The main() does analyze the raw data coming from uart and show the analyzed data on a LCM. The analyzed data is shown on several pages. Each page is selected by an user. The ISR display the analyzed data according to an user's selection from a keypad. Because of the feature I have to take care the display-ordering problem which is mentioned in the thread http://www.keil.com/forum/docs/thread3030.asp. The solution I try to use is to stop the main() displaying the old data after interrupt is serviced. To do so I have to make the interrupt return address differ to the "original" and address it to the "proper" location. The label is used for the return point.

    I read the article in http://www.keil.com/support/docs/307.htmv. It is about soft-reset. 0x0000 can be used as a fixed return address, why not other addresses? So, I try to "modify" the code to achieve my goal. If you pros approve this method, it will be good. If not, I have to work on second plan.

    Thanks you all for helping me.

    chao.

  • Chao,

    You are really trying to make your life difficult! Please don't try and change the ISR return address or anything like that, you'll probably never get it working reliably (if at all).

    Do all analysis and display of data in functions called from main(). Set a flag in the ISR to indicate that a key has been pressed. Reset that flag in main(). Put all the logic that decides when and what should be displayed in main().

    I can't see any problem with doing things this way based on the information you have given. If you think there is a problem doing this please explain what it is and we'll try and help.

    Stefan

  • Stefan,

    Thank you very much. There is nothing wrong with your approach. Actually there are some disadvantages on the fixed return address approach and I am aware of that. My plan 2 is exactly what you pros (in both threads) suggested: place a flag in the ISR and let main() do the display thing.

    I try to evaluate the cons and pros on both approaches, fixed return address (if it is doable) and flag in ISR. It seems I have to go for the flag approach (don't say I didn't try). There is no perfect solution for this project based my limited knowledge. I have to give up something to gain something. It all depends on what is more important or significant to what. After all, perfection is not a solution at all.

    chao.