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.

One intruction in hitech c

Status
Not open for further replies.
It depends on the instruction MCU and frequency..

Take the PIC @ 4MHz this has 1μS per instruction.

If you use a simple one like "a++;"... Then its one instruction cycle ie.. 1μS

worst case is loops..."While ();" takes about 6 instruction cycles..
 
Last edited:
I agree with Ian but need to point out the problem with the wording of the question.

We have a problem with terms here. There are only machine instructions. High level languages like C are translated by the compiler to machine instructions. There are no C or HTC instructions.

So any given machine instruction will execute in the same time regardless of how it was generated.


I hope that helps.
 
Here maybe this will put light on this
Code:
#include <htc.h>
#pragma config FSCM = OFF, IESO = OFF, OSC = INTIO2	

int mask = 0b11110000;
int fourbits; 

void main()
{
	ADCON1 = 0x70;
	TRISB = 0b00000000;
	PORTB = 0b01010101;
	
	fourbits = 0b1000;

	PORTB = PORTB & mask;
	
	PORTB = PORTB | fourbits;

}


Now lets look at fourbits using the compiler asm generated file.

Code:
13:                	fourbits = 0b1000;
   FE8    0E00     MOVLW 0
   FEA    6E02     MOVWF 0x2, ACCESS
   FEC    0E08     MOVLW 0x8
   FEE    6E01     MOVWF 0x1, ACCESS
14:
As you can see to load fourbits takes 4 instructions just for fourbits =0b1000
 
I build a similar example code with different optimizations and below are the results. After the first example I made some important changes to the code - I changed all global variables to local ones with as small scope as possible and the code size dropped significantly. The most important change is that the variables are no longer allocated to RAM, but to registers. This makes the code much more efficient.

Code:
#include <avr/io.h>  // Include register definitions

int mask = 0b11110000;
int fourbits;

int main(void)
{
	// Infinite loop
    while(1)
    {
		DDRB = 0b00000000;
		PORTB = 0b01010101;
		
		fourbits = 0b1000;
		
		PORTB = PORTB & mask;
		
		PORTB = PORTB | fourbits;
    }
}

No optimization:
Program Memory Usage : 310 bytes
Data Memory Usage : 4 bytes

Optimize -O1:
Program Memory Usage : 258 bytes
Data Memory Usage : 4 bytes

Optimize More -O2:
Program Memory Usage : 254 bytes
Data Memory Usage : 4 bytes

Optimize Most -O3:
Program Memory Usage : 254 bytes
Data Memory Usage : 4 bytes

Optimize Size -Os
Program Memory Usage : 254 bytes
Data Memory Usage : 4 bytes


Change global variables to local.

Code:
#include <avr/io.h>  // Include register definitions

int main(void)
{
	// Infinite loop
    while(1)
    {
		int mask = 0b11110000;
		int fourbits;
		
		DDRB = 0b00000000;
		PORTB = 0b01010101;
		
		fourbits = 0b1000;
		
		PORTB = PORTB & mask;
		
		PORTB = PORTB | fourbits;
    }
}

No optimization:
Program Memory Usage : 304 bytes
Data Memory Usage : 0 bytes

Optimize Most -O3:
Program Memory Usage : 228 bytes
Data Memory Usage : 0 bytes

Conclusion:
One "instruction" in C program can be very complex or very simple. Sometimes a whole line of C code can be useless and is optimized away by the compiler. Different compilers produce different (assembly) code. Different optimization levels produce different code. If you change one line of code, it can affect the produced assembly code dramatically somewhere else. So, there are really no one answer to "how long does one line of code take to execute". If you really want to know, you need to look at the produced assembly code.. or "disassembly".

EDIT: Here are the two disassemblies

Global variables, -O3:
Code:
00000068  LDS R21,0x0100		Load direct from data space 
		PORTB = 0b01010101;
0000006A  LDI R20,0x55		Load immediate 
		fourbits = 0b1000;
0000006B  LDI R24,0x08		Load immediate 
0000006C  LDI R25,0x00		Load immediate 
		DDRB = 0b00000000;
0000006D  OUT 0x17,R1		Out to I/O location 
		PORTB = 0b01010101;
0000006E  OUT 0x18,R20		Out to I/O location 
		fourbits = 0b1000;
0000006F  STS 0x0103,R25		Store direct to data space 
00000071  STS 0x0102,R24		Store direct to data space 
		PORTB = PORTB & mask;
00000073  IN R18,0x18		In from I/O location 
00000074  AND R18,R21		Logical AND 
00000075  OUT 0x18,R18		Out to I/O location 
		PORTB = PORTB | fourbits;
00000076  IN R18,0x18		In from I/O location 
00000077  LDS R19,0x0102		Load direct from data space 
00000079  OR R18,R19		Logical OR 
0000007A  OUT 0x18,R18		Out to I/O location 
0000007B  RJMP PC-0x000E		Relative jump


Local variables, -O3:
Code:
		int mask = 0b11110000;
		int fourbits;
		
		DDRB = 0b00000000;
		PORTB = 0b01010101;
00000068  LDI R25,0x55		Load immediate 
		DDRB = 0b00000000;
00000069  OUT 0x17,R1		Out to I/O location 
		PORTB = 0b01010101;
0000006A  OUT 0x18,R25		Out to I/O location 
		PORTB = PORTB & mask;
0000006B  IN R24,0x18		In from I/O location 
0000006C  ANDI R24,0xF0		Logical AND with immediate 
0000006D  OUT 0x18,R24		Out to I/O location 
		PORTB = PORTB | fourbits;
0000006E  SBI 0x18,3		Set bit in I/O register 
0000006F  RJMP PC-0x0006		Relative jump
 
Last edited:
That's funny I'm using XC8 and it said I used 38 bytes program memory and 4 bytes data.
I didn't use optimization because I'm saving my 60 days of it for USB code that really needs it.
 
One more time with feeling !

It is well understood that in asm there is a one to one mapping between opcodes and machine instructions. High level languages, HLLs, add a level of abstraction between what we write and what the machine executes. It is up to the compiler to select which and how many machine instructions are used.

In c we have lines of code made up of keywords. operators, variables, and etc. Talking about c instructions is meaningless. We can and do talk about the machine instructions generated by the c compiler.

While I am at it the language is properly call c rather then C.
 
Well I thought I showed that It's not that easy and there are lot's of ways to do the same thing in code.

And this is so true
Talking about c instructions is meaningless.
But i like it. c is more inline with one's thinking I think.

But if the OP needs tight code he needs to look at
Code:
#asm
MOVLW 0xAA
BANKSEL (PORTA)
MOVWF BANKMASK(PORTA)
BANKSEL (PORTB)
BSF RB1_bit
#endasm
 
While I am at it the language is properly call c rather then C.

What? Are you drunk in oklahoma usa rather then Oklahoma USA?
 
Last edited:
What? Are you drunk in oklahoma usa rather then Oklahoma USA?

Sorry about that.. actually I came home pretty drunk myself and wrote that :) I'm a big C fan.. pun intended.
 
The profs at my university were sticklers about stuff like this. They also provided a lot of historical perspective as the more elderly ones lived the early days of computing. The late Prof Hare was not there the day the found the original bug. But he was on the first name basis with the people who did.

We use all caps for an acronym name like BASIC, from Beginner's All-purpose Symbolic Instruction Code.

The first letter is capitalized if the computer language is named after a person aka Pascal.

The language c does not fall under either of these two rules so it is just c.

I doubt it will cost anyone a job if he gets any of this wrong but there is no harm in getting it right Or maybe at this late date nobody cares one way or another.
 
Last edited:
The language c does not fall under either of these two rules so it is just c.

If the creators name it "C", then C it is.. Just like LaTeX is LaTeX because that is what the creators want it to be. You don't have to blindly believe everything your professor says.
 
3v0 don't let people move you you have a point and it makes sense after all C++ was just a name so C could be trade marked.


I spent some hours digging it had at one time been c but not much about it as to why.
 
Status
Not open for further replies.

Latest threads

Back
Top