Page 215 - Programming Microcontrollers in C
P. 215

200    Chapter 4  Small 8-Bit Systems

                   0151 0F 09 FD BRCLR 7,$09,$0151
                   while(AD_CTST.COCO==0);
                   0154 B6 08 LDA $08  return AD_DATA;
                   0156 81        RTS
                                           }


                   0157 BF 5A STX $5A
                   0159 B7 5B STA $5B
                   015B 4F        CLRA
                   015C 5F        CLRX
                   015D 5C        INCX
                   015E 38 5B LSL $5B
                   0160 49        ROLA
                   0161 B0 5A SUB $5A
                   0163 24 03 BCC $0168
                   0165 BB 5A ADD $5A
                   0167 99        SEC
                   0168 59        ROLX
                   0169 24 F3 BCC $015E
                   016B 53        COMX
                   016C 81        RTS


                   1FFE 01 00
                          The assembly code to execute this single line of C code is found in
                          the address range $10c to $11b, or 17 bytes of code. However,
                          there is a call to the double precision add routine at address $115.
                          This routine occupies the address range $157 to $16c, or 22 addi­
                          tional bytes of code hidden from the main routine. Therefore, this
                          requires 37 bytes of code for its execution.
                              The next approach is to avoid the overflow problem by dividing
                          by 2 each of the terms to be added prior to the addition. Division by
                          2 is accomplished by shifting each of the terms to the right by one
                          bit. This approach guarantees that there will be no overflow into the
                          higher byte because the most significant bit of each number will be 0
                          after the shift, and no binary addition can cause more than a 1-bit
                          overflow. Therefore, the sum will at most have its most significant
                          bit turned on.  This number is then stored in the location
                          adc_data[j].
   210   211   212   213   214   215   216   217   218   219   220