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
Blasting requires dynamite.
@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).
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
Depends on the OS and processor. Good OSes don't allow running code from the data segment for obvious reasons.
@rapdicoder,
You can do it on Linux with the mprotect function, but I couldn't figure it out.
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