Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

IN NEED OF HELP

Status
Not open for further replies.

athlon11

New Member
Am having a problem in writing a program to control an 8051 MC based device, I wrote the following small code to test if its working :

10 INPUT "YOUR AGE (A<>0)=",A
20 IF A<>0 THEN GOTO 30
30 PRINT "WOW", CR

the result are in case 0:

YOUR AGE (A<>0)=0
WOW

the result are in case 1:

YOUR AGE (A<>0)=1
WOW


the relational expression (<>) is not recognized by the compiler, as if line 20 do not exist, may be am not noticing the error, although its a small code.

help me people.
 
I haven't used basic in a long time but I think your problem is how you have you if satement set up.

The way I see it the PRINT statement always gets executed: If A = 0 then GOTO 30 and execute the PRINT. If A <> 0 don't do any goto, then run the next satement which is the PRINT. You need to change it so it jumps over the PRINT line when you don't want it to print.

It's also a good idea to put an infinite loop at the end of your code. This keeps the processor from executing unknown stuff that may be stored at some other point in the code storage.
 
I worte the follwing:

10 INPUT "YOUR AGE (A<>0)=",A
20 IF A<>0 THEN GOSUB 30 ELSE GOTO 50
25 PRINT CHR(13)
30 PRINT "WOW", CR
35 GOTO 10
40 PRINT CHR(13)
50 PRINT "0 CANT BE YOUR AGE", CR
100 GOTO 10


the results are:

YOUR AGE (A<>0)=0
0 CANT BE YOUR AGE
YOUR AGE (A<>0)=0
0 CANT BE YOUR AGE
YOUR AGE (A<>0)=1
WOW
YOUR AGE (A<>0)=2
WOW
YOUR AGE (A<>0)=3
WOW
YOUR AGE (A<>0)=4
WOW
YOUR AGE (A<>0)=5
WOW


thank u bmcculla.

but when I write:

100 RETURN

THE RESULTS ARE:

YOUR AGE (A<>0)=0
0 CANT BE YOUR AGE


ERROR: C-STACK - IN LINE 100

100 RETURN
---------------X

Why I have this error can u explain to me, in the documentation writtin C-STACK errors will occure if a return in exe befor a GOSUB, not clear to me, I know that behaviour of statement Return is to go back to the first statement after GOSUB.

BUT IN CASE A NOT 0:

YOUR AGE (A<>0)=1
WOW
YOUR AGE (A<>0)=1
WOW
YOUR AGE (A<>0)=1
WOW
YOUR AGE (A<>0)=2
WOW
YOUR AGE (A<>0)=3
WOW
YOUR AGE (A<>0)=7878
WOW


thank u in adv.
 
BASIC GOTO versus GOSUB

athlon11 said:
20 IF A<>0 THEN GOSUB 30 ELSE GOTO 50
100 GOTO 10
You have a conditional GOSUB and no RETURN - this will fill the GOSUB stack until it overflows - this is wrong.

20 IF A<>0 THEN GOSUB 30 ELSE GOTO 50
100 RETURN

This program has the potential of finding the RETURN without using a GOTO to get there - hence the 'RETURN without GOSUB error; a GOSUB stack "underflow" - trying to take something that isn't there.

The simple answer is to change line 20 to use GOTO in both cases
20 IF A<>0 THEN GOTO 30 ELSE GOTO 50
100 GOTO 10

Then you avoid both problems.

It is quite possible to use GOSUB in both cases (with a RETURN at line 100) but you will have to sort out the program layout a bit to ensure that either one path or the other is followed and the GOSUB stack won't overflow or (your second example) underflow.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top