CompuServe Messages

#C and Assembler

    25-Jan-89 08:55:34
Sb: #31970-#C and Assembler
Fm: Fred Hewett 76046,3666
To: Drew Lucy 76337,75
Your msg caught my attention because I write 68000 assembly language five days a week at work and only write C on my Amiga. Regarding the last question in your msg: No the 68000 (and 68010) do shifts much faster than divides, even divides by powers of two. (The 68020 has a barrel shifter to speed up shifts even more!) The exact timing of the divide instruction seems hard to predict, but I haven't seen times less about 110 CPU clocks. The timing of the shift instructions is easily computed: 2 clocks per bit plus 6 if byte or word; 2 more clocks if long. For small shifts the difference between shift and divide can be a factor of ten or more. In general, I've noticed that C compilers produce pretty lousy code, at least in comparison with the compilers fr, say, FORTRAN that I've seem on mainframes. In part, this is because C is so low-level: if you want to do a shift, you can use the shift operator. However, I don't see much use of even simple optimizations I learned in college, like strength reduction, moving invariant code out of loops, removing dead code, to say nothing of global flow optimization. All that notwithstanding, it's probably a bad idea to mix assembly language and C unless the CPU speed is critical. You reduce readability and portability. Even if you halve CPU execution time, you probably reduce the wall-clock time by much less, unless you're doing ray-tracing or some such.