assembly hello worlds [intel32]

There are numerous ways to write a hello world program in every programming language. Here are 6 written in 32b assembly using nasm

The top 2 are the std ways of writing this, all the rest are for schittz&gigglz

1-std
1.5-std (NASM style)
2-std using functions
3-writing 1 byte at a time with a forloop
4-pushing 1B, padded so it’s 4B aligned, onto stack, and then writing 1B at a time
5-null-terminated: write until 0x0 encountered
6-pushing string onto stack, and then writing from it

;Guandi97
;std method, the "correct" way to do this

section .text
global _main
_main:
	push 	len
	push	sdr
	push	0x1
	sub	esp,		0x4
	mov	eax,		0x4
	int	0x80
	add	esp,		0x10

	push	0x1
	sub	esp,		0x4		;always 8-byte aligned on intel32
	mov	eax,		0x1
	int	0x80
section .data
sdr	db	"noobie plz",0xa		;consecutive byte sequence, or char array
len	equ	$-sdr				;this is the same as the sizeof() in C

This is an easier way to output a string to the console.

 

;Guandi97
;function call method, the other "correct" way to do this

section .text
global _main
_main:
	push	dword 	sdr
	push 	dword 	len
	call	_rite
	add	esp,		0x8

	push	0x1
	sub	esp,		0x4		;always 8-byte aligned on intel32
	mov	eax,		0x1
	int	0x80
_rite:	push	ebp
	mov	ebp,	esp
	push	dword 	[ebp+0x8]
	push	dword 	[ebp+0xc]
	push	dword	0x1
	sub	esp,		0x4
	mov	eax,		0x4
	int	0x80
	add	esp,		0x10
	pop	ebp
	ret
section .data
sdr	db	"Lunar Republic",0xa
len	equ	$-sdr
;Guandi97
;using a forloop 

;calculate esi,edi
;push args onto stack
;write 1B at a time, and update pointer

section .text
global _main
_main:
	push	ebp
	mov	ebp,		esp	;00
	mov	esi,		sdr
	mov	edi,		esi
	add	edi,		len

	push	dword	0x1		;04
	push	dword 	0x0		;08
	push	dword	0x1		;12
	sub	esp,		0x4	;16

wloops	cmp	esi,		edi
	jge	wloope
	mov	[ebp-0x8],	esi
	mov	eax,		0x4
	int 	0x80
	add	esi,		0x1
	jmp	wloops

wloope	add	esp,		0x10
	push	dword	0x1
	sub	esp,		0x4
	mov	eax,		0x1
	int	0x80
section .data
sdr	db	'twilicorn OP',0xa
len	equ	$-sdr
;Guandi97
;push method

;calculate len 
;push all onto stack
;write 1B at a time

section .text
global _main
_main:
	mov	esi,		sdr
	mov	edi,		esi
	add	esi,		len
ploops	cmp	esi,		edi		;push onto stack
	jl	ploope
	mov	edx,		0x0
	mov	dl,		byte [esi]
	push	edx
	sub	esi,		0x1
	jmp	ploops
ploope	mov	ecx,		0x0
wloops	cmp	ecx,		len		
	jge	wloope
	push	dword 	0x1			;-04
	mov	esi,	esp
	add	esi,	0x4
	push	dword 	esi			;-08			
	push	dword 	0x1			;-12
	sub	esp,		0x4		;-16
	mov	eax,		0x4
	int	0x80
	add	esp,		0x14
	add	ecx,		0x1
	jmp	wloops
wloope	push	dword 	0x1
	sub	esp,		0x4
	mov	eax,		0x1
	int	0x80
section .data
sdr	db	'engieUNion!',0xa
len	equ	$-sdr
;Guandi97
;null terminated while loop

;set edi,esi
;check against 0x0
;write 1B at a time

section .text
global _main
_main:
	push	ebp
	mov	ebp,		esp
	mov	esi,		sdr
	push	dword 	0x1			;04
	push	dword	0x0
	push	dword	0x1
	sub	esp,		0x4
	mov	edi,		ebp
	sub	edi,		0x8
cloops	cmp	byte [esi],	0x0
	je	cloope
	mov	[edi],		esi
	mov	eax,		0x4
	int	0x80
	add	esi,		0x1
	jmp	cloops
cloope	mov	[esp],		dword 0x1
	sub	esp,		0x4	
	mov	eax,		0x1
	int	0x80
section .data
sdr	db	'PootisSpencer here!',0xa,0x0
;Guandi97
;stack byte array method: this method has almost no application irl

;determine esi,edi
;loop esi
;call _byteSStack
;"flush" ebx to stack
;set args, call write

;_byteSStack (8 bit)
	;store in ebx(buffer), accum in eax,
	;shift and add to bl
;_Bflush
	;determin the number of shifts needed to align the fist byte in with the most significant bit in ebx
	;loop shifts

section .text
global _main
_main:
	push	ebp
	mov	ebp,		esp

	mov	edi,		sdr
	mov	esi,		edi
	add	esi,		len
	mov	ebx,		0x0
aloop1s	cmp	esi,		edi
	jle	aloop1e
	call	_byteSStack
	cmp	eax,		0x4		;if eax is full, flush immediately
	jge	bsPush
	jmp	aloop1s
bsPush	push	dword	ebx
	mov	ebx,		0x0
	mov	eax,		0x0
	jmp	aloop1s

aloop1e	cmp	eax,		0x0
	jne	flush
	jmp	end
flush	call	_Bflush				;need to flush manually
	push	dword	ebx
end	push	len		
	mov	edx,		esp
	add	edx,		0x4
	add	edx,		eax
	push	dword 	edx
	push	0x1
	sub	esp,		0x4
	mov	eax,		0x4		;since last 2 args on stack are the same needed for sys_exit(1), interrupt
	int	0x80				;demonstration only, this is bad practice,
	mov	eax,		0x1		;it will confuse both future you, and everybody else who views your code
	int	0x80

_byteSStack:
	sub	esi,		0x1
	shl	ebx,		8		;shift 8b to left
	mov	bl,		byte [esi]
	add	eax,		0x1
	ret
_Bflush:
	mov	edx,		0x4
	sub	edx,		eax
	mov	eax,		edx
	mov	ecx,		0x0
floops	cmp	ecx,		eax		;shift until aligned
	jge	floope
	shl	ebx,		0x8		;shift 8b to left
	add	ecx,		0x1
	jmp	floops
floope	ret
section .data
sdr	db	"AnonyMOOOOOOOOOOooooooooooose",0xa,
len	equ	$-sdr

bit Field manipulation [C]

a program demonstrating the different ways to manipulate bits
xor-^
or-|
and-&

some common library functions have been rewritten in an attempt to avoid the standard libraries as much as possible. (for educational purposes)

read(),write() are unix system calls writing to file descriptors
the standard file descriptors open by default are:
0-stdin
1-stdout
2-stderr

the different modes:
m0-use xor
m1-use |,&
m2-assign a number to the 8 bit wide bit field, entering 256 will cause it to overflow, displaying all zeros

#include <stdlib.h>
#include <unistd.h>

void bon(size_t,char*);
void boff(size_t,char*);
void btog(size_t,char*);
void bprint(char*);
int bchek(size_t,char*);
int charLen(char*);
int charInt(char*);
int memcompare(char*,char*,size_t);

int main()
{
	char *buff=(char*)malloc(10); //80 bits
	char *dump=(char*)malloc(100); //800 bits
	char *bfld=calloc(1,1);
	int i;
	int j=0;

	#define INSTR1 "enter: [0-7],print,quit,m[0-2]\nm0:xor\tm1:or/and\tm2:assign #\n"
	write(1,INSTR1,sizeof(INSTR1));

	while(1)
	{
		if(read(0,buff,10)==10) 
		{
			if(*(buff+9)!=0xa) while(100==read(0,dump,100));
			else *(buff+9)=0x0;
		}

		if(memcompare("quit\n",buff,5)==0) exit(0);
		else if(memcompare("print\n",buff,6)==0)
		{
			bprint(bfld);
			continue;
		}
		else if(*buff=='m' && *(buff+1)>47 && *(buff+1)<51)
		{
			if(*(buff!=0xa)
			{
				#define INSTR2 "invalid input\n"
				write(1,INSTR2,sizeof(INSTR2));
				continue;
			}
			j=charInt((buff+1));
			continue;
		}
		else
		{
			if(j==0)
			{
				if(!(*buff>47 && *buff<56) || *(buff+1)!=0xa)
				{
					write(1,INSTR2,sizeof(INSTR2));
					continue;
				}
			}
			else if(j==1)
			{
				if(!(*buff>47 && *buff<56) || *(buff+1)!=0xa)
				{
					write(1,INSTR2,sizeof(INSTR2));
					continue;
				}
			}
		}

		i=charInt(buff);

		if(j==0) btog(i,bfld);
		else if(j==1)
		{
			if(bchek(i,bfld)==0) bon(i,bfld);
			else boff(i,bfld);
		}
		else *bfld=i;

		bprint(bfld);
	}

	return 0;

}

void bon(size_t i,char *bfld)
{
	*bfld|=(1<<i);
}
void boff(size_t i,char *bfld)
{
	*bfld&=~(1<<i);
}
void btog(size_t i,char *bfld)
{
	*bfld^=(1<<i);
}
int bchek(size_t i,char *bfld)
{
	if((*bfld&(1<<i))==0) return 0;
	else return 1;
}
void bprint(char *bfld)
{
	char i,b[9]={0};
	for(i=0;i<8;i++)
	{
		if(bchek(7-i,bfld)==0) b[i]=48;
		else b[i]=49;
	}
	b[++i]=0xa;
	write(1,&b,10);
}

int charLen(char *c)
{
	int i=0;
	while(*(c+i)!=0xa && *(c+i)!=0x0) 
	{
		i++;
	}
	return i;
}
int charInt(char *c)
{
	int i,j,k=0,l=1;

	for(j=charLen(c)-1;j!=-1;j--)
	{
		switch(*(c+j))
		{
			case '0': i=0; break;
			case '1': i=1; break;
			case '2': i=2; break;
			case '3': i=3; break;
			case '4': i=4; break;
			case '5': i=5; break;
			case '6': i=6; break;
			case '7': i=7; break;
			case '8': i=8; break;
			case '9': i=9; break;
		}
		k+=i*l;
		l*=10;
	}
	return k;
}
int memcompare(char *str1,char *str2,size_t j)
{
	int i;
	for(i=0;i<j;i++)
	{
		if(*(str1+i)!=*(str2+i)) return i+1;
	}
	return 0;
}

formatting .dat files for rhino

XFLR5, a wing analysis/profile plotting software exports the wing profile coordinates into a .dat file.
in order to use this file with rhino, you must reformat it from this:

 1.00000     0.00000
 0.95544     0.00396
 0.91361     0.00764
 ...

to this:

1.00000,0.00000
0.95544,0.00396
0.91361,0.00764
...

because I’m lazy, I wrote a Z-shell script to automate this process
it also adds commands to the file, so you can use the readCommandFile command to automatically plot the airfoil

#! /bin/zsh

#Guandi97
#micropenguin.net

function help
{
	echo -e DaTRhiCoor: Data to Rhino Coordinates!
	awk 'BEGIN {printf("usage: %s [-flags] \n","DaTRhiCoor")}'
	echo -e "flags:\n\t-r\tadd rhino commands\n\t-n\tdon't add commands"
	echo -e "the game"
	exit
}
if [[ $# -lt 2 ]] 
then
	help
fi
if [[ ! ( -a $2 ) ]]
then
	echo "file '$2' not found"
else
	if [[ -z $3 ]]
	then
		3="output"
	fi	
	if [[ $1 == "-r" ]]
	then
		echo !interpCrv | tee $3
	fi

	feof=0
	i=0
	linA=$(wc -l $2 | tr ' ' '\n' | grep -e '^\d')
	linB=$(echo -e $linA | awk '{print $1*2}')

	stream=$(cat $2 | tr ' ' '\n' | grep -e '^\d\|^-\d')
	for i in {1..$linA..2}
	do
		let "j = i+1"
		echo -e $stream | sed -n "${i}p" | tr '\n' ',' | tee -a $3
		echo -e $stream | sed -n "${j}p" | tee -a $3
	done
	if [[ $1 == "-r" ]]
	then
		echo -e 'enter\ninterpCrv' | tee -a $3
	fi
	let "linA = linA+1"
	for i in {$linA..$linB..2}
	do
		let "j = i+1"
		echo -e $stream | sed -n "${i}p" | tr '\n' ',' | tee -a $3
		echo -e $stream | sed -n "${j}p" | tee -a $3
	done
	if [[ $1 == "-r" ]]
	then
		echo -e enter | tee -a $3
	fi	
	
	echo -e "\ndone: $linB lines parsed"
	
fi

Checking steam profiles

using built in shell commands, I coded a quick and dirty (30 min) shell script to check steam profiles

lwp-request flushes the html into the stdio which I pipe into grep to sort out the only line relevant

I then use regex to determine the status of the target profile
then I have apps that show a pop up whenever the target’s status changes

there is a verbose mode (mainly for debugging) and a way to change the check rate of the script

usage ex: sh engieUNion -vt 5
this would put it in verbose mode, and change the refresh time to 5 seconds

-updated

#!/bin/bash

c=0
d=""
i=0
t=30
g=""
o=""
p=""
v=3
z=ZAMN

if [[ "$1" == *"t"* ]]
then
	t=$2
fi
if [[ "$1" == *"v"* ]]
then
	v=1
fi

while [ $i -lt 1 ]
do
	lwp-request -ft 100 http://steamcommunity.com/id/$z | grep 'profile_in_game\|steam://connect' > html
	o=$(cat html | grep "profile_in_game persona")

	if [[ "$o" == *"in-game"* ]] && [[ "$g" == *"Team Fortress 2"* ]]
 	then
	p=$(cat html | egrep -o '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\:[[:digit:]]{5}')
	g=$(cat html | grep "profile_in_game_name")
		if [ $c -ne 3 ]
		then
			c=3
			d=$(date)
			echo "$c" "        " "$d" 
			echo "$o" "         " "$g" | tee -a log		
			let "v = v+1"
			open inGame.app
		fi
		if [ $v -ne 2 ]
		then
			echo $g
		fi
		echo $p
	elif [[ "$o" == *"online"* ]]
	then
		if [ $c -ne 2 ]
		then
			c=2
			d=$(date)
			echo "$c" "        " "$d" "$o" | tee -a log
			let "v = v+1"
			open online.app
		fi
	elif [[ "$o" == *"offline"* ]]
	then
		if [ $c -ne 1 ]
		then
			c=1
			d=$(date)
			echo "$c" "        " "$d" "$o" | tee -a log
			let "v = v+1"
			open offline.app
		fi
	else
		v=2
		echo timeout
	fi

	if [ $v -eq 1 ]
	then
		echo "$c" "$o"
	elif [ $v -eq 2 ]
	then
		v=1
	fi

	sleep $t 	
done

Here is a C++ port:
-Justin

 

 

#include 
#include 
#include 
#include 
#include 
using namespace std;

int system(const char *command);

int main()
{

	std::string website = "http://steamcommunity.com/id/ZAMN";
//	std::cout << "Enter a website that you want to grab: "; 	//std::cin >> website;
	std::string command = "wget " + website;
	system(command.c_str());

	string search = "Currently Online";
	string line;

	ifstream inFile("Steam Community    -[o. W. n.]- No emaN.html");

	if (!inFile){
		cout << "Unable to open file" << endl;
		system("PAUSE");
		exit(1);

	}

	size_t pos;
	while (inFile.good())
	{
		getline(inFile, line); // get line from file
		pos = line.find(search); // search
		if (pos != string::npos) // string::npos is returned if string is not found
		{
			cout << "Found!";
			break;
		}
	}

	system("PAUSE");

}

Brute Force Exercise

minimal use of standard functions
I could have used atoi(), and casts, but I didn’t ;)
written in C

if wordpress screwed up the code, let me know

/*
 *Every character in the character set is interpreted as a numeric value
 *the program increments the value of s using the length of the character set as the base
 *if all lowercase letters in the English alphabet were given as the character set, then it would count in base 26 and store it into s
 *the numeric value stored in s, is then converted to the character and printed
 *such that: 
 *given: abc
 *then: a=0, b=1, c=2
 *if s={1,0,2,2,0}
 *then print: bacca
 */

#include <stdio.h>

void print();
int charint(char *cher);
int charlen(char *cher);
int d,f,i,j,k,l,m,n,o,u;
int *s;
char *p;

int main(int argc, char *argv[])
{
        //if no arguments are given, print usage and exit
	if(argc<2)
	{
		printf("usage:\n./Buck  \n\t<number>\tlength of password\n\t<string>\tcharacter set\n");
		return 1;
	}

        //set variables with their initial values
	d=0;
	j=1;
	k=1;	
	l=1;
	m=charlen(argv[1]);		
	n=charint(argv[2]);
	o=0;
	s=malloc(m*sizeof(int));
	p=malloc(m*sizeof(char));
	u=0;
	p=&argv[1][0];
	k=m-1;

        //using the password length, and number of characters given, determin how many combinations are possible
	while(j<=n)
	{
		f=1;
		for(i=0;i<j;i++) f=(f*m);
		o+=f;
		j++;
	}
	if(o<0) o=o*(-1);

        //print number of combinations, length of character set, and max password length
	printf("o: %d m: %d n: %d\n",o,m,n);

        //makes sure initial values for S are all 0
	for(i=0;i<=m;i++) *(s+i)=0;

	d=l-1;

        /*
         *Explanation of the goto's (cause people hate spaghetti code)
         *being that this was just an exercise, I decided to experiment with gotos a bit.
         *forloop: the ones place is a special case, which is why a seperate for loop was used to increment it, afterwards, I could either have added a identical for loop, or just jump to the bottom, and use the existing code
         *end: jumps out of the do loop, an alternative would have been to use a break
         *whileloop: used in place of a while loop
         */
	for(i=0;i<m;i++)
	{
		*(s+d)=i;
		print();
	}
	*(s+d)=0;
	l++;
	d=l-1;
	goto forloop;

	do
	{
		whyloop:;
		*(s+d)=0;
		i++;
		d--;
		*(s+d)=*(s+d)+1;
                if(d<=0 && *(s+d)>k && l<n)
                {
                     *(s+d)=0;
                     l++;
                     goto end;
                }
        }while(d>0 && *(s+d)>k);
 	end: d=l-1;           
	forloop:;
	for(i=0; i<m; i++)
	{
		*(s+d)=i;
		print();
	}
	i=0;
	if(u<o) goto whyloop;
	return 0;
}
void print()
{
	char buff[l+1];
	for(j=0;j<l;j++)
	{
		buff[j]=*(p+*(s+j));
	}
	buff[j+1]=0;
	printf("%s\n",buff);
}
int charlen(char *cher)
{
	i=0;
	while(*(cher+i)!=0)
	{
		i++;
	}
	return i; 
}
int charint(char *cher)
{
	i = charlen(cher);
	i--;
	int j=1;
	int integer=0;

	for(i=i;i>=0;i--)
	{
		switch(*(cher+i))
		{
			case '1': integer+=(1*j); break;
			case '2': integer+=(2*j); break;
			case '3': integer+=(3*j); break;
			case '4': integer+=(4*j); break;
			case '5': integer+=(5*j); break;
			case '6': integer+=(6*j); break;
			case '7': integer+=(7*j); break;
			case '8': integer+=(8*j); break;
			case '9': integer+=(9*j); break;
		}
		j=j*10;
	}
	return integer;
}

G2 update; how C determines length of string

G2 is still being developed, I am currently working on modeling it in Inventor, so I can more easily happybirthdaytome work out the structural layout being that it is a much simpler design than the G1

also, if you wanted to know how C determines how long a char array is (which is just a char pointer with offsets in C)

int charlen(char *foobar)
{
     i=0;
     while(*(foobar+i)!=0)
     {
        i++;
     }
     return i;
}

*edit: updated post format

G2

The G1 project has been suspended due to structural, and financial reasons. We are now working on G2:

Instead of using 2 40mm EDF motors, we are going to use just 1 60mm EDF due to the limited space within the fuselage. Situation is not helped by the gigantic battery that we will be using.

not modeled yet: wings, intake ducts, exhaust opening/duct, V-tail receiver, and servos

Screen Shot 2013-08-02 at 12.37.42 AM Screen Shot 2013-08-02 at 12.37.33 AM Screen Shot 2013-08-02 at 12.37.24 AM Screen Shot 2013-08-02 at 12.37.51 AM

prototype D-4

This is the 4th design that I made. It is not much of a change from D-3 because that is pretty much the design we have decided on. I have cleaned up it’s shape, and designed a new way to hold the cockpit in┬áplace.

Top

Top

bottom

Bottom

Front

Front

Back

Back

Other

SW Low NW Low NW High