Doubts on type casting

General discussion on mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
robmar
Posts: 13
Joined: 15 Oct 2014 07:10

Doubts on type casting

#1 Post by robmar » 27 Oct 2021 22:32

Hello,
Due to my limited programming experience, the following questions are probably naive, so please forgive me...
Also they could be considered generic Basic language questions, but since I think they are possibly compiler dependent, I'm posting them in this section, hoping not to be OT :)

So, consider these two simple cases:
1)
dim x as byte
dim y as word
Assigning
x=y
as long as y value is certain to be < 255, is to be considered safe or else can lead to variables area corruption nonetheless?

2)
dim x as byte
dim y as byte
the statement
if (x*y > 1000) then ....
where evidently the result of the test is larger than a byte, is performed correctly or not? That is, the test is made automatically through a larger implicit register or else this is another recipe for register overflow?

Thanks in advance to those willing to help clarify my doubts!

Thomas.Pahl@t-online.de
Posts: 158
Joined: 24 May 2008 15:55
Location: Germany

Re: Doubts on type casting

#2 Post by Thomas.Pahl@t-online.de » 28 Oct 2021 12:31

I am using mikrobasic a long time and I can asure you, that it handles these cases very professional. :D
It automatically converts the types when necessary. But you can do it manually with the byte() and word() or float() or ... type qulifier.

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: Doubts on type casting

#3 Post by hexreader » 28 Oct 2021 12:54

I agree with Thomas.Pahl that mikroBasic will not cause any bad behaviour from register overflow in your examples.

I know very little about mikroBasic, but I have written a simple test program to find out actual behaviour. The if statement behaviour surprises me, but seems to behave as documented, rather than as I think it should work.

Here is my test code. Be sure to read comments for hints on use.

Code: Select all

' simple test program for forum - use software debugger to step through while watching varx, vary varz in watch window
' read "Conditional expressions" section of help function for explanation of byte multipplication in if statement
program Forum

' Declarations section
dim varw as byte
dim varx as byte
dim vary as word
dim varz as word                            ' make sure nothing overflows into this variable

main:
    varz = 0                                ' initialise
    
    vary = 100                              ' less than 256 - so no overflow
    varx = vary                             ' copy a word to a byte - expect 100

    vary = 1000                             ' more than 256 - so expect truncation to 8 bits
    varx = vary                             ' copy a word to a byte - should truncate to 232 - make sure no overflow to varz

    varw = 50
    varx = 100                              ' multiply byte by byte you would think would result in 5000, but seems to truncate to byte
    if (varw * varx) > 1000 then
        varz = 1234                         ' use debug single step to check that we get here - also check varz value
                                            ' my observation is that we do not get here - which surprises me
    end if

    while TRUE                              ' loop forever
    wend
end.
Start every day with a smile...... (get it over with) :)

Thomas.Pahl@t-online.de
Posts: 158
Joined: 24 May 2008 15:55
Location: Germany

Re: Doubts on type casting

#4 Post by Thomas.Pahl@t-online.de » 28 Oct 2021 15:33

The if clause will not become true. read the chapter about implicit conversion.

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: Doubts on type casting

#5 Post by hexreader » 28 Oct 2021 15:44

Thomas.Pahl@t-online.de wrote:
28 Oct 2021 15:33
The if clause will not become true. read the chapter about implicit conversion.
Nice reference :) Thanks....
The documentation does explain the behaviour.

Not easy for beginners like me, but at least the behaviour is documented.

Appreciate the clarification from an experienced user.
Start every day with a smile...... (get it over with) :)

Thomas.Pahl@t-online.de
Posts: 158
Joined: 24 May 2008 15:55
Location: Germany

Re: Doubts on type casting

#6 Post by Thomas.Pahl@t-online.de » 28 Oct 2021 16:28

I appreciate your educational explanations. :D Perhaps better then simple reading.

robmar
Posts: 13
Joined: 15 Oct 2014 07:10

Re: Doubts on type casting

#7 Post by robmar » 06 Nov 2021 08:55

Thanks so much for your time and the clear explanations! :D

Post Reply

Return to “mikroBasic PRO for dsPIC30/33 and PIC24 General”