Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Choosing a language

Status
Not open for further replies.
So me and my physics teacher have decided to throw together some PIC projects and what not in some free time, but I wanted to properly start learning the language.

My teacher loves Assembly and has used it for years, but, has said about C being more advanced and functional. Advanced and functional is what I like.

So, what would you guys say? Pros and Cons for each?
 
This has been debated a number of times before, not only on ETO, but other places on the web as well. Open your peepers and have a look!
 
on a slight more helpful note............. i dont think there is a diffinative answer the bland and probally most correct answer would be depends on the job you need to do so you can pick the language that has the most advantages towards what you want to achieve..
now a more personal view.... i am a complete beginner at micro's and have tried bith C and ASM. i gave up on ASM fairly early despite trying to get to grips with it using Nigels exceptionaly good tutorials. i then moved to C using 3v0's tutorials and help. i prefer C but again i have used it alot more, sometimes i wish i had stuck at ASM a bit more as there is no arguing that by using it you get a deeper more complete understanding of the hardware your using. C offers a quicker learning route and takes some of the boreing bits out of codeing because you only have to learn the language (especialy with compilers like mikroc) and not so much of the hardware. i find i can read and understand a C file way better than a ASM file, for sure it will be different for others.
hmm bottom line i prefer C but wish i had the paitence to learn more ASM so i had a better understanding of how the pics work
regards jason
 
With ASM the programmer is in charge of everything that happens on that chip. With C, the compiler is in charge of how the machine code gets written so it's more in control of the chip than you are.

ASM, while it may take longer to write, can be beneficial for many reasons. With ASM, you can write far more optimized code than a compiler can, which takes up less space in program memory and you get faster execution time since the chip isn't having to waste time executing redundant instructions that the compiler generated, making ASM a much more efficient coding language.

Why some people prefer C over ASM? C exchanges human time for computer time as with C you have a compiler that handles most of the housekeeping that the programmer would have to deal with in ASM, like RAM bank selecting, dealing with memory boundary/paging bits in PCLATH, etc etc. The trade off here is that compilers most of the time generate a "bloated" hex file over what a good programmer can write in ASM.

As an example, here is a software SPI bit bang routine written by me in ASM -

Code:
SWSPIWrite		movwf		0x72
			movlw		0x08
			movwf		BITCOUNT
			bcf		PORTA,0
			btfsc		0x72,7
			bsf		PORTA,0
			bsf		PORTA,2
			bcf		PORTA,2
			decfsz		BITCOUNT,F
			goto		$-6
			return

Here is the same thing written in C -
Code:
void SWSPIWrite (unsigned char byte)
{
    Clk_Pin = 0;                                                //init SCK
    Strobe_Pin = 0;                                             //init RCK
    unsigned char BITCOUNT = 0;
    for(BITCOUNT = 0; BITCOUNT < 8; BITCOUNT++)
    {
	Data_Pin = 0;						//default MSB = 0
	if(byte & 0b10000000)					//if MSB = 1
	{
		Data_Pin = 1;					//send 1
	}
    	Clk_Pin = 1;						//strobe clock
	Clk_Pin = 0;
	byte = byte << 1;					//shift bits left
    }
}

And here is the compiler generated ASM from the above C code using Hitech PICC Lite -

Code:
			MOVWF		 0x72
	                BCF		 STATUS, 0x5
			BCF		 STATUS, 0x6
			BCF		 PORTA, 0x2
			BCF		 PORTA, 0x1
			BCF		 STATUS, 0x0
			MOVLW		 0x0
			BTFSC		 STATUS, 0x0
			MOVLW		 0x1
			MOVWF		 BITCOUNT
			BCF		 STATUS, 0x0
			MOVLW		 0x0
			BTFSC		 STATUS, 0x0
			MOVLW		 0x1
			MOVWF		 BITCOUNT
			MOVLW		 0x8
			SUBWF		 BITCOUNT, W
			BTFSS		 STATUS, 0x0
			GOTO		 0x7AA
			GOTO		 0x7AB
			GOTO		 0x7AD
			GOTO		 0x7C6
			GOTO		 0x7C6
			MOVLW		 0x1
			MOVWF		 0x70
			MOVF		 0x70, W
			ADDWF		 BITCOUNT, F
			MOVLW		 0x8
			SUBWF		 BITCOUNT, W
			BTFSS		 STATUS, 0x0
			GOTO		 0x7C4
			GOTO		 0x7C5
			GOTO		 0x7AD
			GOTO		 0x7C6
			BCF		 PORTA, 0x0
			BTFSS		 0x72, 0x7
			GOTO		 0x7B1
			GOTO		 0x7B2
			GOTO		 0x7B3
			BSF		 PORTA, 0x0
			BSF		 PORTA, 0x2
			BCF		 PORTA, 0x2
			MOVF		 0x72, W
			MOVWF		 0x70
			ADDWF		 0x70, W
			MOVWF		 0x71
			MOVF		 0x71, W
			MOVWF		 0x72
			RETURN

As you can see...ASM is a MUCH more efficient coding language from the standpoint that you can write much 'tighter' code with it than what a C compiler can generate.
 
Last edited:
ASM is more efficient, but C is a good one to learn in school. It is easy to follow and you do not need to memorize all the different words, like bcf, movlw, movwf, etc. I personally prefer C, and I've never had a problem with it.
 
The idea that you don't get an understanding of the hardware by using C is just plain wrong. The exception to this is if you use other peoples C code (I.E the supplied libraries).

For example, to setup a 16F88 for 9600 baud RS232, in asm we do,
Code:
	movlw	.50		;9600 baud
	banksel SPBRG
	movwf	SPBRG
	movlw	b'00110100'	;setup transmit parameters
	banksel	TXSTA
	movwf	TXSTA
	movlw	b'10010000'	;setup receive parameters
	banksel	RCSTA
	movwf	RCSTA
In C we do,
Code:
	SPBRG=50;		//9600 baud
	TXSTA=0b00110100;	//setup transmit parameters
	RCSTA=0b10010000;	//setup receive parameters
With either bit of code I need to read the data sheet. BTW, the asm above is 12 instructions, the C is only 9 so C is 25% more efficient. (using BoostC). Also, I prefer to write 3 lines rather than 9!!

The other reason often quotes is efficiency. It is true that asm can (see above) be more efficient if written by a good programmer, however, we are generally talking 1 or 2 percent better efficiency. Timing critical routines are the exception and these can still be written using asm in a C program.

So, in case anyone hasn't guessed, I recommend C without library routines.

Mike.
 
Jon!! A wise man once told me... Yes! ASM is faster, more optimised etc.. However the makers of really decent compilers are extremely efficient assembly writers.. Any one can write a really bad asm code.. at least with C it can be optimised!!!

I can write in assembly or C or C++ or any high level language.... I produce the same results in any language...
 
Granted the compiler that compiled the above code was Hitech PICC Lite, which is the "free" compiler so in Lite mode it offers almost zero optimization (which I did mention that it was Hitech PICC Lite). But to me generating that many instructions to do something that I could do with 1/3rd the instructions in ASM was absolutely absurd to me.
 
Could you tell me were to find 3v0's tutorials. I leant assembly and been using it for 3 years, now i want to learning C.
But can not find any nice tutorials. Some thing like Nigels but using C.
The main reason why i want to use C is that i want to start using PIC32.

David
 
Hmm, all very interesting. I've started with Assembly seeing as it's what my current tutor uses and has been mentioned on the electronics engineering university course I want to take.

Although, I wish someone made some Assembly tutorials like 3v0s C ones. Nigel's are great, but they feel like they're not for complete beginners. I've picked up a lot of general knowledge from 3v0s first few though, so to any future programmers that are starting off and stumble upon this from google, check them out.
 
Although, I wish someone made some Assembly tutorials like 3v0s C ones.

If you really want to learn assembly, I suggest you use these tutorials:
**broken link removed**
that's where I first started learning, and it helped a lot. Of course, code will look different for different chips, but at least you can learn the basic ASM actions.
Der Strom
 
1. program in whatever language you or your boss desires.

2. see #1

But which is better ASM or a high level language. Lets hold off on that for just a minute and look at three ways to program.

ASM provides one level of abstraction from writing machine code directly in HEX or binary. In its most simple form each ASM instruction results in one machine operation. This is very informative if you are interested in how the machine's CPU operates. ASM does not get you any closer to the peripherals or their registers.

A high level language allows you to more directly express the program and leave the machine level details to the compiler. This additional level of abstraction allows you to write code that is easier to read and port. It is also faster to code. Depending on the language we may also get variable scoping.

The next major level of abstraction is object oriented language which attempt to have the code better reflect the system it controls. Here data and the operations on it are encapsulated into objects and classes of objects. In a home control system we may have a class of objects called lights. Instead of simply setting a variable to on or off we request a light object to turn itself on or off. We could create a security light so that it would never turn itself off at night and a bathroom light to turn off after a period of time or have it communicate with a motion sensor.

OK now here is the kicker. A good programmer can achieve any level of abstraction using ASM. He can also create object oriented code in non object languages like C or Basic. But using the tool designed to work at the right level of abstraction will make the job a lot easier with fewer bugs.

So a better question would be to ask: What is the correct level of abstraction to use in programming?

If you want an unbiased answer talk to the guys who have fluently coded at all 3 levels.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top