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.

Hi-Tech C - Problem converting to assembly

Status
Not open for further replies.

MrNobody

New Member
Hi,
I have written simple code using Hi-Tech C in MPLAB. When I compile it, the Disassembly Listing shows a weird result.

Here is the code in Hi-Tech C
Code:
#include <htc.h>
void main ()
{
	unsigned char a;
	while (1)
	{

		a = 1;
		a += 1;
		a = 10;
		a = 20;	
	}
}


Here is the code in Disassembly Listing
Code:
1:                 #include <htc.h>
2:                 void main ()
3:                 {
4:                 	unsigned char a;
5:                 	while (1)
   001    2801     GOTO 0x1

This is when the optimization is turned off. Any solution?
Please advice.
Thanks.
 
It looks like the compiler realizes that you are not using a for anything and it has been optimized. To put it another way the code it generates does exactly what you have asked for from an external viewpoint.

Turn off optimization.

Hi,
I have written simple code using Hi-Tech C in MPLAB. When I compile it, the Disassembly Listing shows a weird result.

Here is the code in Hi-Tech C
Code:
#include <htc.h>
void main ()
{
	unsigned char a;
	while (1)
	{

		a = 1;
		a += 1;
		a = 10;
		a = 20;	
	}
}


Here is the code in Disassembly Listing
Code:
1:                 #include <htc.h>
2:                 void main ()
3:                 {
4:                 	unsigned char a;
5:                 	while (1)
   001    2801     GOTO 0x1

This is when the optimization is turned off. Any solution?
Please advice.
Thanks.
 
When I compile the same code in BoostC I get the same thing whether optimization is enabled or not:
Code:
---  C:\MCU\18F248\blinky\mr_nobody.c  
-----------------------------------------------------------
1:                 #include <system.h>
2:                 
3:                 #pragma CLOCK_FREQ 20000000
4:                 #pragma DATA    _CONFIG1H, _HS_OSC_1H
5:                 #pragma DATA    _CONFIG2H, _WDT_OFF_2H
6:                 #pragma DATA    _CONFIG4L, _LVP_OFF_4L
7:                 
8:                 void main(void)
9:                 {
10:                	unsigned char a;
11:                	while(1){
  0012    D7F8     BRA 0x4
12:                		a = 1;
  0004    0E01     MOVLW 0x1
  0006    6E01     MOVWF 0x1, ACCESS
13:                		a += 1;
  0008    2A01     INCF 0x1, F, ACCESS
14:                		a = 10;
  000A    0E0A     MOVLW 0xa
  000C    6E01     MOVWF 0x1, ACCESS
15:                		a = 20;	
  000E    0E14     MOVLW 0x14
  0010    6E01     MOVWF 0x1, ACCESS
 
It doesn't look the same to me. Boost is changing the variable. HiTech is just sitting in an endless loop. Unless I'm missing something here?

Looks like what 3v0 said:
To put it another way the code it generates does exactly what you have asked for from an external viewpoint.
 
Last edited:
It doesn't look the same to me. Boost is changing the variable. HiTech is just sitting in an endless loop. Unless I'm missing something here?
What I meant was that optimized or unoptimized I get the same thing - what I listed there. :p

The OP said he had optimization turned off though. Maybe he's mistaken?
 
What I meant was that optimized or unoptimized I get the same thing - what I listed there. :p

I wasn't smoking anything! OK, I'm enrolling in reading 101. :p

The OP said he had optimization turned off though. Maybe he's mistaken?

Well it would make sense if the optimization was on, but yes...
 
I missed the part about optimization being off. But it still looks like it is on.

Try modifying the code by setting PORTA (or some other IO port) to the value of A somewhere in the code.

Code:
#include <htc.h>
void main ()
{
	unsigned char a;
	while (1)
	{

		a = 1;
		a += 1;
		a = 10;
		a = 20;	
                 PORTA=a;   // register name may differ in hi-tech
	}
}

Then try

Code:
#include <htc.h>
void main ()
{
	unsigned char a;
	while (1)
	{

		a = 1;
                 PORTA=a; 
		a += 1;
                 PORTA=a; 
		a = 10;
                 PORTA=a; 
		a = 20;	
                 PORTA=a; 
	}
}
They will tell you if optimization is the problem.

EDIT: Or just declare var a as
volatile unsigned char a;
 
Last edited:
3v0:

I tried the code below and it compiled correctly. I also tried using volatile unsigned char a; and it also compiles correctly.
Seems that the optimization is still on. Attached is a print screen of the optimization setting. Not sure if I did it correctly..

Code:
#include <htc.h>
void main ()
{
	unsigned char a;
	while (1)
	{

		a = 1;
		a += 1;
		a = 10;
		a = 20;	
                PORTA=a;   // register name may differ in hi-tech
	}
}

Hmm.. normally, if the compiler 'over optimizes' the code up to the point where the code doesn't work as intended, how to solve it..? Manually insert the required inline assembly in C?
 

Attachments

  • Optimization.jpg
    Optimization.jpg
    98.6 KB · Views: 219
I do not use this compiler.

But if you are going to be debugging I would think the debug box should be checked, that should cause the compiler to optimize less (or none) so the generated code can be more easily understood while using the debugger. The box is grayed out so I am not sure what that is about. You best bet would be to ask on HiTech forum.

Hmm.. normally, if the compiler 'over optimizes' the code up to the point where the code doesn't work as intended, how to solve it..? Manually insert the required inline assembly in C?

A compiler should not (intent is that it will not) do that. In you example the var a is not volatile and never used so the compiler is justified in ignoring it. It is code that has no effect so it is tossed. Why? Because tossing it does not change your program in any functional way.

I hope I answered your questions. If not I will try again if you restate them for me.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top