Hello World.

Apr 27, 2011 at 1:31am
Favorite New Hello World =]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void* voidfunction(void* (*funpoint)(void*),void* param){
 return funpoint(param);
}

void* outthis(void* str){
 int ret = false;
 if (ret = (str != NULL))
 cout << (char*)str;
 return (void*)(int*)&ret;
}


int main(){
return *(int*)voidfunction(&outthis,(void*)"\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64");
}


Apr 27, 2011 at 3:07am
I wanted to post this before hamsterman did:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MEMSIZE	32768

int brainfuck(const char* code, unsigned char memory[], unsigned msize)
{
	unsigned csize = strlen(code),
		 cindex = 0,
		 mindex = 0;
	for (; cindex < csize; ++cindex) {
		switch (code[cindex]) {
			case '<':
				if (mindex > 0)
					--mindex;
				break;
			case '>':
				if (mindex < msize)
					++mindex;
				break;
			case '+':
				memory[mindex]++;
				break;
			case '-':
				memory[mindex]--;
				break;
			case '.':
				putchar(memory[mindex]);
				fflush(stdout);
				break;
			case ',':
				memory[mindex] = getchar();
				if (memory[mindex] != '\n')
					getchar();
				break;
			case '[':
				if (memory[mindex] == 0) {
					int i = cindex;
					int found = 0;
					while (i < csize) {
						if (code[i] == '[') {
							found = 1;
							break;
						}
						++i;
					}
					if (!(found)) {
						fprintf(stderr, "Unmatched '['\n");
						return 1;
					} else {
						cindex = i;
					}
				}
				break;
			case ']':
				if (memory[mindex] != 0) {
					int i = cindex;
					int found = 0;
					while (i --> 0) {
						if (code[i] == '[') {
							found = 1;
							break;
						}
					}
					if (!(found)) {
						fprintf(stderr, "Unmatched ']'\n");
						return 1;
					} else {
						cindex = i;
					}
				}
				break;
			default:
				break;
		}
	}

	return 0;
}

int main(int argc, char* argv[])
{
	if (argc < 2) {
		fprintf(stderr, "Usage: %s <brainfuck code>\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	unsigned char memory[MEMSIZE] = {0};
	return brainfuck(argv[1], memory, MEMSIZE);
}


$ /hello ">+++++++++[<++++++++>-]<.>+++++++[<++++>-]
<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++
[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]
<+.[-]++++++++++."
Hello world!

(remove the line breaks above when actually calling the program)
Last edited on Apr 27, 2011 at 3:08am
Apr 27, 2011 at 10:54am
blast...
Apr 27, 2011 at 11:16am
Blasting requires dynamite.
Apr 27, 2011 at 2:49pm
Here's mine. It is incredibly likely not to work on your machine.
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int print[] = { 69485767, 1819043144, 136594631, 1867980911, 203703495, 6581362, 1147936963 };

int main(){
   char hello_world[12];
   ( ( void(*)() ) ( void* ) print )();
   std::cout << hello_world;
   return 0;
}
Apr 27, 2011 at 3:47pm
@hamsterman,
Ironically, running opcodes from memory is exactly what I originally tried to do, but I couldn't get it to work (it just segfaulted).
Apr 27, 2011 at 5:12pm
This is probably my favorite Hello World out of all the ones I've made.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* 
 * File:   therealhelloworld.cpp
 * Author: Albatross
 *
 * Created... actually about a year ago. I think.
 */

#define __STDC_LIMIT_MACROS 0
#define __STDC_CONSTANT_MACROS 0

#include <llvm/Support/raw_ostream.h>
#include <string>
#include <exception>

class querytimeout : std::exception
{
public:
	virtual const char* what() const throw()
	{
		return "No response from the world, probably because it's overwhelmed with \"Hello!\" queries.";
	}
};


std::string query(std::string,std::string);
int main()
{
    try {llvm::outs() << query("Hello!","World");}
    catch (querytimeout& e)
    {
        llvm::errs().changeColor(llvm::raw_ostream::RED) << e.what() << "\n";
        llvm::errs().resetColor();
        return 1;
    }
    return 0;
}
std::string query(std::string query,std::string towhom)
{
    if(towhom == "World")
        throw querytimeout();
    else
        return towhom + " doesn't want to talk to you today.\n";
}
(In red) No response from the world, probably because it's overwhelmed with "Hello!" queries.


EDIT: Added output.

-Albatross
Last edited on Apr 27, 2011 at 5:16pm
Apr 27, 2011 at 5:33pm
Depends on the OS and processor. Good OSes don't allow running code from the data segment for obvious reasons.
Apr 27, 2011 at 5:50pm
@rapdicoder,
You can do it on Linux with the mprotect function, but I couldn't figure it out.
Apr 28, 2011 at 9:39pm
Hello OOP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include <iostream>

using namespace std;

class Hello {
	friend ostream& operator<<(ostream&, const Hello&);
	char *msg;
	
 public:
	Hello(char msg[]) { this->msg = msg; }
};

ostream& operator<<(ostream &out, const Hello &hw) {
	out << "Hello, " << hw.msg << "!";
	return out;
}

int main() {
	cout << Hello("world of OOP") << endl;
	return 0;
}
Apr 29, 2011 at 10:20pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
	char Pieces[16] = {'H', 'e', 'l', 'o', ',', ' ', 'W', 'r', 'd', '!'};
	vector<bool> Bits;
	string HelloWorld = "0000000100100010001101000101011000110111001010001001";

	for(unsigned long i = 0; i < HelloWorld.length(); ++i)
	{
		Bits.push_back(bool(HelloWorld[i] - '0'));
	}

	for(unsigned long i = 0; i < Bits.size() / 4; ++i)
	{
		char SemiByte = Bits[i * 4 + 3] + Bits[i * 4 + 2] * 2 + Bits[i * 4 + 1] * 4 + Bits[i * 4] * 8;
		cout << Pieces[SemiByte];
	}
}
Hello, World!
If you do it without the comma, you can compress it to perfectly fit 3 bits per letter instead of 4 :)
Last edited on Apr 29, 2011 at 10:21pm
Topic archived. No new replies allowed.