XC8: .asm to C ( i.e. Lowering One's Expectations)

Thread Starter

joeyd999

Joined Jun 6, 2011
6,331
Here is the main C file. This is all it will ever be. Just calls to each module's polling routine.

main.c:
#include <xc.h>
#include "config_pic18f57k42.h"
#include "system_timer.h"
#include "init.h"
#include "power.h"
#include "ledbarbi.h"
#include "charlcd.h"

void main(void) 
{
    coldboot();         //cold boot initialization (init.c)

    while (1)
    {
       system_timer_poll();
       power_poll();
       led_poll();
       LCD_poll();
    }
}
 

ApacheKid

Joined Jan 12, 2015
1,762
New question -- with a simple example:

Let's say I want to skip a few instruction cycles, say a series of 10 NOPs. I can write in my code Nop(); 10 times, obviously.

In .asm, I can also write 10 NOPs in succession, but I can write a macro like:

Code:
nopn    macro   numn
variable count=0
while (count<numn)
        nop
count=count+1
        endw
and invoke this as:

Code:
        nopn    10
And the macro expands at assembly time.

How do I do similar in C?
You likely can't because despite what some claim, C was not designed to let you do that, it could have been, a language could recognize that sometimes there's a need but it simply does not have any such concept despite the occasional need.
 

ApacheKid

Joined Jan 12, 2015
1,762
Well polling is

Well I think it would help to define a specification for your product or for customers expectations. That specification will define power consumption, battery life, weight, speed, response times etc. as well as acceptable margins, tolerances so to speak.

Then - one the basis of that - one can start to define costs, how much does it cost in terms of capital investment, planning time, development and testing time, how many many hours, how many people, expected elapsed hours, anticipated deliver dates, desired delivery dates.

Customers can expect whatever they want to expect, the bottom line there is a cost - its really that simple, in principle anyway. If they find you too expensive then why? where else can they go? who can deliver all these things for less than you, and why? how can they? what are they doing that you can't?
 

ApacheKid

Joined Jan 12, 2015
1,762
With all your experience of this very specific platform, tools and customer expectations, you should be able to determine a best case ceiling for this. You should be able - I would argue - with relatively little analysis, be able to say pretty quickly if their goal is achievable or not, rather than struggle to push things to the n'th degree to see if you can, it's OK to do that though IF they are paying for that. I mean who are these customers? NASA? USAF? SpaceX? ESA?
 

Thread Starter

joeyd999

Joined Jun 6, 2011
6,331
You likely can't because despite what some claim, C was not designed to let you do that, it could have been, a language could recognize that sometimes there's a need but it simply does not have any such concept despite the occasional need.
Wow. I was going to write a macro Delay_ns().

Delay_us() and Delay_ms() are *far* too coarse and I'd never waste cycles on them.

Between you and @nsaspook, I'm learning all the things that C cannot do, and all the ways I must lower my standards for what I consider good code.
 

Thread Starter

joeyd999

Joined Jun 6, 2011
6,331
With all your experience of this very specific platform, tools and customer expectations, you should be able to determine a best case ceiling for this.
I have. And I haven't asked you for assistance in this regard.

Thanks for your help -- but I'm good.
 
Last edited:

ApacheKid

Joined Jan 12, 2015
1,762
Wow. I was going to write a macro Delay_ns().

Delay_us() and Delay_ms() are *far* too coarse and I'd never waste cycles on them.

Between you and @nsaspook, I'm learning all the things that C cannot do, and all the ways I must lower my standards for what I consider good code.
Well I'm now going to surprise some here and defend C !

It is inarguable that using a high level language carries costs, nobody here would argue otherwise, your problem is to perform an objective cost/benefit analysis, do the benefits outweigh the costs or not? Only you can do that calculation but you must do it dispassionately in order to reach a valid conclusion.

C does give you portability, a complicated algorithm that well written in C and tested and verified is hugely valuable because the same code can be taken to any platform and it will work, it will not need to be debugged.

I've written 64 bit concurrent heap managers in C for Windows. The design can sustain hundreds of concurrent threads allocating and freeing randomly and asynchronously 24/7 without failure. That took some time, but now that it's done I can port the code to any platform (if I ever needed to) and other than minor teething issues, be confident it will work.

That would be impossible to do if I had coded it in X64 assembler, despite the perceived benefits that might yield. Instead we build such code in C and then profile it under heavy load for extended periods and identify the hotspots, then we can scrutinize the hotspots and make those faster, that might even mean coding some small parts in assembler if that yields and overall benefit.

Compilers can do things that we cannot, a compiler will never fail to correctly manage a volatile variable, but an assembler has no idea what that means because assembler has no such concept and so the cost of having errant code managing concurrently accessed memory locations is huge, debugging such a system with assembly as the only abstraction is commercial suicide.

A compiler can trap or embed code to trap, illegal array subscripts, assembler cannot, the cost saving in a test failing with "Illegal subscript encountered" is huge compared to the system merrily stamping over memory and only later seeing a failure when the cause occurred an hour ago in some remote piece of code.

Compilers can 100% detect uninitialized variables and refuse to compile, assembler again has no idea.

These are the benefits you need to consider, they are meaningful, you believe your code is bug free but doesn't that just mean it has bugs that you've yet to expose?

Recall, 10% of the code uses 90% of the resources typically, there's no benefit in speeding up this or that area, if the overall benefit is not measurable or beneficial to the user.

So this is nothing to do with "standards" it is - or should be - dispassionate cost/benefit and nothing else.
 
Last edited:

MrSalts

Joined Apr 2, 2020
2,767
126 posts...
Learning c via posts on a forum. The documentation for c is Uber complete and must be faster to look at the table of contents, read the section and get the concept, the limitations and every detail. The one thing the manual won't do is give you a shoulder to cry on when you complain how different it is verses how you want it to be. Good luck.
 

Thread Starter

joeyd999

Joined Jun 6, 2011
6,331
126 posts...
Learning c via posts on a forum. The documentation for c is Uber complete and must be faster to look at the table of contents, read the section and get the concept, the limitations and every detail. The one thing the manual won't do is give you a shoulder to cry on when you complain how different it is verses how you want it to be. Good luck.
Eat me.
 

ApacheKid

Joined Jan 12, 2015
1,762
New question -- with a simple example:

Let's say I want to skip a few instruction cycles, say a series of 10 NOPs. I can write in my code Nop(); 10 times, obviously.

In .asm, I can also write 10 NOPs in succession, but I can write a macro like:

Code:
nopn    macro   numn
variable count=0
while (count<numn)
        nop
count=count+1
        endw
and invoke this as:

Code:
        nopn    10
And the macro expands at assembly time.

How do I do similar in C?
See: https://forum.allaboutcircuits.com/...mming-language-hpl.190133/page-2#post-1777281
 

xox

Joined Sep 8, 2017
936
Joey, you have a potential buffer overflow on line 110 of charlcd.c:

Code:
strcpy(lcd_line_buffer,messages[message]);
Just change it to this:

Code:
strncpy(lcd_line_buffer,messages[message], LCD_NUM_CHARS);
 

Thread Starter

joeyd999

Joined Jun 6, 2011
6,331
Joey, you have a potential buffer overflow on line 110 of charlcd.c:
Yup. And that would matter if I were taking input from external sources, which I'm not.

The strings are in a ROM array, always 16 characters, and zero terminated. No need for the size check.
 
Last edited:
Top