[OT] C Programming and the 68HC11 - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: [OT] C Programming and the 68HC11 (/thread-111486.html) |
[OT] C Programming and the 68HC11 - Ken Ratkevich - 04-02-2007 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.
"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 - The person formerly known as dot - 04-02-2007
Quote: 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;
Re: [OT] C Programming and the 68HC11 - Cameron Paine - 04-02-2007 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 - Cameron Paine - 04-02-2007 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 - Ken Ratkevich - 04-02-2007 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.
Re: [OT] C Programming and the 68HC11 - Alan Firth - 04-02-2007 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 - The person formerly known as dot - 04-02-2007 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 - The person formerly known as dot - 04-03-2007 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.
|