asm or to c ???

takao21203

Joined Apr 28, 2012
3,702
That was the approach 20 or 25 years ago, optimize trivial instructions, and count cycles.

Since the Pentium age, the answer is MMX and other streaming extensions, and mulltiple cores.

Applications are no longer programmed in assembler, none of the mainstream stuff like Word or Skype or Excel is using it.

Video drivers use some MMX but also to some degree, via Intrinsics, or special compilers.

Forget about these optimizations it does not make a difference at the end of the day. Get your job done as quick as possible, if you dont get enough performance there are two approaches:

a) more powerful chip, higher clocking frequency, more memory
b) multiple controllers

b) might be favourable in some cases, where you only want 8bit architecture, but the MCU becomes clogged very much by display refresh for instance, so you do that with a seperate MCU in realtime, and send serial commands with a 10s of msecs. latency.

You can scale up like they do it with CPU cores in PCs, use 2, 4, 8, or more, just to keep everything to the same architecture (and same electrical specs).

Also C programs are optimized, not down to the bone, but good programmers know what to do, some constructs are just painfully slow. The extra instructions from a free compiler are a minor tradeoff, you dont get 4x speed just from optimizations, when you write good C code already.

It's a marketing gag.

Look at PIC32- you get very fast serial ports, and things like DMA. You dont need assembler anymore for time critical tasks.

If a PIC32 is still too slow, use a cluster of them, or another MCU with even more MHz.

When you look at the Manual for PIC32 and it's assembler instruction set, you know it's a lost cause, and when you compare the price of a small PIC32 to 8bit 16F, you also know optimizing a 16F assembler program for speed is a lost cause too.

I mean, do it some months, but dont get absorbed by it. It no longer plays an important role in the embedded world.

1. Get your job done.
2. If you have extra time, optimize it. If not, just move on to next project.
 

joeyd999

Joined Jun 6, 2011
5,234
That was the approach 20 or 25 years ago, optimize trivial instructions, and count cycles....
...

Q. I am learning PIC asm. Are there any resources that will help me?

A. Don't be a stupid head. Only idiots and children use asm. C is for superior beings.
This is why I don't get involved in these discussions anymore. There are situations, takao, where speed, power consumption, and/or cost are of primary concern with respect to the marketability -- and profitability -- of a product.

I design battery powered commodity products.

My customers first look at price vs. performance.
Secondly, they look at battery life.

Doubling the speed of a processor doubles the power consumption.
Doubling the memory size increases the price (though not double).

In my work, pennies and mA's count.

There are things I can do in asm, at nominal clock speeds and in small memory footprints, that are impossible to do in C -- at least not without inline asm code.

I, for one, do not wish to babysit a compiler. I prefer to babysit my code.

And, since I've already amassed dozens of asm libraries that I know, not only how they work, but also that they do, in fact, work, I save considerable time writing asm than if I were to write C.

EDIT: And, I don't lack C experience. I've written dozens of professional C and C++ applications in my career. On small processors, asm is simply a better fit for me.
 

takao21203

Joined Apr 28, 2012
3,702
OK, respect about your viewpoint.

I think

a) most devices these days use lithium, which has high capacity
b) you can switch clock speeds on modern chips
c) they are low voltage so that doubles battery life almost

d) what a gadget costs in China, and what is the retail price- can be factor 3 to factor 30.

25% more or less for the components are not cutting it at all, when you use good marketing.

The largest proportion (of the money) these day goes to landlords who own the retail shop property (at least in our country).

OK, inlline assembler, you win. Nobody stops you from using it.

But you dont really want to tell me, a gadget is computing all the time, and by using assembler you can run it at half speed, and double battery life?

A 32bit chip gives you XX MIPS per mA, and a 8bit chip gives you YY MIPS per mA.

In a portable device, most the power is used when illuminating the display, accessing SD card and transmitting. Often it will just sleep and use very little power.

Do you think anyone is programming one of these small TV BOX PCs in assembler, or one of these cheap tablets?

I dont think anyone is doing it. But these are mass produced. Mostly they run a kind of a Linux, and it is full of bugs and security holes and settings which dont do a thing- for sure, most of the computing power is just wasted.
 

NorthGuy

Joined Jun 28, 2014
611
Forget about these optimizations it does not make a difference at the end of the day. Get your job done as quick as possible, if you dont get enough performance there are two approaches:

a) more powerful chip, higher clocking frequency, more memory
b) multiple controllers
I agree. Wasteful mentality definitely prevails. People would rather spend more money than more effort. Most of the things are build by Frankenstein method - pieces are thrown together, then most of the effort comes into linking them together. Such approach is thought to be efficient. An idea of designing your own pieces is perceived as stupid.

BTW: MMX has been outdated too and replaced by SSE some 10 years ago.
 

nsaspook

Joined Aug 27, 2009
13,079
I agree. Wasteful mentality definitely prevails. People would rather spend more money than more effort.
There's a pretty simple and basic calculation that governs the universe of business when product cycle times are short.

More effort usually equates to more time and money. We can make more money but we can't make more time.
 

NorthGuy

Joined Jun 28, 2014
611
There's a pretty simple and basic calculation that governs the universe of business when product cycle times are short.

More effort usually equates to more time and money. We can make more money but we can't make more time.
You misenderstood me. I didn't say anything about time. However, if you put good effort into reducing your time-to-market through careful planning, you're likely to get your thing to market faster.

What is interesting is that Frankenstein mentality is also prevalent in hobby projects, where you would expect the goal would be to put more efforts into the project so that you would have something to be really proud of.
 

MaxHeadRoom

Joined Jul 18, 2013
28,617
I figured when this thread was started that it would degenerate into one side A versus C, one side against the other.
In my case I program Pic as a paying hobby with small run designs. I program in Assy, because I learned this way back in the PDP8 days.
I enjoy programming and learning PIC assembly and that is probably the way I will continue.
I do not punch a clock with the pressure to produce.
So I make no apologies. nor do I intend to.:cool:
Max.:)
 
Last edited:

takao21203

Joined Apr 28, 2012
3,702
It's like if using a powerful chip would be a crime and you aren't allowed to waste just one byte.

Forever damned to juggle with 120 bytes and a few K Flash, and if you use assembler, you can do amazing things with that.
 

THE_RB

Joined Feb 11, 2008
5,438
It should as that's a basic compiler optimization (eliminating unnecessary loads and stores/code selection) for anything but a crippled 'free' compiler.
...
In my experience that won't be the case. With;
byte var1 |= byte CONSTANT

the compiler will always be faithful to the fact you told it to do a logical operation of one byte value and another.

What if you code said;
byte var 1 |= byte CONSTANT; // where CONSTANT = 0b00000101

Do you think the optimising compiler would replace that with TWO bsf instructions? Well of course that would not be "optimising" as it is no smaller than byte logical operation. But the whole concept still seems funky to me.

Of course that's just my experience, and your compiler might be different? I'd like to see the ASM dump after compilation.
 

NorthGuy

Joined Jun 28, 2014
611
What if you code said;
byte var 1 |= byte CONSTANT; // where CONSTANT = 0b00000101

Do you think the optimising compiler would replace that with TWO bsf instructions?
If var1 is a variable in RAM then it could do this if it wanted to preserve WREG, although compilers usually don't think that well.

If writing to SFR then using two bsf instructions would be a bug, because "or" turns both bits at the same time, but 2 bsfs don't. Optimization can improve code, but it cannot alter the functonality.
 

ErnieM

Joined Apr 24, 2011
8,377
I had access to a PDP8L in junior high school, circa 1970. We used FOCAL as the high level language for programming.

Output was on a teletype, which also punched paper tape for "mass storage" of programs.
 

ErnieM

Joined Apr 24, 2011
8,377
What if you code said;
byte var 1 |= byte CONSTANT; // where CONSTANT = 0b00000101

Do you think the optimising compiler would replace that with TWO bsf instructions? Well of course that would not be "optimising" as it is no smaller than byte logical operation. But the whole concept still seems funky to me.
As two BSF instructions are no shorter than a MOVLW / IORWF pair there is no optimization with that method.
 

MrChips

Joined Oct 2, 2009
30,706
My first exposure to ASM was on a CDC6400 in 1971.

My first real working experience and understanding of ASM was on a PDP8/S in 1972.
Yeah, source file was punched on paper tape which was then read back in for assembly.
When the paper tape reader or punch didn't work we had to resort to the Teletype running at 110 baud.

One of my jobs required setting the baud on the Teletype using a stroboscope.
 

takao21203

Joined Apr 28, 2012
3,702
my first exposure was BASIC on the IBM PC, DOS 2.1, and 5 1/4 disk drives.

Not assembler.

Later I played around with 68000 assembler, and 8086 assembler.
then I learned Visual Studio C++, not C as first language.

In the 2000s I used PIC assembly language, for too long, I think, I stopped when I changed to 18F and my programs became hard to read.

C helped me to reduce number of lines, and keep it portable among different PICs, and not to bother about banking anymore.
 

MrChips

Joined Oct 2, 2009
30,706
For my course project on the PDP8, I wrote a program to play chess against the computer.
When required you could request a printout on the Teletype of the chess board and pieces. It took a long time to print.

Total memory on the PDP8/S was 4k.

I don't remember how much memory the program needed but it was all written in ASM.
C wasn't available then. FOCAL would have been the only other option.
 
Top