strlen

Discussion of challenges you have already solved
Bryghlawg
Posts: 1
Joined: Mon Nov 10, 2008 10:34 pm

strlen

Post by Bryghlawg »

Mmh, I wonder how call stack overflow proof the VM used to validate the solutions really is...
My program surely will eat up call stack space like mad:

00^<2?2gp!1+1c

;-)
User avatar
m!nus
Posts: 202
Joined: Sat Jul 28, 2007 6:49 pm
Location: Germany

Post by m!nus »

I wanted to post my code aswell, then realized I hadn't saved it. So I go open the challenge and start typing code, my solution turned out to still be in the browser history buffer.

00^<4?1+1cp!

Yes, mine abuses calls aswell.
MerickOWA
Posts: 182
Joined: Mon Apr 07, 2008 5:54 pm
Location: HkRkoz al KuwaiT 2019 HaCkEr 101

Post by MerickOWA »

i dont think the "!" is necessary, not if its at the end of the program anyways
megabreit
Posts: 141
Joined: Sat Jan 03, 2009 3:33 pm

strlen counts \0?

Post by megabreit »

Looks like I have to find out how the call stack trick works :P
Til then the "straight forward" version that cleans up the stack:

00<0:26*?1+0^044*3+-gp

My first version subtracted 1 because strlen in C is defined as length
without the trailing \0! Maybe this should be pointed out in the challenge...
it's quite confusing to get the "right" answer rejected.
tails
Posts: 191
Joined: Tue Jun 10, 2008 7:51 pm
Location: Tokyo

Re: strlen counts \0?

Post by tails »

No, it does not count \0.

And you don't need to subtract 1 from the \0 position.

If the given string is "abc" for example, the \0 pisition is 3, and the length of the string is also 3.

mem[0] = 'a'
mem[1] = 'b'
mem[2] = 'c'
mem[3] = 0
megabreit
Posts: 141
Joined: Sat Jan 03, 2009 3:33 pm

Post by megabreit »

Ooops. You're right... another Obiwan (off by one) error.
Somehow I'm attracted by those things. :shock:

I finally made it right.. even with slightly wrong thoughts.

Thanks for pointing this out!
PeterS
Posts: 24
Joined: Thu Mar 05, 2009 7:17 pm

Post by PeterS »

I think writing programs for the Hack VM even worse than writing assembly. What's the point of this programming language?

However, what do you mean by 'call stack overflow' and 'abusing calls'?

By the way, this is my program:
http://img3.imagebanana.com/img/o2fuqhh ... nshot1.png

Code: Select all

00<0:6?1+0^2cp
z3ro
Posts: 1
Joined: Tue Jun 16, 2009 11:34 pm

Post by z3ro »

The obtuseness bugged me, so I wrote a quick assembler for it; to help out with HVM problems.
It let me write strlen in 10 seconds! [wrote the assembler originally for the King Mouse problem].

Now I can write code like:

Code: Select all

	const   0
loop:
	dup     0
	fetch
	rjmpiz  done
	const   1
	add
	rjmp    loop
	
done:
	printint
	end
and let the assembler do the job of translating that into

Code: Select all

00^<     92+?1+ 092*5+-gp!
for me. I need to make the linker a bit smarter so rather than reserving lots of space for a link constant and then just filling what it needs, it uses a smarter algorithm to calculate inner constants first, but oh well, this works :). Its never going to beat the size limit problems, but it makes the no-size-limit-ones much more fun.
therethinker
Posts: 144
Joined: Fri Mar 28, 2008 11:29 pm
Location: #hacker.org on Freenode

Post by therethinker »

I tried writing an assembler that also had a few more utility functions (!= loops, for example). It was meh... It was easier to do them by hand.

(Reminder that adum doesn't want us sharing assemblers, but talking about them, I presume, is fine)
User avatar
livinskull
Posts: 22
Joined: Fri Jun 26, 2009 12:01 pm
Location: /dev/null
Contact:

Post by livinskull »

Now whats the matter with abusing call stack ?
My program needs exactly the same cycle-count but seems much bigger ;)

Code: Select all

099*9*>099*9*1+>99*9*1+<<95*5-?99*9*1+<1+99*9*1+>99*9*<1+99*9*>09-6*1-g99*9*<p
However its kinda bad when the string is too long... storing some counter at 729 and pointer at 730
megabreit
Posts: 141
Joined: Sat Jan 03, 2009 3:33 pm

Post by megabreit »

The thing about the call stack is simple: "g" and "?" are relative jumps, but "c" is an absolute jump that places a return address onto the call stack. But there is no law to use "$" whenever you used "c". And if you don't return from your subroutines, the stack fills up.
Since all the challenges I saw do not require to cleanup anything (they only need to terminate without error), "abusing" the call stack is a legitimate way to achieve certain goals.

Regarding the assembler: There are plenty of HVM challenges, and if I was able to write an assembler/compiler for HVM I would do that (King Mouse is still on my todo list). Fortunately there is the Java IDE which makes it a lot easier to write and debug HVM programs.

I don't think that adum cares about how the challenges are solved (as long as you do it on your own) ... you know... this is hacker.org. But maybe I'm wrong... adum?
User avatar
livinskull
Posts: 22
Joined: Fri Jun 26, 2009 12:01 pm
Location: /dev/null
Contact:

Post by livinskull »

I know what a callstack is and how it works ;)

What im wondering is why bother using it, when using variables in memory is executed at the same speed and much less complex.
megabreit
Posts: 141
Joined: Sat Jan 03, 2009 3:33 pm

Post by megabreit »

E.g. to make your code faster or shorter... watch out for the next HVM challenges :lol:
wizard0815
Posts: 1
Joined: Thu Oct 30, 2008 6:34 pm
Location: Stuttgart/Germany

strlen ...

Post by wizard0815 »

My version don't use the call stack.

Code: Select all

00^<0:0^7?+09-7-gdp!
DaymItzJack
Posts: 106
Joined: Thu Oct 29, 2009 9:21 pm

Post by DaymItzJack »

00^<8?1+058+-gp

=/
Post Reply