BasicMicro - Forums

www.basicmicro.com
It is currently Mon May 21, 2012 12:06 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Comparing arrays??
PostPosted: Wed Oct 13, 2010 4:45 pm 
Offline
Citizen

Joined: Wed Mar 13, 2002 11:00 am
Posts: 32
Hi
Is it somhow possible to compare directly two arrays similar like this:

if arrayA(10) = arrayB(10) then OK

Heinz


Top
 Profile  
 
 Post subject: Re: Comparing arrays??
PostPosted: Wed Oct 13, 2010 5:18 pm 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 527
What your sample code is doing is comparing the 11th element of each of the arrays to see if they are the same which is valid. (0 biased index).

Now if you are asking if you can in one easy statement ask if all of the elements of the two arrays are the same, then the short answer is no. The compiler has very little knowledge of arrays, let alone bounds or the like.

Code:
Array1 var byte(10)
Array2 var byte(10)
xyz var word
int var byte

with the above defines you can ask for: Array1(10) and the compiler will give it to you. You can ask for Array1(20), and it will give you something. Likewise you can ask for xyz(10) and it will treat xyz as a word array and give you what ever is at the address of XYZ+20 (2 bytes per word).

But back to your original question you would need to do something like:
Code:
for i = 0 to 10
    if Array1(i) <> Array2(i) then  Bad
next
Ok:
...
Bad:

This could be built into some simple subroutines like memcmp(pointer1, pointer2, numberbytes), returnval
Easy to implement these in basic, would be great if BM did some of these as actual type command.

Kurt


Top
 Profile  
 
 Post subject: Re: Comparing arrays??
PostPosted: Thu Oct 14, 2010 2:34 am 
Offline
Citizen

Joined: Wed Mar 13, 2002 11:00 am
Posts: 32
“What your sample code is doing is comparing the 11th element of each of the arrays”
Oh yes I see, my wrong thinking.

thanks for quick replay

Kurt, is it aloud to jump out of an ”for ..to.. next” routine ?
..going to “bad”

Heinz


Top
 Profile  
 
 Post subject: Re: Comparing arrays??
PostPosted: Thu Oct 14, 2010 6:03 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 527
You are welcome and yes you can jump out of a for loop... Likewise you can do a return...

Kurt


Top
 Profile  
 
 Post subject: Re: Comparing arrays??
PostPosted: Thu Oct 14, 2010 8:10 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 903
Location: Temecula, CA
"Likewise you can do a return..."

assuming you are in a subroutine. :)

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Comparing arrays??
PostPosted: Thu Oct 14, 2010 11:17 am 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
And there is always the handy EXCEPTION command, which allows the user to break out of a subroutine without unbalancing the stack:
Quote:
EXCEPTION
This is for EXTREMELY advanced users.
EXCEPTION is used when it is necessary to break out of a subroutine for some reason before reaching the “return”(eg an exception). EXCEPTION differs from RETURN as follows:
RETURN
Retrieves the saved address from the stack, clears the address from the stack, and sets program execution to the line following the GOSUB command.
EXCEPTION
Clears the return address from the stack, and resumes program execution at the label given. The label it jumps to must be at the same gosub level as the calling function was at.

The compiler has no way of knowing at compile time what level any gosub is at. Only during runtime. The programmer/user has to determine if they are “exception”ing back to an appropriate level.

Syntax
exception label
label is the label at which program execution should continue

Example 1
looper VAR byte

SEROUT S_OUT, i9600, ["You should only see this once!", 13,13]
PAUSE 1000

MAIN
FOR looper = 1 to 150
GOSUB _GoThere

_ComeBack
NEXT ; looper
PAUSE 500 ; so you can catch the upper count
GOTO MAIN

_GoThere
IF looper > 145 THEN
SEROUT S_OUT, i9600, [DEC looper, 13]
ENDIF

IF looper < 51 THEN _skiphere
EXCEPTION _ComeBack; Return from Gosub without overflowing stack
_skiphere ; comes here if looper less than 51
SEROUT S_OUT, i9600, [DEC looper, 13]
RETURN

This example will print “You should only see this once!”, followed by 1 through 50, then 146 through 150. The “…only once!” message will be seen once, the numbers will continue to appear over and over, until boredom sets in.
Now replace the EXCEPTION line with “GOTO _ComeBack”. We now drop out of the _GoThere subroutine prematurely, without clearing the stack. Eventually there is stack overflow and the PIC goes into a death spiral which leads to a reset. And how do you know this? Because “You should only see this once!” appears every fourth or fifth loop, where it only showed once at the beginning in the example with EXCEPTION.

Example2
This program is similar to the one under GOSUB but provides an "escape" from the subroutine if the minimum weight is too low.

val var word
weightmin var word
weightmax var word

start
code to calculate minimum weight
hserout ["Minimum "]
val = weightmin
gosub outvaldec
code to calculate maximum weight
hserout ["Maximum "]
val = weightmax
gosub outvaldec
goto start

outvaldec ; start of subroutine
if weightmin > 5 then continue
exception start ; value is too low – do again
continue
hserout ["weight is ",dec val," mg",13]
return

The above program is similar to the one under GOSUB but provides an "escape" from the subroutine if the minimum weight is too low.

In the below code, subroutine "test" never RETURNs. Instead the code jumps back to main without ever reaching the RETURN or the PAUSE.

main
gosub test
pause 1000
goto main
test
exception main
return

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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 post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO