The Museum of HP Calculators

HP Forum Archive 17

[ Return to Index | Top of Index ]

[OT] C Programming and the 68HC11
Message #1 Posted by Ken Ratkevich on 2 Apr 2007, 7:29 a.m.

Greetings,

I know this is totally OT and I will delete it promptly if deemed innapropriate. However, I also know there are many code gurus and smart fellows that frequent this forum and that this is THE place to get a definitive answer.

I am currently involved in a home study course for my A.A.S degree in electronics. I am just finishing a module on the 68HC11 microcontroller. All programming up to this point has been assembly and machine code. But, at the very end, they stuck in a VERY brief intro to C programming and added a few questions in the exam concerning C programs for the 68HC11. I am having a problem with one very simple question. The question reads:

"Write a small program that will multiply the character variables X and Y if Y>X; otherwise it will perform an integer divide, X/Y."

My concern is that if I declare X and Y as "char" data types, the compiler will only RMB 1 for each. In this case, I think the IDIV instruction wont work properly because it fetches a 16 bit dividend and divisor. In either case I think I should declare Z (product or quotient) as "int" data type because bothe the MUL and IDIV instructions produce a 16 bit result. I am not sure how to handle this. Any help would be appreciated.

This is not cheating since this exam is open book and I am allowed to use "any resource" available.

Ken

      
Re: [OT] C Programming and the 68HC11
Message #2 Posted by the person formerly known as dot on 2 Apr 2007, 8:09 a.m.,
in response to message #1 by Ken Ratkevich

Quote:
My concern is that if I declare X and Y as "char" data types, the compiler will only RMB 1 for each. In this case, I think the IDIV instruction wont work properly because it fetches a 16 bit dividend and divisor.

Who cares? It's the compilers problem, not yours. If it can't use one instruction it will generate others that will give the correct result.

The whole point of using compilers and writing in C is so you don't have to worry about issues like these.

This function will work on any C compiler on any chip:

int doCalculation (char X, char Y){

if (Y>X) return X*Y; return X/Y;

}

            
Re: [OT] C Programming and the 68HC11
Message #3 Posted by Cameron Paine on 2 Apr 2007, 9:16 a.m.,
in response to message #2 by the person formerly known as dot

An observation, nothing more. The return value of your function will be widened to int. Depending on input values, this may result in an unintended side-effect. Perhaps this is one of those instances that aren't simply the compiler's problem.

Cameron

                  
Re: [OT] C Programming and the 68HC11
Message #4 Posted by the person formerly known as dot on 2 Apr 2007, 5:55 p.m.,
in response to message #3 by Cameron Paine

That was intentional... which was why I made the function return int. The question didn't specify anything about the return value type.

      
Re: [OT] C Programming and the 68HC11
Message #5 Posted by Cameron Paine on 2 Apr 2007, 9:07 a.m.,
in response to message #1 by Ken Ratkevich

I'd take the former dot's response a little further: think of C as the target architecture that you're writing for. Knowledge of the actual platform is a distraction that should be ignored unless the problem you're trying to solve refuses to let it be. The question you ask is not such a problem; ejecting a char-sized object through an I/O port might be one that is.

An alternative solution:

return Y > X ? X * Y : X / Y;
Cameron
      
Re: [OT] C Programming and the 68HC11
Message #6 Posted by Ken Ratkevich on 2 Apr 2007, 9:58 a.m.,
in response to message #1 by Ken Ratkevich

Thanks to all for your input. You've both confirmed my assumption that it it the job of the compiler to generate the appropriate code for the target platform. I think I will like C (C++, C#) when I get to them in a more detailed lesson.


Ken

            
Re: [OT] C Programming and the 68HC11
Message #7 Posted by Alan Firth on 2 Apr 2007, 1:15 p.m.,
in response to message #6 by Ken Ratkevich

It may be a moot point to you, but if you get hooked by the microcontroller bug, check the Microchip website. They have a very good *free* development environment, and a 60-day evaluation of their C compiler (the 'student version') for the '18' series chips. (The evaluation doesn't expire - it has slightly reduced functionality after 60 days)

Programmers are available from Microchip, as well as several other sources.

(I'm a 'Microchip jingoist')... not connected with them in any way.

                  
Re: [OT] C Programming and the 68HC11
Message #8 Posted by the person formerly known as dot on 3 Apr 2007, 4:08 a.m.,
in response to message #7 by Alan Firth

PICs are OK, but I'd strongly recomend the Atmel AVR series. I think they're better then PICs in many ways (32 registers vs a single 'W' register, faster, very cheap Dragon debugger) but the best thing is the free GCC port. Combined with AVR studio, you get a C IDE + simulator for free.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall