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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Jan Waclawek
08/05/05 08:47
  08/05/05 10:31

Read: 1125 times

#98870 - Tester/Counter
Responding to: Steve M. Taylor's previous message
I have prepared a "tester", which compares a result from shifting an arbitrary input value with a reference routine, and, more importantly, counts the instruction cycles of the "tested" shift routine, finds their minimum and maximum and adds them up to a sum (which then divided by 31 gives the average duration).

;----------- testing program for the shift code competition ----------------
;------------------------------- by wek ------------------------------------

   DSEG  AT  30h
MaxTime: ds  2     ;worst case duration
MaxAcc:  ds  1     ;value of acc (=shift count) for worst case
MinTime: ds  2     ;best case duration
MinAcc:  ds  1     ;value of acc (=shift count) for best case
TotTime: ds  2     ;total duration (TotTime/31=average duration)
Result:  ds  4     ;temporary store of reference result

                   ;initialize the time measurement variables
   mov   MaxTime+0,#0
   mov   MaxTime+1,#0
   mov   MinTime+0,#0FFh
   mov   MinTime+1,#0FFh
   mov   TotTime+0,#0
   mov   TotTime+1,#0
   mov   tmod,#1   ;set timer0 to 16 bit counter

   mov   a,#1Fh

TesterLoop:        ;for acc:=1f downto 1 do this loop
   push  acc
   mov   r4,#0ABh     ;this is the test vector
   mov   r5,#0CDh
   mov   r6,#056h
   mov   r7,#078h
                      ;first, do the reference calculation
   lcall ShiftTrivial
   mov   Result+0,r4  ;...and store the result for future comparison
   mov   Result+1,r5
   mov   Result+2,r6
   mov   Result+3,r7

   pop   acc
   push  acc          ;restore accumulator (=shift count)
   mov   r4,#0ABh
   mov   r5,#0CDh
   mov   r6,#056h
   mov   r7,#078h     ;and use the same test vector
   mov   th0,#0       ;reset timer0 - we are going to measure the 
   mov   tl0,#0       ;               duration of the shift routine
   setb  tr0          ;start timer0
   lcall Shift        ;and call the tested routine
   clr   tr0          ;stop timer0

   mov   a,r4         ;now test the result against the reference
   cjne  a,Result+0,Error
   mov   a,r5
   cjne  a,Result+1,Error
   mov   a,r6
   cjne  a,Result+2,Error
   mov   a,r7
   cjne  a,Result+3,Error

   clr   c            ;get measured time into r2:r3
   mov   a,tl0
   subb  a,#3           ;correction for setb tr0 and call
   mov   r3,a
   mov   a,th0
   subb  a,#0
   mov   r2,a

   mov   a,TotTime+1  ;add the measured time to total time
   add   a,r3
   mov   TotTime+1,a
   mov   a,TotTime+0
   addc  a,r2
   mov   TotTime+0,a

   mov   a,r2         ;if measured time is higher than MaxTime then update MaxTime
   cjne  a,MaxTime+0,TesterX1
   mov   a,r3
   cjne  a,MaxTime+1,TesterX1
   jc    TesterX2
   mov   MaxTime+0,r2
   mov   MaxTime+1,r3
   pop   MaxAcc
   push  MaxAcc

   mov   a,r2         ;if measured time is lower than MinTime then update MinTime
   cjne  a,MinTime+0,TesterX11
   mov   a,r3
   cjne  a,MinTime+1,TesterX11
   jnc   TesterX12
   mov   MinTime+0,r2
   mov   MinTime+1,r3
   pop   MinAcc
   push  MinAcc

   pop   acc          ;restore acc - end of loop
   djnz  acc,TesterLoop

                   ;end of test, stay looping forever
                   ;in a non-simulated environment, the timing results could
                   ;  been sent out e.g. via UART for inspection
   sjmp  Stop

                   ;in case of failed test, loops here forever
                   ;again in a non-simulated environment, something could be
                   ;   sent out (e.g. "ERROR" :-)
   sjmp  Error

;--------------------- the reference shift routine -----------------------
;--- input:    r4:r5:r6:r7 - the operand
;              acc         - number of shifts (any number)
;--- output:   r4:r5:r6:r7 - the result
;--- destroys: b
;Performance (cycles including ret):
; WorstCase - 470
; BestCase  - 20
; Average   - 245
; Code Length - 21 bytes

   jz    ShiftTRet
   mov   b,a
   clr   c
   mov   a,r4
   rrc   a
   mov   r4,a
   mov   a,r5
   rrc   a
   mov   r5,a
   mov   a,r6
   rrc   a
   mov   r6,a
   mov   a,r7
   rrc   a
   mov   r7,a
   djnz  b,ShiftTLoop
   ret                  ;a*15+5 cycles (incl. ret)

;-------- insert your shift routine here ---------


Jan Waclawek

List of 17 messages in thread
Code Challenge operational      Craig Steiner      08/03/05 18:09      
   Questions for current challenge ?      Steve M. Taylor      08/04/05 07:20      
      Instruction Cycles?      Andy Neil      08/05/05 08:06      
         best case/worst case      Jan Waclawek      08/05/05 08:30      
            how about 30 instructions      Erik Malund      08/05/05 08:51      
               30 instructions?      Jan Waclawek      08/05/05 09:22      
                  i did count inside loops      Erik Malund      08/08/05 08:07      
            Changed last night      Craig Steiner      08/05/05 10:10      
      Tester/Counter      Jan Waclawek      08/05/05 08:47      
         Wow!      Craig Steiner      08/05/05 10:30      
   The 3 better solutions      Robert Perez      08/04/05 16:02      
   Definition of BEST      Graham Cole      08/05/05 10:24      
      Definition of best      Craig Steiner      08/05/05 10:56      
         Good idea      Graham Cole      08/10/05 06:06      
   pre tags?      Dan Henry      08/06/05 11:15      
      Yes      Craig Steiner      08/06/05 13:56      
         A highlight please      Jerson Fernandes      08/06/05 23:17      

Back to Subject List