Jump to content


Photo

Compiler Is Screwing Up My Assembly Code!


3 replies to this topic

#1 John P

John P

    Newbrie

  • EstablishedMember
  • 17 posts

Posted 03 January 2013 - 06:03 PM

I wrote some of my program in assembly for the reason that people usually do--to get completely optimized code where I'd know exactly how long everything will take. So here's a line of C code that I rewrote into assembly:

    MOVLW 0x40             // portb = 0b01000000;
    MOVWF _portb

Now when I compile/assemble the program, I find that the compiler insists on setting the register bank to zero. Doing this adds two extra instructions! And the wasted lines are added again and again, just in case I sneaked a bank change in when the compiler wasn't looking. Like this, in the "View Disassembly Listing":

117:               MOVLW 0x40			// portb = 0b01000000;
   054    3040     MOVLW 0x40
118:               				MOVWF _portb
   055    1283     BCF 0x3, 0x5
   056    1303     BCF 0x3, 0x6
   057    0086     MOVWF 0x6

I'd have thought that if I'm willing to use assembly, it's my responsibility to set the bank. Or at least, there should be a command to lock out helpful behavior by the compiler. But I'm not encouraged by the manual, where it says "Bank switching and code page switching code should NOT be added to inline assembly code. The linker will add the appropriate Bank switching and code page switching code." If there were a way to stop it happening, wouldn't this be the place to tell the user?

This isn't a positive feature, in my humble opinion.

#2 JorgeF

JorgeF

    Super Enthusiast

  • EstablishedMember
  • PipPipPip
  • 276 posts
  • Gender:Male
  • Location:ES @ Europe, third rock from the Sun

Posted 03 January 2013 - 07:55 PM

Hi

This looks like a reasonable and expectable behaviour of the "C" compiler.
There is no way you can know what RAM bank will be selected when the program reaches your ASM instructions, so the bank switching must be added to prevent runtime errors.

If you really need serious optimization you need to make the whole program in ASM, not only a few inline indtructions.


Best regards
Jorge

Edited by JorgeF, 03 January 2013 - 07:55 PM.


#3 John P

John P

    Newbrie

  • EstablishedMember
  • 17 posts

Posted 07 January 2013 - 02:40 PM

That last suggestion shows some lack of knowledge about how code for microcontrollers is used. Here we are on a discussion forum about a compiler; I don't object to compilers! The advantages to using one are too obvious to be worth discussing.

On the other hand, there are times when part (just part!) of one's code needs to run at the highest speed, or needs to run at some speed that's predictable on the basis of how the program was written, on a finer scale than you could ever get simply using an interrupt. That's when the user would consider using a brief section of assembly, and then it's intolerable to have the compiler inserting "helpful" extra lines. For the time that I'm using assembly, I want full control of what's in the code.

As you see in my code snippet, I'm controlling an external port. I'm using it to bit-bang an external circuit, and I want control over the timing. Is that too much to ask?

Well, I've fired up my many-versions-obsolete CCS compiler and it still works fine. I had to lie to it about which processor I'm using and define a whole lot of registers, but fortunately, with the 16F PICs, the code is always the same.

#4 JorgeF

JorgeF

    Super Enthusiast

  • EstablishedMember
  • PipPipPip
  • 276 posts
  • Gender:Male
  • Location:ES @ Europe, third rock from the Sun

Posted 07 January 2013 - 10:25 PM

Hi

You can be sure I know what you mean by timing control and fine tunning ( http://www.microchip...b.ashx?m=680798 ).
But, after 10 years of ASM only programming for embeded systems and "C" programming for desktop apps, I also know what are the limitations inposed by a compiler.
By the way, the need for bank selection is not only for a "C" compiler, evne with ASM, if its relocatable code you also must use the bankel for each RAM access, this can be easily seen in Microchip sample code for example.
The kind of ultra fine control over timing you mention is only available in "absolute" ASM code.

OTOH, as the inserted "bank selection instructionsl" are predictable you can take it into account in your timing.


Best regards
Jorge



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users