Skip to content

Instantly share code, notes, and snippets.

@jobscry
Created November 25, 2013 00:09
Show Gist options
  • Save jobscry/7634315 to your computer and use it in GitHub Desktop.
Save jobscry/7634315 to your computer and use it in GitHub Desktop.
/**
* Hello Generator
*
* Joe Vasquez <joe.vasquez@gmail.com>
*
* I wish I knew more assembly!
**/
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define MAX_SIZE 64
#define MAX_JUNK_LINES 53
#define JUNK_GROUPS 5
#define NOP "nop"
#define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890!@#$%&*."
#define NUM_FILES 10
#define NEWLINE "\n"
#define STR_TERMINATOR "$"
#define P_NOP 45
/**
* add_nop
*
* based on current filesize and current junk lines, decide to add a nop
* decicsion based on probability of nop (p_nop)
*
* returns nop
**/
string add_nop(int &currentFileSize, int &currentJunkLines) {
if((currentJunkLines < MAX_JUNK_LINES) || (currentFileSize < MAX_SIZE)) {
bool add_it = (rand() % 100) <= P_NOP;
if(add_it) {
currentFileSize++;
currentJunkLines++;
string newNop = NOP;
return newNop.append(NEWLINE);
}
}
return "";
}
/**
* add_nops
*
* based on current file size and current junk lines, decide how many nops to try
* number of nops to try is 0 to JUNK_GROUPS
*
* returns string of nops
**/
string add_nops(int &currentFileSize, int &currentJunkLines) {
string nops = "";
int numNops = rand() % JUNK_GROUPS;
for(int i = 0; i < numNops; i++)
nops.append(add_nop(currentFileSize, currentJunkLines));
return nops;
}
/**
* make_noise
*
* fill rest of file with random letters
*
* returns noise
**/
string make_noise(int currentFileSize) {
string noise = "";
string letter = "";
for(int i = currentFileSize; i < MAX_SIZE -1 ; i++){
letter = ALPHABET[rand() % (sizeof(ALPHABET) - 1)];
noise.append(letter);
}
noise.append(STR_TERMINATOR);
return noise;
}
int main() {
srand(time(NULL));
string fileName;
string command;
int currentFileSize;
int currentJunkLines;
for(int i = 0; i < NUM_FILES; i++) {
ostringstream number;
number << i;
fileName = "hello_";
fileName.append(number.str());
fileName.append(".asm");
currentFileSize = 0;
currentJunkLines = 0;
command = "nasm -f bin ";
ofstream fh(fileName.c_str());
if(fh.is_open()){
fh << "ORG 100h";
fh << NEWLINE;
fh << add_nops(currentFileSize, currentJunkLines);
fh << "mov ax, cs";
fh << NEWLINE;
currentFileSize += 2;
fh << add_nops(currentFileSize, currentJunkLines);
fh << "mov ds, ax";
fh << NEWLINE;
currentFileSize += 2;
fh << add_nops(currentFileSize, currentJunkLines);
fh << "mov dx, hel";
fh << NEWLINE;
currentFileSize += 3;
fh << add_nops(currentFileSize, currentJunkLines);
fh << "mov ah, 09h";
fh << NEWLINE;
currentFileSize += 2;
fh << add_nops(currentFileSize, currentJunkLines);
fh << "int 21h";
fh << NEWLINE;
currentFileSize += 2;
fh << add_nops(currentFileSize, currentJunkLines);
fh << "mov ax, 4C00h";
fh << NEWLINE;
currentFileSize += 3;
fh << add_nops(currentFileSize, currentJunkLines);
fh << "int 21h";
fh << NEWLINE;
currentFileSize += 2;
fh << add_nops(currentFileSize, currentJunkLines);
fh << "hel: db \"Hello World!$\"";
fh << NEWLINE;
currentFileSize += 13;
fh << add_nops(currentFileSize, currentJunkLines);
fh << "noise: db \"";
fh << make_noise(currentFileSize);
fh << "\"";
fh << NEWLINE;
fh.close();
command.append(fileName);
command.append(" -o hello_");
command.append(number.str());
command.append(".com");
system(command.c_str());
number.clear();
} else {
cout << "Could not open file " << fileName << NEWLINE;
}
}
system("del *.asm");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment