Friday, July 15, 2011

Surviving the Y2K’s with 1950’s Pizzazz: Let's "Cut the Gas" People!

Surviving the Y2K’s with 1950’s Pizzazz: Let's "Cut the Gas" People!: "Feel some words coming out? Put food in your mouth. Something that people back in the day used to do was Be Quiet . There were the Loud M..."

Wednesday, May 19, 2010

A simple Compilable Programming Language using Python and Assembler

I don't really blog much, but I thought I'd pass on what I did today and last night to try and under stand how languages like c/c++ and even some higher level languages like python and php work. To reproduce what I have done here, you're going to either need the following (or the equivalent and some smarts)
  1. Ubuntu 9.10 (or some other flavor)
  2. Python
  3. ac and ld (from the binutils packages)
  4. a text editor, and some patience
First I wanted to compile a helloworld program in assembler, just to insure that I had a capable system to compile, link and execute it.

Enter the following code into your text editor (preferably vi or nano). I found this sample program from Linux Experiences. You can check out his blog, it is was what this entire blog and project was based on.

Start your text editor, and copy everything between the cut markers below into it, and save it as helloworld.s.

filename: helloworld.s
-=[copy everything below this line]=-
.text
.global _start
_start:
movl $len,%edx # third argument: message length
movl $msg,%ecx # second argument: pointer to message to write
movl $1,%ebx # first argument: file handle (stdout)
movl $4,%eax # system call number (sys_write)
int $0x80 # call kernel

# and exit
movl $0,%ebx # first argument: exit code
movl $1,%eax # system call number (sys_exit)
int $0x80 # call kernel

.data # section declaration
msg:
.ascii "Hello, world!\n" # our dear string
len = . - msg # length of our dear string

-=[copy everything above this line]=-

Once you have the above saved into a file, you are going to want to compile it. To do this, just type the following into your shell...

$ as helloworld.s -o helloworld.o

Hopefully everything went well, and you now have an object file, which you now need to link and convert into an executable file. To do this, type the following into your shell...

$ ld helloworld.o -o helloworld

You should now be able to run your first program! Go ahead, and run it...

$ ./helloworld
Hello, world!
$

That's it! I was happy I could compile and run it, but I wanted more...

What I figured I could do, is create a simple python program that would parse a simple text file with minimal instructions or commands, and then generate assembler code that I could then compile, creating my own mock "programming language" that could then be compiled into machine code.

So, I created this python program... copy and paste it into 'newlang.py'. I actually hard coded in the program file it will read, as I was too lazy to actually figure out how to pass in a program. Ideally, one would rewrite this into c and create a proper parser for your language using Bison, etc.

filename: newlang.py
-=[copy everything below this line]=-
import fileinput

staticdata = []

filename = "prog1.lc"

# open program file
f = open(filename, 'r')

# loop through program file
for line in f:
# skip comments
if (line.startswith('#')):
pass

# handle echo
if (line.startswith('echo ')):
staticdata.append(line[6:-2])

f.close()

# open our new asm output file
o = open(filename+".as", 'w')

# add text block
o.write(".text\n")
o.write(" .global _start # tell it where our main function is\n")
o.write("\n")
o.write("_start: # this is the main function\n")

counter = 0
for vardata in staticdata:
o.write(" # echo static data to stdout\n")
o.write(" movl $len"+str(counter)+",%edx # move var length into register\n")
o.write(" movl $dat"+str(counter)+",%ecx # move pointer to data in register\n")
o.write(" movl $1,%ebx # move file handle to stdout into register \n")
o.write(" movl $4,%eax # instruct kernel to use sys_write\n")
o.write(" int $0x80 # call the kernel\n")
o.write("\n")
counter = counter + 1

o.write("# exit\n")
o.write(" movl $0,%ebx #\n")
o.write(" movl $1,%eax #\n")
o.write(" int $0x80; # call the kernel\n")
o.write("\n")

counter = 0
o.write(".data # start the data block\n")
for vardata in staticdata:
o.write(" dat"+str(counter)+":\n")
o.write(" .ascii \""+vardata+"\\n\" # a string \n")
o.write(" len"+str(counter)+" = . - dat"+str(counter)+" # string length\n")
o.write("\n")
counter = counter + 1

o.close()
-=[copy everything above this line]=-

Well, now that we have a very simple (and fragile) program to read in programs in our new language, we just need a program to read in to compile! I started, and ended with the following program, which I used the file extension ".lc" for no reason at all except to differentiate from all of the other files in the directory.

The language is simple, but very picky as I did not take the time to insure proper program structure, etc. I do not handle inline remarks, nor do I handle the fact that one could forget to close quotes, or leave them out completely.

So here it is...

filename: prog1.lc
-=[copy everything below this line]=-
# this is a comment
echo "Hello Hacker News!"
echo "Hello Reddit!"
echo "Look ma! No hands!"
-=[copy everything above this line]=-

You'll notice, that I was careful to close all quotes, and insure there was no trailing spaces!!

Ok, so I wanted to automate this a little more, so it seemed streamlined... so I created this shell file...

filename: compile.sh
-=[copy everything below this line]=-
#!/bin/sh
rm prog1
rm prog1.lc.o
rm prog1.lc.as
python newlang.py
as prog1.lc.as -o prog1.lc.o
ld prog1.lc.o -o prog1
-=[copy everything above this line]=-

One more thing to do...
$ chmod +x compile.sh

Now, what we have been wating for... compile your program!!!
$ ./compile.sh

If everything went well, you will have an assembler version of your program, an object file, and an executable... run it!

$ ./prog1

I am sure with some time, love and patience, one could make this into something cool!

Tuesday, April 21, 2009

Zynga's Texas Hold'em Poker - (The one thats on Facebook.)

Well, I wasted 4 hours of my life creating a program in c# that would read the current table status for Zynga's Hold'em Poker (the facebook one). Works great, and if I were to add a poker hand stats algorithm, I think it would be a pretty good bot!

Anyhow you can check it out here...

http://www.blue74.com/zynga_holdem.zip

It works as of April 2009.

Saturday, March 3, 2007

Facebook...

Well, I signed up to Facebook a few months ago... and forgot all about it. My wife checked it out, joined, and got me hooked. You should check it out too, its classmates on speed, and free of course!

My profile is here: http://www.facebook.com/profile.php?id=535605429

Pop by and say hi! :)

Mike

Wednesday, January 3, 2007

The BSA and CAAST regarding my EUA/AUP habbits

Yes, I know better, and everyone does it from 'time-to-time' however, I live in Canada and enjoy a little more freedom then down there in the USA, or at least I like to think so...

I just received an email from my ISP (see below) regarding my internet usage and what I have been downloading... I fear I have been molested by the American Law System.

Thanks Mr. John R. Wolfe from Washington, DC USA

Best Regards,
Your Canadian FAN

P.S - Can anyone tell me where I can find a working copy of VISTA?

---[CUT]---

Dear XXXXXXXXXXXXXX

Rogers Cable (Rogers) has received a notice stating that activities associated with your IP address are infringing copyright in material(s) owned or exclusively licensed by others.

The full notice is appended to this e-mail below.

Under the Rogers Yahoo! Hi-Speed Internet End User Agreement (EUA) and Acceptable Use Policy (AUP), you are prohibited from using the Rogers Yahoo! Hi-Speed Internet service to engage in illegal activities, including activities that infringe copyright. Copies of our EUA and AUP are available at:

http://help.yahoo.com/rogers

Where there has been a violation of our EUA and/or AUP, including the unauthorized distribution of copyright-protected material, Rogers has the right to take appropriate action against you.

If you have any questions about the attached copyright notice, please contact the sender of the notice using the contact information provided in the notice. Please do not reply to this e-mail.

We trust you will comply with our policies and all applicable laws in using the Rogers Yahoo! Hi-Speed Internet service.

Sincerely,

EUA Management Team
Rogers Yahoo Hi-Speed Internet

http://na.edit.client.yahoo.com/rogers/show_static?.form=terms



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

2 Jan 2007 23:11:32 GMT

Rogers Cable


RE: Unauthorized Distribution of the following copyrighted computer program(s):

Dear Sir/Madam:

The Business Software Alliance (BSA) and The Canadian Alliance Against Software Theft (CAAST) has determined that the above connection, which appears to be using an Internet account under your control , is using a P2P network seen below to offer unlicensed copies of copyrighted computer programs published by the BSA's and CAAST's member companies.

Evidentiary Information:
Notice ID: XXXXXX
Asset: Macromedia Studio
Protocol: BitTorrent
IP Address: XXXXXXXXXXXXXXX
DNS: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.net.cable.rogers.com
File Name: Macromedia Studio MX 2004 with Flash Professional MX.ISO/Macromedia Studio MX 2004 with Flash Professional MX.ISO
File Size: xxxxxxxxx
Timestamp: xx Dec 2006 xxxxxxxxx GMT
Last Seen Date: xx Dec 2006 xxxxxxxxx GMT
URL:
Username (if available):



The above computer program(s) is/are being made available for copying, through downloading, at the above location without authorization from the copyright owner(s).

Based upon BSA's and CAAST's representation of the copyright owners in anti-piracy matters, we have a good faith belief that none of the materials or activities listed above have been authorized by the rightholders, their agents, or the law. BSA and CAAST represent that the information in this notification is accurate and states, under penalty of perjury, that it is authorized to act in this matter on behalf of the copyright owners listed above.

We hereby give notice of these activities to you and request that you take expeditious action to remove or disable access to the materials described above, and thereby prevent the illegal reproduction and distribution of pirated software via your company's network. As you know, illegal on-line activities can result in 50 million people on the Internet accessing and downloading a copyrighted product worldwide without authorization - a highly damaging activity for the copyright holder.

We appreciate your cooperation in this matter. Please advise us regarding what actions you take.

Please include the following Notice ID in any response you send: xxxxxxxxxxxxxxx

Please respond indicating the actions you have taken to resolve this matter. The provided link has been assigned to this matter http://webreply.baytsp.com/webreply/webreply.jsp?customerid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

For email correspondence, please reference the above Notice ID in the subject line mailto:bsa@copyright-compliance.com?subject=RE%3A%20Copyright%20Infringement%20Notice%20ID%3A%xxxxxxxxxxxxxxxxxx

Yours sincerely,


John R. Wolfe
Manager of Investigations
Business Software Alliance
1150 18th St NW Suite 700
Washington, DC 20036
http://www.bsa.org
http://www.caast.org
E-mail: bsa@copyright-compliance.com
1-800-263-9700

- ---Start ACNS XML


---[CUT]---