Menu

Strange interaction between long variable and HSerPrint of concatenated value

2019-11-04
2019-11-04
  • Jim giordano

    Jim giordano - 2019-11-04

    When I do an HSerPrint of a concatated string, and also do an operation with a variable declared as Long, there is some sort of interaction that trashes the values in one of my other arrays.

    example:

    #option Explicit
    #chip 16LF1938, 32
    
    #define USART_BAUD_RATE 115200 '460800 '460800 '921600 '230400 '115200
    #define USART_TX_BLOCKING
    
    #define prt HSerPrint
    
    dim seg(9)
    dim ii
    dim timel,timeh
    dim bigtime as long
    
    prt "test"
    
    for ii=1 to 8:seg(ii)=63:next ii  'preset all values to 63
    
    prt chr(27):prt "[5;1H"
    for ii=1 to 8
      prt seg(ii):prt " "  ' print out values before the concatenation
    next i
    
    prt chr(27):prt "[3;1H"
    prt "abcde"+"fghij"  ' this caused the problem
    
    prt chr(27):prt "[6;1H"
    for ii=1 to 8
      prt seg(ii):prt " "   ' print out values after the concatenation
    next i
    do
    loop
    
    ' the rest is the original program stripped down to the
    '   minimum that still affects the problem
    timel=TMR1L
    timeh=TMR1H-0x80 ' subtract out starting amount
    bigtime=timeh*256+timel   ' ** this instruction causes the problem
    

    Note that if I comment out the last line, the problem doesn't occur, even though it is never executed.

    The result look like:

    °°est

    abcdefghij

    63 63 63 63 63 63 63 63
    103 104 105 106 63 63 63 63

    so you can see that the first four values of the seg array were changed to "ghij"

     
  • Anobium

    Anobium - 2019-11-04

    I have seen this before. Try.

    hsersend 27: wait 100 us:prt "[5;1H"
    

    I think it is the terminal processes the <esc>. </esc>

    My test on a 16f1938

    test
    
    abcdefghij
    
    63 63 63 63 63 63 63 63
    103 104 105 106 63 63 63 63
    
     
  • Anobium

    Anobium - 2019-11-04

    and, you can use... Timer1 and cut that code.

    bigtime=Timer1

     
  • Jim giordano

    Jim giordano - 2019-11-04

    that's two interesting things, but it has no effect of what is trashing the seg array.

    What interaction would change the first four values in and unrelated array.

    It seems the compiler is overlapping a declared array with some of it's intermediate values, not a good thing.

     
  • Anobium

    Anobium - 2019-11-04

    Is it? I think not.

    Where is it in my response? I dont understand the issue - me thinks.

    What version of compiler etc. Look at the first line of the ASM

     
  • Jim giordano

    Jim giordano - 2019-11-04

    ;Program compiled by Great Cow BASIC (0.98.06 2019-06-12 (Windows 32 bit))

    I guess I'm missing something here.

    This is the seg array before the print -
    63 63 63 63 63 63 63 63

    and after doing - HSerPrint "abcde"+"fghij"

    here is the seg array after that print-
    103 104 105 106 63 63 63 63

    Why would an HSerPrint change the values in an unrelated array?

    Notice that the values changed were changed to 103,104,105,106 which is ascii for "ghij"

    So the compiler put together code that used the first part of my array for scratch.

    From the asm file-

    ;prt "abcde"+"fghij"  ' this caused the problem
      movlw low SYSSTRINGPARAM1
      movwf FSR1L
      movlw high SYSSTRINGPARAM1
      movwf FSR1H
      clrf  SysStringLength
      movlw low StringTable5
      movwf SysStringA
      movlw (high StringTable5) & 127
      movwf SysStringA_H
      call  SysReadStringPart
      movlw low StringTable6
      movwf SysStringA
      movlw (high StringTable6) & 127
      movwf SysStringA_H
      call  SysReadStringPart
      movlw low SYSSTRINGPARAM1
      movwf FSR0L
      movlw high SYSSTRINGPARAM1
      movwf FSR0H
      movf  SysStringLength,W
      movwf INDF0
      movlw low SYSSTRINGPARAM1
      movwf SysPRINTDATAHandler
      movlw high SYSSTRINGPARAM1
      movwf SysPRINTDATAHandler_H
      movlw 1
      movwf COMPORT
      call  HSERPRINT256
    

    But it doesn't give me a clue where the compiler overlapped the seg array.

    To be clear, the problem does not occur if two separate HSerPrints are done, one for each string, it's only the concatination of the two strings within one print that causes the problem.

     
  • Anobium

    Anobium - 2019-11-04

    What version of compiler etc. Look at the first line of the ASM

     
  • Jim giordano

    Jim giordano - 2019-11-04

    ;Program compiled by Great Cow BASIC (0.98.06 2019-06-12 (Windows 32 bit))

     
  • Anobium

    Anobium - 2019-11-04

    Leave it with me. I need to examine in a debugger.

    Thanks for version info.

     
  • Anobium

    Anobium - 2019-11-04

    Look like a bug.

    Check the first code example. This is the easier way to reproduce,

    Use the workaround. Shown in second example.

    #chip 16f877a
    
    #option explicit
    
    dim seg(9)
    dim ii
    
    
    for ii=1 to 8:seg(ii)=63:next ii  'preset all values to 63
    
    for ii=1 to 8
      epwrite ii,seg(ii)
    next ii
    
    
    passedsub ("abcde"+"fghij")  ' this caused the problem
    
    for ii=1 to 8
      epwrite ii+16,seg(ii)
    next ii
    
    
    sub passedsub( instring as string )
    
    end sub
    

    To work around, concat first then pass as a parameter.

    My guess, you know this already.

    #chip 16f877a
    
    #option explicit
    
    dim seg(9)
    dim ii
    
    
    for ii=1 to 8:seg(ii)=63:next ii  'preset all values to 63
    
    for ii=1 to 8
      epwrite ii,seg(ii)
    next ii
    
    dim mystring as string
    mystring = "abcde"+"fghij"
    passedsub ( mystring )  ' this will not cause a problem
    
    for ii=1 to 8
      epwrite ii+16,seg(ii)
    next ii
    
    
    sub passedsub( instring as string )
    
    end sub
    
     
  • Anobium

    Anobium - 2019-11-04

    Another way to resolve.... add a CONCAT function.

    But, we need to resolve to avoid this issue in the future.

    #chip 16f877a
    
    #option explicit
    
    dim seg(9)
    dim ii
    
    
    for ii=1 to 8:seg(ii)=63:next ii  'preset all values to 63
    
    for ii=1 to 8
      epwrite ii,seg(ii)
    next ii
    
    
    passedsub ( concat("abcde","fghij") )  ' this resovles the problem
    
    for ii=1 to 8
      epwrite ii+16,seg(ii)
    next ii
    
    
    sub passedsub( instring as string )
    
    end sub
    
    function concat ( instring1 as string, instring2 as string ) as string * 40
    
      concat = instring1 + instring2
    
    end Function
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.