Menu

(no subject)

Santiago
2015-10-24
2015-10-25
  • Santiago

    Santiago - 2015-10-24

    I am playing with macros for getting some 2D arrays working, and i got something wrong when usin element 0 of array.
    the code is this:
    Great Cow BASIC (0.94 2015-08-05)

    #chip mega328p, 16
    
    create2Darray( my2Da, 8, 4 )     ' Create 2x4 Array called my2Da
    
    set2Ddata( my2Da, 2, 4, 24 )     ' Set element 2,4 in array to 24
    
    myvar = 0
    get2Ddata( my2Da, 2, 4, myVar )  ' Get element 2,4 and put in myVar
    
    do
    loop
    
    Macro create2Darray( name, size, dimY )
        Dim name( size )
        name(0) = dimY
    End Macro
    
    Macro set2Ddata( name, ordx, ordy, value )
        name( (ordx-1)*name(0)+ordy ) = value
    End Macro
    
    Macro get2Ddata( name, ordx, ordy, var )
        var = name( (ordx-1)*name(0)+ordy )
    End Macro
    

    I se that gcbasic creates an alias for element 0 and sets its value properly:
    ;Alias variables

    ;Alias variables
    #define SYSMY2DA_0  256
    ...
    ...
        ldi SysValueCopy,4
        sts SYSMY2DA_0,SysValueCopy
    

    And uses it in the macro set2Ddata, for example the line in file.gcb:

    set2Ddata( my2Da, 2, 4, 24 )
    

    generates this code in file.asm:

        ldi SysValueCopy,4
        sts SYSMY2DA_0,SysValueCopy
        ldi SysValueCopy,24
        sts SysArrayTemp1,SysValueCopy
        lds SysTemp2,SYSMY2DA_0    ;<<<<<<<<<<
        ...
        ...
    

    In the macro get2Ddata i use exactly the same calculations.
    but the line in file.gcb:

    get2Ddata( my2Da, 2, 4, myVar )
    

    gerenates this asm:

        lds SysTemp2,MY2DA0     ;<<<<<<
        ldi SysTemp3,low(MY2DA)
        add SysTemp2,SysTemp3
        ...
        ...
    

    Then looking in the memory location of variables i see:

    ;Set aside memory locations for variables
    .EQU    MY2DA=256
    .EQU    MY2DA0=265  ;<<<<<<<<<<<<
    .EQU    MYVAR=266
    .EQU    SysArrayTemp1=267
    .EQU    SysPointerX=268
    

    MY2DA0 is not in my code and was generated by gcbasic and used in place of SYSMY2DA_0 only in the get macro.
    Finally i got my code working properly this way:

    Macro set2Ddata( name, ordx, ordy, value )
        index = (ordx-1)*name(0)+ordy
        name( index ) = value
    End Macro
    
    Macro get2Ddata( name, ordx, ordy, var )
        index = (ordx-1)*name(0)+ordy
        var = name( index )
    End Macro
    

    This way the variable MY2DA0 is not created and the alias SYSMY2DA_0 is properly used in both macros.
    I know my code is a bit tricky but may be there is something in the compiler.

     
    • Chris Roper

      Chris Roper - 2015-10-25

      In GCBasic arrays are indexed from 1, there is no element zero.
      The First element, zero, of the array stores the length of the array.

      Cheers
      Chris

       

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.