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

Movx command in Keil

Hello all,
I'm using Keil uVision2 Version 7.05, and everything seems to be OK. However, there is one thing I don't quite understand, thus I'm posting here asking for your ideas/opinions.
When I run "Hello" example in Keil\Example directory and I found something that puzzled me. The assembly code Keil generated in Disamsembly window as well as in list file always have MOVX command eventhough I don't use external ram at all. I did set memory model to small and I only use IDATA but it still does the same. I even use disamsembler to translate the hex file Keil generated back to assembly source and still get the same result. Can someone spend some times explain this for me? Any inputs would be greatly appreciated. Have a nice weekend.

Best Regards,
T.L

Parents
  • Thank you Jon and Drew for your explainations. I'm very comfortable programming 8051 with Assembly, but I'm totally new to C, please bare with me. One more thing here Jon as you said that

    The printf function takes generic pointers and,
    therefore, must have code that can access all memory areas
    Are there anyways to tell compiler not generate code that access all the memory areas if it's not needed? Because I see the assembly code is only few lines as it shows in the list file, but the hex code takes roughly more than 1kbyte at the compiling time (if I'm not mistaken because I'm away from my work now). This is not very efficient on the view of code size. Don't you agree? Or there is something that I'm missing here?

    Best Regards,
    T.L

Reply
  • Thank you Jon and Drew for your explainations. I'm very comfortable programming 8051 with Assembly, but I'm totally new to C, please bare with me. One more thing here Jon as you said that

    The printf function takes generic pointers and,
    therefore, must have code that can access all memory areas
    Are there anyways to tell compiler not generate code that access all the memory areas if it's not needed? Because I see the assembly code is only few lines as it shows in the list file, but the hex code takes roughly more than 1kbyte at the compiling time (if I'm not mistaken because I'm away from my work now). This is not very efficient on the view of code size. Don't you agree? Or there is something that I'm missing here?

    Best Regards,
    T.L

Children
  • Are there anyways to tell compiler not generate code that access all the memory areas if it's not needed?

    What you failed to understand so far is that the movx'es you found are not actually generated by the compiler. They're found in generic routines pulled in from the run-time library, by the linker.

    Nor does this code necessarily access unused memory classes --- it just has the potential to do so, in case that you did have stuff stored there. I.e. your code will only ever reach any of those movx instructions by exercising undefined behaviour.

    If you insist on doing so, the way to tell the tools not to link in these functions is to not call any library functions that operate on generic pointers (in the case at hand, printf()).

  • "Are there anyways to tell compiler not generate code that access all the memory areas if it's not needed?"

    The code to implement printf() was not generated when you compiled your code. printf is a module in a library what was generated by Keil as part of its toolchain distribution. For your code, use the small model and when using pointers in your routines, use explicit memory space qualifiers.

    "Because I see the assembly code is only few lines as it shows in the list file, but the hex code takes roughly more than 1kbyte at the compiling time ... Or there is something that I'm missing here?"

    More accurately, that would be "takes roughly more than 1kbyte at the linking time". Look at the linker map file and find out where all that code is coming from -- printf(), no doubt. Since this example does not need formatted output, printf() is overkill. Try using puts() instead and check the size.

  • Dear Hans and Dan,
    Thank you very much for your kind explanations. I'll try as your suggestions. It might take me a while to understand the whole process of linking, compiling, and calling library ...etc and so on. As Erik used to say: "It's bible time". I think I have to read more Keil C manual and try do digest it as much as I can. Besides, I'm also reading "C51 Primer", and "The Final Word on the 8051". Hopefully, after reading all this I'll get a better grasp of C programming for 8051.
    Once again thanks for your attentions. Have a great day.
    Best Regards,
    T.L

  • Wow!!!!
    I tried the puts instead of printf as Dan said, and the code went from 1186 bytes down to 251 bytes. It's amazing.
    One more question here forks: Is there any penalty for doing this? Because I see the "Hello World" print out on the screen seem to be a little bit slower than using printf. Is it just my feeling or it's something else that I don't know of?
    Thank you all.
    Best Regards,
    T.L

  • "I tried the puts instead of printf as Dan said, and the code went from 1186 bytes down to 251 bytes. It's amazing."

    It shouldn't be in the least amazing at all!

    Look at the description of printf in the Manual - it runs to five pages!
    It should thus be obvious that printf is an extrmely powerful and complex function - it should come as no surprise that it takes a lot of code to implement all that stuff!!

    See: http://www.keil.com/forum/docs/thread2741.asp