Self Displaying Program

Share your advanced PureBasic knowledge/code with the community.
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Self Displaying Program

Post by Konne »

Code updated for 5.20+

This Program displays itsself in the debugger. Maybe someone can make it smaller.

Code: Select all

C.s="C.s=#:C=ReplaceString(C,Chr(35),Chr(34)+C+Chr(34)):Debug C":C=ReplaceString(C,Chr(35),Chr(34)+C+Chr(34)):Debug C
Apart from that Mrs Lincoln, how was the show?
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

waaahouuu that's so strange :shock:

what the use for such code ?

how it works ? i didn't understand at all your logic...
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

That's clever.
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

lol :D cool
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

:shock:

Wow!
I may look like a mule, but I'm not a complete ass.
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Post by Konne »

Flype wrote: what the use for such code ?
Normally you would think this would be a Infinity Recursive problem.
You may think it's impossible to make a Quote of a Quote etc

But thanks to Stringmanipulation it's possible :)

Once you understand the concept you have to make the program smaller. The one making the smallest PB Program wins ;)
Apart from that Mrs Lincoln, how was the show?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

It nearly sent me crackers trying to figure out what the hell was going on!

:)

It is very clever.
I may look like a mule, but I'm not a complete ass.
Tommeh
Enthusiast
Enthusiast
Posts: 149
Joined: Sun Aug 29, 2004 2:25 pm
Location: United Kingdom

Post by Tommeh »

Ok, as we seem to be using PB hacks :p here is one for you

Code: Select all

String$ = "hello"     ;String is defined
String$ = "world"    ;String is redefined with new value

!MOV    edx,dword _S1
!MOV    [v_value],edx

Debug PeekS(value, 5)       ;Get previous value
Debug PeekS(value+6, 5)     ;Get new value
You can also use this method to retreve defined variable names and values :p
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Post by Konne »

It's not at all a PB Hack.

It's understandable with a couple of comments and the replacement of :'s.

Code: Select all

C.s="C.s=#:C=ReplaceString(C,Chr(35),Chr(34)+C+Chr(34)):Debug C"    ;We save the Code in this Variable. # is then Replaced with " +Code +"
C=ReplaceString(C,Chr(35),Chr(34)+C+Chr(34))  ;Here we Replace # (Chr(35))  with the Code.And Two " (Chr(34))
Debug C       ;Here we debug the Code
Apart from that Mrs Lincoln, how was the show?
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

C.s="C.s=#:C=ReplaceString(C,Chr(35),Chr(34)+C+Chr(34)):Debug C":C=ReplaceString(C,Chr(35),Chr(34)+C+Chr(34)):Debug C
OK keep in mind I am dense! but that is what pops up in debug... Oy I feel stupid for asking but that is what is Supposed to happen???
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

It's a program that is capable of displaying it's own source code, thats called a quine see http://en.wikipedia.org/wiki/Quine_%28computing%29
JCV
Enthusiast
Enthusiast
Posts: 580
Joined: Fri Jun 30, 2006 4:30 pm
Location: Philippines

Post by JCV »

:D
1st kind of code to see in pb.
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Self Displaying Program

Post by #NULL »

Konne wrote:This Program displays itsself in the debugger. Maybe someone can make it smaller.
i once posted one in the german forum: http://forums.purebasic.com/german/view ... f=8&t=8471
apparently its 3 characters shorter :mrgreen: and there are still 4 spaces that can be eliminated..

Code: Select all

s$="s$=:Debug Mid(s$,1,3)+Chr(34)+s$+Chr(34)+Mid(s$,4,100)":Debug Mid(s$,1,3)+Chr(34)+s$+Chr(34)+Mid(s$,4,100)
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Self Displaying Program

Post by Demivec »

#NULL wrote:
Konne wrote:This Program displays itsself in the debugger. Maybe someone can make it smaller.
i once posted one in the german forum: http://forums.purebasic.com/german/view ... f=8&t=8471
apparently its 3 characters shorter :mrgreen: and there are still 4 spaces that can be eliminated..

Code: Select all

s$="s$=:Debug Mid(s$,1,3)+Chr(34)+s$+Chr(34)+Mid(s$,4,100)":Debug Mid(s$,1,3)+Chr(34)+s$+Chr(34)+Mid(s$,4,100)
This code is shorter by 11 more characters (under 100) :mrgreen: .

Code: Select all

s.s="s.s=:Debug Mid(s,1,4)+Chr(34)+s+Chr(34)+Mid(s,5)":Debug Mid(s,1,4)+Chr(34)+s+Chr(34)+Mid(s,5)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Self Displaying Program

Post by wilbert »

Left will make it two chars shorter

Code: Select all

s.s="s.s=:Debug Left(s,4)+Chr(34)+s+Chr(34)+Mid(s,5)":Debug Left(s,4)+Chr(34)+s+Chr(34)+Mid(s,5)
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply