NXC: strlen vs. StrLen

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: strlen vs. StrLen

Post by HaWe »

hello,
for NXC: what are the differences between strlen vs. StrLen ?
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC: strlen vs. StrLen

Post by spillerrec »

AFAIK, StrLen was renamed into strlen so that it correspond to the C function. StrLen still exists for backwards-compatibility. There is a couple of functions that are like that, however some might be macro versions, while others aren't iirc.

Anyway, both functions both uses ArrLen and then subtract one, no matter where NULL actually is...
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: NXC: strlen vs. StrLen

Post by timpattinson »

2/07 test release, level 2 optimization.
StrLen():

Code: Select all

task main(){
StrLen("Hello, world");

}
produces

Code: Select all

dseg	segment
;------- definitions -------
;------- declarations -------
__D0main	sdword	
____initialize_global_data_return	byte	
__constVal1	sbyte	1
__constStr0009	byte[]	'Hello, world'
__strbufmain	byte[]	
dseg	ends
;------- code -------
thread main
	subcall __initialize_global_data, ____initialize_global_data_return
	strcat __strbufmain, __constStr0009
	arrsize __D0main, __strbufmain
	sub __D0main, __D0main, __constVal1
	exit -1, -1
endt
;------------------------
subroutine __initialize_global_data
	subret ____initialize_global_data_return
ends
;------------------------
strlen():

Code: Select all

task main(){
strlen("Hello, world");

}
produces

Code: Select all

dseg	segment
;------- definitions -------
;------- declarations -------
____initialize_global_data_return	byte	
dseg	ends
;------- code -------
thread main
	subcall __initialize_global_data, ____initialize_global_data_return
	exit -1, -1
endt
;------------------------
subroutine __initialize_global_data
	subret ____initialize_global_data_return
ends
;------------------------
Commit to Lego Mindstorms StackExchange Q&A http://area51.stackexchange.com/proposals/4105
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: strlen vs. StrLen

Post by HaWe »

I would not prejudge your evaluation now - but at first glance, it looks as if strlen code is shorter...
any other significant differences?
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: NXC: strlen vs. StrLen

Post by timpattinson »

I don't know, it appears smaller and faster, but i'm a n00b at reading asm
It also appears the call has been omitted from the version using strlen()
Commit to Lego Mindstorms StackExchange Q&A http://area51.stackexchange.com/proposals/4105
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC: strlen vs. StrLen

Post by spillerrec »

Please be aware that the optimizer might remove unneeded code and this is why the second program is smaller, it is simply empty...
Remember to use TextOut/NumOut or at least something to make sure the calculated values are actually used. (It is interesting that only one of the functions disappear though.)
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: strlen vs. StrLen

Post by afanofosc »

Here's the code without it being removed by the optimizer:

Code: Select all

task main()
{
  NumOut(0, LCD_LINE1, strlen("Hello, world"));
/*
	strcat __strbufmain, __constStr0009
	mov __strlen_7qG2_str_7qG2_000_inline_main, __strbufmain
	arrsize __D0strlen_inline_main, __strlen_7qG2_str_7qG2_000_inline_main
	sub __D0main, __D0strlen_inline_main, __constVal1
*/
  NumOut(0, LCD_LINE2, StrLen("Hello, world"));
/*
	strcat __strbufmain, __constStr0009
	arrsize __D0main, __strbufmain
	sub __D0main, __D0main, __constVal1
*/
}
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

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