Shop OBEX P1 Docs P2 Docs Learn Events
Comparing Bytes Anywhere in RAM — Parallax Forums

Comparing Bytes Anywhere in RAM

JDOhioJDOhio Posts: 72
edited 2006-08-13 23:38 in General Discussion
I wrote a subroutine to compare bytes in two arrays. The intent of the comparison is to compare the bytes at each location until there is no match. If Idx reaches zero, then all of the bytes matched.

Idx=7
DO UNTIL TimeDate(Idx)<>MyDate(Idx)
    DEC Idx
    IF Idx=0 THEN EXIT 
LOOP




In stepping through this DO loop, I found that the comparison for the first byte always fails even when the data at the two locations described by the SX/B are the same (as verified by reviewing the locations in RAM on the debugger screen).

When I reviewed the assembly, it appears that it retrieves the value for the specified byte in TimeDate correctly. Before retrieving the value of the specified byte in MyDate, it assigns FSR to $10. In my program, the two arrays are not located in the $10 memory bank. When the code after the FSR assignment executes, it looks like it retrieves values from the $10 bank and the comparison fails.

__DO_4:                          ;  DO UNTIL TimeDate(Idx)<>MyDate(Idx)
MOV W,#TimeDate               
ADD W,Idx                     
MOV FSR,W                     
MOV __PARAM1,IND  
            
MOV FSR,#$10     
             
MOV W,#MyDate          
ADD W,Idx                     
MOV FSR,W                     
 MOV __PARAM2,IND              
 MOV FSR,#$10                  
CJNE __PARAM1,__PARAM2,@__LOOP_4




I have also tried using

MyDateIndex=MyDate
DO UNTIL TimeDate(Idx)<>__RAM(MyDateIndex)




with the same results. Any ideas?

Joe

Comments

  • BeanBean Posts: 8,129
    edited 2006-08-13 18:23
    Please post the whole program.
    Are your arrays defined as Byte (8) ?

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "You're braver than you believe, stronger than you seem, and smarter than you think" Christopher Robin to Pooh
    ·
  • JDOhioJDOhio Posts: 72
    edited 2006-08-13 22:01
    Thanks for the reply and sorry for the delay. I was inspired by your question to post the complete code (and a certain set of seven year olds wanted to play some baseball on this beautiful day).

    Since my program is made up of 14 SXB files, I'd rather attempt to faithfully reproduce the code in a single file showing the error rather than have someone trace the 14 files (this is why you see the large array and the declaration of many byte variables in the SX/B code below). In so doing, I think I resolved the error which raised more of a guidelines question for me.

    The assembly code below demonstrates the error. In the SX/B, I populate some memory and then copy that memory using MoveBytes (you may recognize this from a previous post). Then, I call CompareBytes.

    When the processor enters into the CompareBytes subroutine, FSR is set for the memory location of TimeDate and MyDate. This causes the processor to assign the wrong place in memory the value of Idx (MOV Idx,#6).
    In setting up for the initial compare (TimeDate(Idx)<>MyDate(Idx)), the code assigns __PARAM1 the correct value from TimeDate (__PARAM1 has the correct value because FSR did not change during the retrieval of the TimeDate value) and then assigns FSR the value of $10.
    When the code attempts to assign __PARAM2 from MyDate, the value is incorrect because the location of Idx is not the same as when it entered the subroutine (i.e., the value of FSR has changed).

    ;inside CompareBytes
    MOV Idx,#6
    __DO_1:                          ;  DO UNTIL TimeDate(Idx)<>MyDate(Idx)
    MOV W,#TimeDate               
    ADD W,Idx                     
    MOV FSR,W        
    MOV __PARAM1,IND  
                
    MOV FSR,#$10     
                 
    MOV W,#MyDate                 
    ADD W,Idx                     
    MOV FSR,W                     
    MOV __PARAM2,IND 
    
    



    To correct this, I added

    MOV FSR,#$10
    
    



    to the end of MoveBytes (see below). The DO loop in CompareBytes executes correctly after the change to MoveBytes.

    I reviewed some other assembly code around my program, and there is a pattern indicating that after every use of FSR for indirection, it is assigned $10.

    Would this be a general guideline in working with SX/B?


    This reproduces the error.
    DEVICE    SX48,OSCHS1
    FREQ    50000000
    
    CompareBytes    SUB 1
    MoveBytes    SUB 3
    
    PROGRAM Start
    
    BigArray        VAR BYTE(48)
    TimeDate        VAR BYTE(8)
    MyDate        VAR BYTE(8)
    AByte1        VAR BYTE
    AByte2        VAR BYTE
    AByte3        VAR BYTE
    AByte4        VAR BYTE
    AByte5        VAR BYTE
    AByte6        VAR BYTE
    AByte7        VAR BYTE
    AByte8        VAR BYTE
    AByte9        VAR BYTE
    ABytea        VAR BYTE
    AByteb        VAR BYTE
    ABytec        VAR BYTE
    Idx        VAR BYTE
    
    Start:
        PUT TimeDate, $00, $11, $15, $13, $08, $07, $06, 0    
        MoveBytes TimeDate,MyDate,7
        CompareBytes MyDate
        END
    
    CompareBytes:
        Idx=6
        DO UNTIL TimeDate(Idx)<>MyDate(Idx)
            DEC Idx
            IF Idx=5 THEN
                DEC Idx
            ENDIF
            IF Idx=0 THEN EXIT 
        LOOP
    RETURN
    
    MoveBytes:
    ASM
    :Again:
        MOV W,__PARAM1
        MOV FSR,W
        MOV W,IND
        MOV __PARAM4,W
        MOV W,__PARAM2
        MOV FSR,W
        MOV W,__PARAM4
        MOV IND,W
        INC __PARAM1
        INC __PARAM2
        DJNZ __PARAM3, :Again
    ;MOV FSR,#$10        ;remove the semi-colon to correct the error
      ENDASM
    RETURN
    
    



    Joe
  • BeanBean Posts: 8,129
    edited 2006-08-13 23:38
    Joe,
    Yes but only for the SX48.
    It's better to use the SX/B command "BANK" with no parameters. This will set FSR correctly for the SX28 or SX48.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "You're braver than you believe, stronger than you seem, and smarter than you think" Christopher Robin to Pooh
    ·
Sign In or Register to comment.