Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Is there a way to know which type is a variable?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Wed Jan 27, 2021 8:42 am    Post subject: Is there a way to know which type is a variable? Reply with quote

Is there a way to know which type is a variable? or at least the number of bytes of a variable for instance a function that returns 4 for a Long, 2 for a Word, etc.

(BASCOM-AVR version : 2.0.8.3 )
Back to top
View user's profile
q065522

Bascom Member



Joined: 17 Dec 2009
Posts: 5

germany.gif
PostPosted: Wed Jan 27, 2021 10:15 am    Post subject: Reply with quote

Look at "Bascom fundamentals" in the Bascom help file
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5917
Location: Holland

blank.gif
PostPosted: Wed Jan 27, 2021 10:22 am    Post subject: Reply with quote

maybe you can use the sizeof() function:

Code:
Dim W As Word , Ar(10) As Byte
Print Sizeof(w)
Print Sizeof(ar())

_________________
Mark
Back to top
View user's profile Visit poster's website
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Wed Jan 27, 2021 12:06 pm    Post subject: Reply with quote

Thanks Mark!
Sizeof is just what I was looking for. (It is not in BASCOM help)
Back to top
View user's profile
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Wed Jan 27, 2021 12:25 pm    Post subject: Is there a function to know the address where the SRAM varia Reply with quote

Is there a function to know the address where an SRAM variable is stored?

Knowing this address and the size of the variable it will be possible read the bytes that the variable is made of
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Wed Jan 27, 2021 1:01 pm    Post subject: Re: Is there a function to know the address where the SRAM v Reply with quote

hzz wrote:
Is there a function to know the address where an SRAM variable is stored?

I have shown this function and calculations therewith in your thread:
https://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=14742
Either you don't follow the threads initiated by you, or you are a slow learner and do not understand the replies.
Choice a or b?
Back to top
View user's profile
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Wed Jan 27, 2021 4:02 pm    Post subject: Reply with quote

I do read the threads and appreciate very much the time you take to answer.

I read your post and tested Varptr("var") but it didn't work so I thought it was just for EEPROM variables; maybe I read it too fast. I have just tested it without quotes and it works. Thanks!
Back to top
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2156

netherlands.gif
PostPosted: Wed Jan 27, 2021 5:18 pm    Post subject: Reply with quote

Code:
var = VARPTR( var2 )     'A variable to retrieve the address from
var = VARPTR( "var3" )   'A constant


Take a note of the quotes.

Don't know at what moment you want to know at what address the variable is saved in SRAM, but you can also have a look at the compiler report under the show result button.
Carefully, because adding, changing or moving variable can change the order in SRAM.

_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Wed Jan 27, 2021 6:41 pm    Post subject: Reply with quote

hzz wrote:
...maybe I read it too fast.

As Evert wrote, you've missed that I used it with constants.

Constants, if suitable, have the immense advantage to allow all sorts of calculations, and they are cheap in terms of memory space and cpu time, as everything is done by the compiler at compile-time.
Quote:
I have just tested it without quotes and it works. Thanks!

As Sizeof() is a pure compile-time function, and the addresses of all variables are also fixed at compile-time, it was fair to assume you'll better work with constants, and finally this was the reason I've pointed to my example with constants.
Showing your intentions would simplify the answer, btw.
Back to top
View user's profile
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Wed Jan 27, 2021 7:28 pm    Post subject: Reply with quote

Here is the working code of what I pretended to do
Thanks all!
Code:
'____________________________________________________________________________________
' Given any sequence of variables of type Byte, Word, Long and Array of Bytes this example shows how to obtain a string
' whose bytes are the bytes of the variables one after the other.

' It is useful to write binary data to an SD card or the send it via serial port using a binary protocol

'____________________________________________________________________________________
$regfile = "xm192a3def.dat"                                 ' ATxmega192A3
'____________________________________________________________________________________
$hwstack = 256
$swstack = 128
$framesize = 128
'____________________________________________________________________________________
'   For  16MHz crystal
Config Osc = Disabled , Extosc = Enabled , Range = 12mhz_16mhz , Startup = Xtal_1kclk , 32khzosc = Enabled
' Set PLL OSC conditions:
Osc_pllctrl = &B1100_0010                                   ' reference external oscillator, set the PLL' multiplication factor to 2 (bits 0 - 4)
Set Osc_ctrl.4                                              ' Enable PLL Oscillator
Bitwait Osc_status.4 , Set                                  ' wait until the pll clock reference source is stable
Clk_ctrl = &B0000_0100                                      ' switch system clock to pll
' Prescale
Config Sysclock = Pll , Prescalea = 1 , Prescalebc = 1_1
$crystal = 32000000
'____________________________________________________________________________________
Config Com1 = 9600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Open "COM1:" For Binary As #1
'____________________________________________________________________________________
'   VARIABLES

     Dim Long_variable As Long
     Dim Long0 As Byte At Long_variable Overlay
     Dim Long1 As Byte At Long_variable + 1 Overlay
     Dim Long2 As Byte At Long_variable + 2 Overlay
     Dim Long3 As Byte At Long_variable + 3 Overlay

     Dim Word_variable As Word
     Dim Word0 As Byte At Word_variable Overlay
     Dim Word1 As Byte At Word_variable + 1 Overlay

     Dim Byte_array_variable(5 )as Byte

     Dim Wtemp As Word
     Dim Btemp As Byte
     Dim N As Byte

     Dim Var_address As Word
     Dim Var_size As Byte

     Dim Reg_p_ar(401) As Byte
     Dim Reg_p_str As String * 400 At Reg_p_ar Overlay
     Dim K As Word
'____________________________________________________________________________________
'  INI VARIABLES

'   Initialize array Reg_p_ar()  index
     K = 0

'  Give a value to each byte of the variable "Btemp",  "Long_variable" and "Word_variable" for testing
     Btemp = 1
     Word0 = 11
     Word1 = 12
     Long0 = 101
     Long1 = 102
     Long2 = 103
     Long3 = 104
     Byte_array_variable(1) = 5
     Byte_array_variable(2) = 6
     Byte_array_variable(3) = 7

'____________________________________________________________________________________
'     LOAD VARIABLES INTO REG_P_AR()

'     Load BTEMP
      Var_size = Sizeof(btemp)
      Var_address = Varptr(btemp)                           ' Get the address where the variable is stored in SRAM
      Gosub Load_variable
'     Load WORD_VARIABLE
      Var_size = Sizeof(word_variable)
      Var_address = Varptr(word_variable)                   ' Get the address where the variable is stored in SRAM
      Gosub Load_variable
'     Load LONG_VARIABLE
      Var_size = Sizeof(long_variable)
      Var_address = Varptr(long_variable)                   ' Get the address where the variable is stored in SRAM
      Gosub Load_variable
'     Load BYTE_ARRAY_VARIABLE
      Var_size = Sizeof(byte_array_variable())
      Var_address = Varptr(byte_array_variable())           ' Get the address where the variable is stored in SRAM
      Gosub Load_variable
'____________________________________________________________________________________
      Reg_p_ar(k + 1 ) = 0                                  ' Put a string end at the end of data
'____________________________________________________________________________________

'      Print result  reg_p_ar()
       For N = 1 To K
          Print #1 , Reg_p_ar(n)
       Next N
' The result printed is:
'                          1
'                          11
'                          12
'                          101
'                          102
'                          103
'                          104
'                          5
'                          6
'                          7
'                          0
'                          0

           Print #1 , Reg_p_str                             ' The string does not contain printable characters so this doesn't print anything meaningful

'____________________________________________________________________________________
      Do
      Loop
'____________________________________________________________________________________

'__________________________________________________________________
'                              LOAD VARIABLE INTO   REG_P_AR()

'  Put the bytes of any given variable into the array Reg_p_ar() and keep track of the next
'   free position in the array, K
'     INPUT:
'              K= Position in Reg_p_ar() where the last yte was stored
'              Var_address = Address in SRAM of the Variable to load
'              Var_size = Number of byte of the variable 1, 2 or 4)
'     OUTPUT
'             Reg_p_ar()  updated
'             K  updated
'__________________________________________________________________
Load_variable:
   For N = 1 To Var_size
      Incr K
      Reg_p_ar(k ) = Inp(var_address)                       ' Get the Byte0 (LSB) of the variable
      Incr Var_address
   Next N
Return
'__________________________________________________________________

End
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Wed Jan 27, 2021 11:10 pm    Post subject: Reply with quote

hzz wrote:
Code:
' Given any sequence of variables of type Byte, Word, Long and Array of Bytes this example shows how to obtain a string

Would you explain the purpose of said string?
Quote:
Code:
' It is useful to write binary data to an SD card or the send it via serial port using a binary protocol

It looks like a maximum complicated version of what Ian or my person described in my latest link.
Back to top
View user's profile
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Wed Jan 27, 2021 11:52 pm    Post subject: Reply with quote

Hello MKS
First of all I truly appreciate you sharing your experiencie.
Second, and this is for anyone reading this thread, I'm writing the code almost real time, it sure is useful to get ideas, but is just tested with the shown examples and may have flows; for instance, I keep talking about a string, but it was made clear earlier that a string is not adecuate because in cannot contain zero values, excep for the termination.
Third, I think that I did not understood your solution. I understood that it requires that variables are declared one after the other so that their memory locations are consecutive. If this is the case, I have several problems:
- I have to create 9 registers, and they use the variables in different order
- I use many (more than 30) variables to create the registers which I store in a SD Card and they are in different files. I rather keep it this way to facilitate maintenance and reusability.
- I read from BASCOM Help, searching for DIM, the following:
Quote:
Variables are usually stored in the same memory order as they are dimensioned. But you should not depend on it. Some optimization techniques requite that some variables are stored in a certain order. Use VARPTR to get the address of a variable in memory.
So I thought it would be risky to relay on this.

But I will read your post more carefully
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Thu Jan 28, 2021 1:00 am    Post subject: Reply with quote

hzz wrote:
Hello MKS

Last time I checked my nick was MWS.
Quote:
Second, and this is for anyone reading this thread, I'm writing the code almost real time, it sure is useful to get ideas, but is just tested with the shown examples and may have flows; for instance, I keep talking about a string, but it was made clear earlier that a string is not adecuate because in cannot contain zero values, excep for the termination.

Right so, a string is unusable in this case, but nonetheless you still use it in your code and in your comments, code which you propose to be useful for a certain purpose.
Quote:
Third, I think that I did not understood your solution. I understood that it requires that variables are declared one after the other so that their memory locations are consecutive. If this is the case, I have several problems:
- I have to create 9 registers, and they use the variables in different order
- I use many (more than 30) variables to create the registers which I store in a SD Card and they are in different files. I rather keep it this way to facilitate maintenance and reusability.

Till now, and imho it will stay this way, variables are consecutively mapped into SRam exactly as they are Dim'd.
Based thereon you can build more than one block and overlay them with byte arrays.
Quote:
- I read from BASCOM Help, searching for DIM, the following:
Quote:
Variables are usually stored in the same memory order as they are dimensioned. But you should not depend on it. Some optimization techniques requite that some variables are stored in a certain order. Use VARPTR to get the address of a variable in memory.
So I thought it would be risky to relay on this.

Bascom itself uses variables in SRam, for example if you set up a buffered serial in, these are so-called internal variables, all possible to check within Bascom's report.
Till now and afaics also in the future there is no requirement to intermix internal variables with user dimensioned ones, so the statements about dim'd together applies.
The maker of Bascom however reserves the right to change this, and that's what the quote tells.

Will he?
Ask Mark yourself, but likely you get the same answer as in the quote.

Would he?
Imho not without urgent requirement, as the SRam's order was always put this way, therefore some user code depends on and any change will break existing code. In the moment and based on current controller architecture, I also see not any requiremt to change it.
Thus the answer to this question is: unlikely.

What you should do? Exactly as YOU feel is right and good for you.
If you are unsure whether future versions of Bascom will break with existing behavior, always have a backup of the Bascom version you compiled certain code with at hand.
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Thu Jan 28, 2021 11:13 am    Post subject: Reply with quote

Hzz
you ask for
Quote:
Is there a way to know which type is a variable? or at least the number of bytes of a variable for instance a function that returns 4 for a Long, 2 for a Word, etc.


I name all my variables with their type
I never use I or J but Jbyte and Ibyte or Iword
pressureB or voltW
I see another great advantage, when you search for a variable with the bascom search tool, it doesn't stop at every I, J K
JP Wink

ps

Mark do the same see Reserved words
Additional, there are also these reserved words :

LBYTE , HBYTE, TYPE

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5917
Location: Holland

blank.gif
PostPosted: Wed Feb 03, 2021 10:54 am    Post subject: Reply with quote

hzz wrote:
Thanks Mark!
Sizeof is just what I was looking for. (It is not in BASCOM help)


i see, i should not have mentioned this. it is not fully implemented. But in the mean time i finished it and added it to the help.
Notice that for strings it will return the actual size: dim s as string * 1 will use 2 bytes so sizeof(s) will return 2. Did not check if the current version does so.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum