I was trying to write a sub where you passed in a word sized variable and that was used as the limit for a loop inside the sub but ran into some strange differences with the condition evaluation for word variables. Here is a bit of test code with 3 similar subs that produce different assembly code.
'Conditions with word variables
'Chip settings
#chip 16F690, 8
#config OSC = INTOSCIO, PWRT = OFF, MCLRE = OFF, WDT = OFF
dim limit as word
dim loopCount as word
dim loopCount3 as word
wordtest1(1000)
wordtest2(1000)
wordtest3(1000)
do
loop
sub wordtest1(limit)
loopCount = 0
do
loopCount++
loop until loopCount = limit
end sub
sub wordtest2(limit2 as word)
dim loopCount2 as word
loopCount2 = 0
do
loopCount2++
loop until loopCount2 = limit2
'loop until loopCount2 = [word]limit2
'loop until [word]loopCount2 = limit2
'loop until [word]loopCount2 = [word]limit2
end sub
sub wordtest3(limit3 as word)
loopCount3 = 0
do
loopCount3++
loop until loopCount3 = limit3
end sub
wordtest1 produces correct looking assembly and passes limit and loopCount into the SysCompEqual16 sub completely.
wordtest2 just compares the low bytes of loopCount2 and limit2. Having the DIM for loopCount2 inside the sub seems to be the major problem there.
wordtest3 where the DIM for loopCount3 is at the top of the program is better than wordtest2 - the assembly passes loopCount3 into SysCompEqual16 but only passes the low byte of limit3.
So where and how you declare variables as words affects the assembly produced to evaluate a condition involving those word variables.
Frank
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
dim limit as word
dim loopCount as word
dim loopCount3 as word
wordtest1(1000)
wordtest2(1000)
wordtest3(1000)
wordtest4(1000)
wordtest5(1000)
wordtest6(1000)
do
loop
sub wordtest1(limit)
loopCount = 0
do
loopCount++
loop until loopCount = limit
end sub
sub wordtest2(limit2 as word)
dim loopCount2 as word
loopCount2 = 0
do
loopCount2++
loop until loopCount2 = limit2
'loop until loopCount2 = limit2
'loop until loopCount2 = limit2
'loop until loopCount2 = limit2
end sub
sub wordtest3(limit3 as word)
loopCount3 = 0
do
loopCount3++
loop until loopCount3 = limit3
end sub
sub wordtest4(limit)
loopCount = 0
do until loopCount = limit
loopCount++
loop
end sub
sub wordtest5(limit5 as word)
dim loopCount5 as word
loopCount5 = 0
do until loopCount5 = limit5
loopCount5++
loop
'loop until loopCount5 = limit5
'loop until loopCount5 = limit5
'loop until loopCount5 = limit5
end sub
sub wordtest6(limit6 as word)
loopCount3 = 0
do until loopCount3 = limit6
loopCount3++
loop
end sub
wordtest4, 5 and 6 treat the limit and loopCount as words and pass them whole into SysCompEqual16 so it looks like its just a problem when the condition is evaluated at the end of a loop. I've tried it with 'while' as well and the same applies there.
Hopefully that will narrow down the problem. I suppose it could also be an issue on for - next loops with word variables.
Frank
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I was trying to write a sub where you passed in a word sized variable and that was used as the limit for a loop inside the sub but ran into some strange differences with the condition evaluation for word variables. Here is a bit of test code with 3 similar subs that produce different assembly code.
wordtest1 produces correct looking assembly and passes limit and loopCount into the SysCompEqual16 sub completely.
wordtest2 just compares the low bytes of loopCount2 and limit2. Having the DIM for loopCount2 inside the sub seems to be the major problem there.
wordtest3 where the DIM for loopCount3 is at the top of the program is better than wordtest2 - the assembly passes loopCount3 into SysCompEqual16 but only passes the low byte of limit3.
So where and how you declare variables as words affects the assembly produced to evaluate a condition involving those word variables.
Frank
I've just tried moving the 'until' to the start of the do loop and that produces good code for all three subs -
'Conditions with word variables
'Chip settings
#chip 16F690, 8
#config OSC = INTOSCIO, PWRT = OFF, MCLRE = OFF, WDT = OFF
dim limit as word
dim loopCount as word
dim loopCount3 as word
wordtest1(1000)
wordtest2(1000)
wordtest3(1000)
wordtest4(1000)
wordtest5(1000)
wordtest6(1000)
do
loop
sub wordtest1(limit)
loopCount = 0
do
loopCount++
loop until loopCount = limit
end sub
sub wordtest2(limit2 as word)
dim loopCount2 as word
loopCount2 = 0
do
loopCount2++
loop until loopCount2 = limit2
'loop until loopCount2 = limit2
'loop until loopCount2 = limit2
'loop until loopCount2 = limit2
end sub
sub wordtest3(limit3 as word)
loopCount3 = 0
do
loopCount3++
loop until loopCount3 = limit3
end sub
sub wordtest4(limit)
loopCount = 0
do until loopCount = limit
loopCount++
loop
end sub
sub wordtest5(limit5 as word)
dim loopCount5 as word
loopCount5 = 0
do until loopCount5 = limit5
loopCount5++
loop
'loop until loopCount5 = limit5
'loop until loopCount5 = limit5
'loop until loopCount5 = limit5
end sub
sub wordtest6(limit6 as word)
loopCount3 = 0
do until loopCount3 = limit6
loopCount3++
loop
end sub
wordtest4, 5 and 6 treat the limit and loopCount as words and pass them whole into SysCompEqual16 so it looks like its just a problem when the condition is evaluated at the end of a loop. I've tried it with 'while' as well and the same applies there.
Hopefully that will narrow down the problem. I suppose it could also be an issue on for - next loops with word variables.
Frank
Thanks for the quick fix Hugh