Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
07/29/06 08:31
Read: times


 
#121302 - Optimisations
Responding to: ???'s previous message
Whenever you're repeating much the same thing over again you can usually use a loop. Also, with your strings, most of them are the same execpt for the number and on or off.

So you could split the strings as such:

device_str db CR,LF,'Device.',0
on_str db 'ON',0
off_str db 'OFF',0

You've saved quite a few bytes just by doing this.

To loop you stats code you could do something like this (don't expect it to be 100% correct!!!)
;
; uses R1,R2
;
print_status:
mov r1,device ;I normally have constants as upper case
mov r2,#0 ;input#
ps_lp:
mov dptr,#device_str
acall pstring ;print string->dptr
mov a,r2 ;get input#
add a,#'0' ;convert # to ascii
acall pchar ;out the serial port

mov a,r1 ;get bit statuses
rrc a
mov r1,a ;shift bit status into Carry
jc ps_on

mov dptr,#off_str ;if status was OFF
sjmp ps_1
ps_on:
mov dptr,#on_str ;if status was ON
ps_1:
acall pstring

inc r2 ;next input
cjne r2,#8,ps_lp
ret ;job done


As you can see the code is a lot more compact and still has the same ouput. I usually have routines called pstring and pchar. pchar just sends a character out the serial port (or to a lcd) and pstring does the same as you routine named disp_message except it would call pchar for the character output. This means if you wanted to change the output destination, you would only have to change the pchar code. Sure, doing this means you use more stack, but unless pressed, it makes you life easier.
If you divide your routines up nicely, then that makes changes a lot easier in the future.

List of 33 messages in thread
TopicAuthorDate
code optimisation            01/01/70 00:00      
   Optimisations            01/01/70 00:00      
   problem solved            01/01/70 00:00      
      good closure!            01/01/70 00:00      
         and the very best ...            01/01/70 00:00      
            Isn't that why we have "tools"?            01/01/70 00:00      
               not quite and a good read            01/01/70 00:00      
               A Posidriv driver and a Phillips screw            01/01/70 00:00      
                  I have done this too, I think you are wrong            01/01/70 00:00      
                     Best Ways?            01/01/70 00:00      
                        what is best?            01/01/70 00:00      
                           Guide lines and trade-off\'s            01/01/70 00:00      
                              what about 'and'            01/01/70 00:00      
                                 Dangerous Information            01/01/70 00:00      
                                    depends on what you define as overhead            01/01/70 00:00      
                                       Overhead            01/01/70 00:00      
                                          the whole truth, please            01/01/70 00:00      
                                             one good thing...            01/01/70 00:00      
                                             CPU Overhead            01/01/70 00:00      
                                                there is an article 'somewhere' that cover C for            01/01/70 00:00      
                                                   the real optimum            01/01/70 00:00      
                                                      Your time is appreciated            01/01/70 00:00      
                                                   how about these?            01/01/70 00:00      
                                                      a required capability for optimum code            01/01/70 00:00      
                        small and readable            01/01/70 00:00      
                           my sentiment            01/01/70 00:00      
                              In that spirit ... where's the user guide?            01/01/70 00:00      
                                 Did you download the documentation            01/01/70 00:00      
                                    have a look            01/01/70 00:00      
                                       Non-GPL            01/01/70 00:00      
                                       I'll have a look!            01/01/70 00:00      
                                    It's not quite as the page suggests            01/01/70 00:00      
                        Lint            01/01/70 00:00      

Back to Subject List