simple code cause NXT brick frozen

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
felix2ch
Posts: 33
Joined: 11 Jun 2013, 16:46

simple code cause NXT brick frozen

Post by felix2ch »

Code: Select all

float x = 0;
float y;
task main()
{
     do
     {
           y = x;
     }
     until(1);
}
Bricxcc 3.3.8.11 nbc 1.2.1.5 (test_release20130813)
FirmWare 1.31

It's weird...
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: simple code cause NXT brick frozen

Post by h-g-t »

Looks like it's in an infinite loop, not frozen.

http://bricxcc.sourceforge.net/nbc/nxcd ... pi/do.html

The do statement

A variant of the while loop is the do-while loop.

The syntax for this control structure is shown below.
do body while (condition)

The difference between a while loop and a do-while loop is that the do-while loop always executes the body at least once, whereas the while loop may not execute it at all.
do
{
x = x+1;
y = y*2;
} while(x < 10);

If you don't put the values on the screen you have no way of knowing what is happening.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
felix2ch
Posts: 33
Joined: 11 Jun 2013, 16:46

Re: simple code cause NXT brick frozen

Post by felix2ch »

But it works fine at
bricxCC Version3.3 build3.3.8.9
Next Byte Codes Compiler version 1.2 (1.2.1.r4,


Below is the F12 code of 3.3.8.11

Code: Select all

dseg	segment
;------- definitions -------
;------- declarations -------
__D0main	sdword	
__DF0main	float	
____initialize_global_data_return	byte	
__constVal1	sbyte	1
__PFTmp	byte	
dseg	ends
;------- code -------
thread main
	subcall __initialize_global_data, ____initialize_global_data_return
__ASM_Label_558:
	mov __DF0main, __constVal1
	not __DF0main, __DF0main
	tst 5, __D0main, __DF0main
	brtst 4, __ASM_Label_559, __D0main
	jmp __ASM_Label_558
__ASM_Label_559:
	exit -1, -1
endt
;------------------------
subroutine __initialize_global_data
	subret ____initialize_global_data_return
ends
;------------------------

The F12 code of 3.3.8.9

Code: Select all

dseg	segment
;------- definitions -------
;------- declarations -------
__DF0main	float	
____initialize_global_data_return	byte	
__zfmain	byte	
__constVal0	sbyte	
__PFTmp	byte	
dseg	ends
;------- code -------
thread main
	subcall __initialize_global_data, ____initialize_global_data_return
__NXC_Label_489:
	mov __DF0main, __constVal0
	tst 5, __zfmain, __DF0main
	brtst 4, __NXC_Label_490, __zfmain
	jmp __NXC_Label_489
__NXC_Label_490:
	exit -1, -1
endt
;------------------------
subroutine __initialize_global_data
	subret ____initialize_global_data_return
ends
;------------------------
felix2ch
Posts: 33
Joined: 11 Jun 2013, 16:46

Re: simple code cause NXT brick frozen

Post by felix2ch »

h-g-t wrote:Looks like it's in an infinite loop, not frozen.
Thanks for your reply.
The "frozen" mean the brick can't response for any operation, even power off.
The only thing i can do is take off battery.
mrblp
Posts: 82
Joined: 02 Oct 2010, 14:33

Re: simple code cause NXT brick frozen

Post by mrblp »

Hello,
felix2ch wrote:

Code: Select all

float x = 0;
float y;
task main()
{
     do
     {
           y = x;
     }
     until(1);
}
I think this is the result of macro expansion. "until(x)" is defined as "while(!(x))". So at the end the statement is "while (!(1))". A real c compiler will make a "false" out of this but may be "nxc" is confused by that construction.

Give "while(0)" a try and tell us the result.
Bye Marvin

- "I think you ought to know I'm feeling very depressed." - (Android Marvin in "The Hitchhiker's Guide to the Galaxy" by Douglas Adams, 1978)
mrblp
Posts: 82
Joined: 02 Oct 2010, 14:33

Re: simple code cause NXT brick frozen

Post by mrblp »

Hello,
felix2ch wrote:Bricxcc 3.3.8.11 nbc 1.2.1.5 (test_release20130813)
FirmWare 1.31
Which firmware do you use? The stock LEGO one or the extended firmware? If you are using the stock one did you setup the nxc compiler like that? If you are using the extended firmware you should upgrade to the one packaged in the bricxcc package. I know that John optimized and corrected floating point things. They may not work always correctly in older firmware versions.
Bye Marvin

- "I think you ought to know I'm feeling very depressed." - (Android Marvin in "The Hitchhiker's Guide to the Galaxy" by Douglas Adams, 1978)
felix2ch
Posts: 33
Joined: 11 Jun 2013, 16:46

Re: simple code cause NXT brick frozen

Post by felix2ch »

mrblp wrote: I think this is the result of macro expansion. "until(x)" is defined as "while(!(x))". So at the end the statement is "while (!(1))". A real c compiler will make a "false" out of this but may be "nxc" is confused by that construction.

Give "while(0)" a try and tell us the result.
I'm not sure.
It's looks fine if replace float variable with int, still using "until".
felix2ch
Posts: 33
Joined: 11 Jun 2013, 16:46

Re: simple code cause NXT brick frozen

Post by felix2ch »

Firmware is Lego official 1.31.
In the NXC config section, NXT2.0 compatible is checked, enhanced firmware is not.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: simple code cause NXT brick frozen

Post by HaWe »

IMO it's a good advice to ALWAYS check "enhanced firmware" if you're working with NXC (and of course having it already uploaded to the NXT).

Another hint according to my experiences:
better always drop "until" if possible and ever use "while" if possible.

"until" often and easily leads to logical misunderstandings, misinterpretations, and misconstructions;
additionally, "until" is no legal standard C keyword but only "while"

- regardless if any C preprocessor always allows you to
#define until(x) while(!x)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: simple code cause NXT brick frozen

Post by afanofosc »

There's definitely something going wrong - possibly a floating point type opcode incompatibility of some kind. Can you try with different optimization levels to see if it is an optimizer bug or a code generator bug? My guess is it is in the optimizer. It may be something that works with the enhanced firmware but not the standard firmware but I tried to avoid those sorts of issues as much as possible, i.e., code generation that hangs the standard firmware but works with enhanced vs code that doesn't compile for the standard firmware but does with the enhanced firmware. The latter is fine but I have tried to avoid the former.

Also, can you use the variables in the loop? They are being (partially) optimized out and that may lead to the bug you are seeing. Is there a "real code" case where this is hanging the brick or can you only replicate it in a trivial loop like you posted? For example, add code to write X and Y to the LCD using NumOut or something along those lines.

I would encourage you to use the "automatic firmware" option if that is not already checked. That lets you check 2.0 and Enhanced but then it will automatically switch to standard if that is what you have installed on your NXT.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 15 guests