[OT] C Programming and the 68HC11



#9

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


#10

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;


}


#11

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


#12

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

#13

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
#14

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


#15

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.


#16

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.


Possibly Related Threads…
Thread Author Replies Views Last Post
  My greatest programming fault [OT] Tizedes Csaba [Hungary] 13 3,076 05-03-2005, 02:51 AM
Last Post: Palmer O. Hanson, Jr.
  Custom Programming vs. Pre-packaged programming Eddie Shore 3 1,544 01-24-2005, 03:42 AM
Last Post: Karl Schneider

Forum Jump: