Page 1 of 1

NXC: strlen vs. StrLen

Posted: 24 Jul 2011, 13:49
by HaWe
hello,
for NXC: what are the differences between strlen vs. StrLen ?

Re: NXC: strlen vs. StrLen

Posted: 24 Jul 2011, 17:31
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...

Re: NXC: strlen vs. StrLen

Posted: 25 Jul 2011, 09:08
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
;------------------------

Re: NXC: strlen vs. StrLen

Posted: 25 Jul 2011, 09:29
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?

Re: NXC: strlen vs. StrLen

Posted: 25 Jul 2011, 10:21
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()

Re: NXC: strlen vs. StrLen

Posted: 25 Jul 2011, 13:46
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.)

Re: NXC: strlen vs. StrLen

Posted: 25 Jul 2011, 20:48
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