I made a MIPS simulator

AKA MIPS – A Java Based MIPS simulator. Browse the code on GitHub.

Long story short, my Data Structures teacher gave us this project where we hasd to use our knowledge of data structures to make something useful. She said we could do it in groups so naturally I found myself in a team of 3.

She told us about the project near the start of the semester and a week before it was due we were still trying to decide what we were gonna make. She said that we would have to present our project and pitch it and convince the audience that it’s a useful piece of shit. The problem with us was that any idea we came up with was either far too advanced and therefore not worth the time and effort or far too simple for our ego to allow us to go about presenting it as our grand project.

I thought maybe we could make like a virtual machine of the Altair or any other primitive computer and that led us to the idea of making a MIPS simulator. We opened the instruction set in a browser tab immediately and were relieved to find that it was sufficiently small and therefore we decided that this was what we wanted to do.

Within 24 hours I had written a buggy but functional parser that could read assembly files coupled with a machine object using a couple of integer arrays for simulating the RAM and registers. Downloading and running some assembly code for MIPS on my parser helped me fix some bugs and typos that had crept in.

Because the teacher had insisted that the project be a graphical program, one of our team members was tasked with creating a GUI for this and so he made a JavaFX project that ended up looking very similar to the MARS simulator for MIPS. No it wasn’t at all a coincidence because he had used MARS before.

After coupling my code with the GUI, I would say we ended up with a pretty decent program. It has a few limitations, e.g. lack of floating-point support. I learned from Steve Wozniak that it’s okay to leave that out when you’re writing your own language processor. On a serious note though, I didn’t initially plan on leaving it out but I learned after it was a bit too late that MIPS had a whole set of 64 bit registers for handling floating-points and I didn’t feel like making any changes to the otherwise working parser. Other than that, there’s no support for unsigned arithmetic. The whole program is Java based and Java doesn’t have unsigned types so neither does the simulator.

The code for the program is available on github. Feel free to check it out, give your feedback and even suggest improvements. It’s not on my teammates account because he created an pushed the code for the GUI on a separate repo and then merged the parser code directly into it.

 

Toggl’s “Building a Horse with Programming” comic explained

If you don’t know what this is about, you should first go and check it out.

So, C++ is, for me at least, an intolerable programming language. Everything from the code to the resultant program tends to be as ugly as it gets. More than half the time it feels like it was hacked-together to include everything. However, it has been around for a very long time and you can use it to do just about anything that you could want to do with a programming language. Only, the experience and the result may not exactly be great.

Then we have Java. The main problem with Java is that for a language that aims to be useful for all sorts of applications on all platforms, it’s missing a lot of features that are commonly used by full-stack developers. Very often, when writing programs in Java, programmers end up spending more time than they need to creating new types and methods so that they can use them in the program that they actually set out to write.

Ever since npm and nodejs gained popularity, JavaScript has become one of the world’s most widely used languages and npm is probably the most used package manager of them all. And the thing about Javascript programmers is that they use a lot of external libraries and packages and every once in a while, they add their own package to the global repository just to get a kick out of it. By now there are probably more packages in npm than there are libraries for any other language out there and when you are a JavaScript developer, you really have use them if you want to get any work done. The pinked “Backbone” and “Angular” are references to Backbone.js and Angular.js, two popular JS frameworks.

NoSQL refers to database systems that don’t use SQL and are non-relational. An example is MongoDB, which stores JSON objects grouped into collections. The joke is that the non-relational model doesn’t always expose enough information for you to be able to access your objects without using the abstract api.

COBOL? Well, I guess no one entirely gets it except for its creator.

Lisp has a lot of parentheses. Just google sample codes for lisp and you’d know.

C# is a fairly complete and tolerable language. The problem? Microsoft. Windows. The thing about the costume is that C# is basically Java in a costume (it closely resembles Java in terms of syntax and semantics.) The camel is basically the windows environment. Basically, it means that C# programs don’t always work as intended if not running on Windows.

Assembly doesn’t really offer a lot of language features. There’s a basic set of operations that have to be used for everything. But coding something in such a low level language means you get control over aspects that most languages abstract over. So you can make a really efficient program, hence, the thing about running.

Everyone hates PHP. They say it’s a terrible and unsafe language and has unpredictable behavior. I personally have reasons to really like PHP but I will still go ahead and tell you that most good posts on the subject target aspects of the language that really do suck so yeah some of the hate is justified.

 

SQL Server screwups and how to fix them.

So recently I installed Visual Studio 2017 on a laptop and I installed it with only the ASP.NET and WinForms features. I was working on a webapp and initializing the database when I realized that VS was completely unable to connect to the database. I opened the “SQL Server Object Explorer” and tried to manually connect to the MSSQLLocalDB instance and I got this error.

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 – Local Database Runtime error occurred. Error occurred during LocalDB instance startup: SQL Server process failed to start.) (Microsoft SQL Server)

I tried to start the service from the CMD and I kept on getting similar errors. Eventually, I concluded that the installation was corrupted and therefore tried reinstalling LocalDB. Guess what? It got me past this error but introduced me to another. It was something like this.

CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file ‘c:\Users\AnasDatabaseName-asdf5sdfasd5fs5dfs5f.mdf’.

This time, the CREATE Database command was failing because it was trying to create and MDF file in my “Users” folder. I checked the default location for the databases and it was in a sub-sub-sub-sub-folder inside AppData. A bit of Googling told me that this was actually a bug in the program itself and had been fixed in a recent update. So I just downloaded the latest Cumulative Update from the Microsoft site and it did the trick.

 

Angular vs React vs Vue.JS

This is the shortest and most effective comparison that aims to make the decision making process easier.

Angular: 

Pros:

  • The oldest and therefore very mature.
  • A complete framework in itself and ideal for large projects.
  • The MEAN stack remains, to date, one of the most popular stacks for web-development so finding jobs is never tough for Angular developers.
  • With NativeScript, you can use it to develop smartphone apps.

Cons:

  • If you don’t know, don’t like or don’t want to learn TypeScript, I would suggest staying away from it because the current releases are TypeScript based.
  • Packed with features, it’s a huge framework and therefore takes a fair amount of time to learn and master.

React:

Pros:

  • Been around for a fair amount of time and therefore can be said to be just as mature as Angular.
  • Smaller than Angular and therefore easier to learn.
  • Just as popular, if not more, and therefore has a well-developed community.
  • Just as many jobs out there for it as for Angular.
  • React Native allows for it to be used to make Android apps.

Cons:

  • You will have to learn JSX. Basically the idea is that in ReactJS, you try to maximize your use of JS and minimize your use of HTML. JSX kinda merges both. Obviously this adds to the learning curve.
  • Not a complete framework. React Developers almost always use Redux or some other flux implementation for state management. Code often becomes a real mess with large projects.

Vue.JS:

Pros:

  • Has just as many features as React, if not more.
  • Small enough for you to be able to learn it in a single day.
  • Great for small projects.
  • Gaining popularity real quick.
  • Resembles the original Angular.JS so if you are familiar with it, you’d love Vue.
  • Unlike React, with Vue, the goal is to maximize the use of HTML and minimize the use of JS.
  • Although not a complete framework, it comes with it’s own router and flux implementation, that, although independent projects, offer seamless integration with Vue itself.

Cons:

  • Relatively new and therefore has a small community and fewer jobs are available.
  • NativeScript has a plugin for Vue support but it not supposed to be ready for development yet. Although, it seemed pretty functional when I tried it.
 

Rewriting Legacy Code

I once wrote an answer on Quora about the improbability of a programming language to completely die out once it has gained popularity. The idea, not so original, was that there are two factors preventing a language from dying:

  1. A significant amount of code is written in it and a significant amount of people or other code depend strongly on it. An example is Linux. Now Linux has always been C and always will be. As long as Linux exists, it would be impossible for C to die out.
  2. Everyone knows about it. The problem with a programming language being popular enough for it’s existence to be common knowledge is that there will always be people who’ll be fond of it and would want it to never die out. The best example would be Richard Eng, the smalltalk evangelist who has grown more popular than the language itself and likes to call himself Mr. Smalltalk.

This post is about the first reason. C, PHP and JavaScript are three of the most popular and most hated languages that are getting closer and closer to being about as old as time itself. For my own reasons, I both like and dislike the 3 and also rely a lot on them. Let’s assume everyone agrees to killing all 3 of them. Now the problem is that C is:

  1. The language Linux is written in.
  2. The language most programmers start with. (Often this is because universities prefer to teach it in the earlier semesters.)
  3. More or less the only mature language that has the least amount of abstraction that one could ask for except for C++, which is a mess and an offspring of C itself and therefore not worth talking about in this post.

… 

 

Comparing strings with "==" operator vs ".equals" method [Java]

During lunch, I was reflecting on that day when my programming teacher asked me to come forward and teach “bitwise” operations to the whole class, and I remembered how, in my attempt to explain to them the basic difference between a regular “OR” and a bitwise “OR”, I had talked about value comparison being different from a bitwise comparison. Then I began to wonder. A bitwise operation on machine level is fairly simple to understand, but what about value comparison? What is it that happens at machine level when you check if two integer values are equal?
As I began my search for the answer, I pinged Vladislav Zorov, my mentor and friend, and asked him. He said that compilers mostly just subtract the memory addresses of the two objects being compared and returns true if the difference is zero i.e. if they are literally the same object. I couldn’t agree with this so I argued against it. I said that it is possible for two variables to point to identical objects without pointing to the same object and so I set out to prove it.
I wrote a very simple program:

import java.util.Scanner;
public class HelloWorld {
public static void main(String []args){
// Created two strings using the same literal and an array with the second index set using that literal.
String x = “abcd”;
String y = “abcd”;
String[] z = {“asafaf”,”abcd”};
// Printing addresses of all 3.
System.out.println(Integer.toHexString(x.hashCode()));
System.out.println(Integer.toHexString(y.hashCode()));
System.out.println(Integer.toHexString(z[1].hashCode()));
// Checking to see if the equality symbol works on these
if(x == z[1] && x==y && y==z[1])
System.out.println(“== works on the 3”);
// Comparing the 3 using the equals method which will, obviously, work.
if(x.equals(z[1]) && x.equals(y) && y.equals(z[1]))
System.out.println(“equals method works on the 3”);
// Creating Scanner object to read from stdin.
Scanner input = new Scanner(System.in);
// Taking two strings as input. Will test with
// 1. Two different strings
// 2. Identical strings
// 3. “abcd” for both
String j = input.next();
String k = input.next();
//printing their addresses
System.out.println(Integer.toHexString(k.hashCode()));
System.out.println(Integer.toHexString(j.hashCode()));
// Testing both comparisons
if(j==k)
System.out.println(“== working on input strings”);
if(k.equals(j))
System.out.println(“equals working on input strings”);
// Initializing a string identical to x,y,z[1] but with the new keyword
String a = new String(“abcd”);
// Printing its address
System.out.println(Integer.toHexString(a.hashCode()));
if(a==x || a==y || a== z[1])
System.out.println(“== working with new keyword”);
if(a.equals(x))
System.out.println(“equals method working with new keyword”);
}
}

If you run it, you’ll see that its output shows that all strings that have the same value point to the same address regardless of how they’re initialized. But the equality symbol only works when two strings have been created with the same literal. The fact that we’ve now proven that identical strings in Java do have the same memory address does perhaps imply that comparison is done simply by comparing addresses but then what about the equality symbol? Well, I’ll update this post when I find out.

 

How GoDaddy robbed me.

Humble request to all readers: Share this post.

I really want to keep this as short as I can and address only the primary issue. For those who don’t know, GoDaddy is one of the most popular domain registrar’s in the market and I started using it a couple of years back as a means for getting rid of my local registrar who had been screwing up routinely in a most inefficient way.
Abstract: Lately, they have not only made a few irrational and immoral decisions, but they also robbed me of a rather large sum of money and then messed up my order completely. In short, I paid thrice for the same order and I didn’t even get what I ordered. Furthermore, somehow the same domain was purchased twice using my account and I have absolutely no idea how it’s even possible. … 

 

What the hell?

So I was minding my own business in my humble abode when I receive an email. A quick look at my phone’s notification bar tells me that it was from the admissions counselor (, let’s call her Daisy,) of a college that had accepted me. I wasn’t at all surprised because the decision day for that college had just passed and I hadn’t shared mine at all.
The email said “Daisy has shared a document with you.” And had a button that said:Open in Docs.
Like a dumbass, I clicked on it. I did wonder why she’d share a document with me and I did, kinda, wonder why it didn’t look like the average Google Doc invitation, but I was on my phone and I chose to overlook it.
Then I land on a page that asks me if I want Google Drive to have access to my email account. It asked for permissions like “Edit, delete and create emails.” I was certain, by this point, that something was not right. And I just allowed it. You know why? Cause the goddamned address bar clearly showed that I was still on Google and still on https. I was redirected and then it appeared to be completing a request and loading something and after waiting for a few minutes, I realized what I had done.
Now I noticed another weird email, and it said “Anas Khan has shared a document with you.” That was it. Now I realized what those permissions were for. I checked the sent mail and realized that this shit had forwarded itself to “hhhhhhhhhhhhhhhh@mailinator.com” and that email was BCC’d to virtually every email address that I had ever communicated with.
Well, kinda screwed up. But I have to say this: It was a clever scam.

 

The Dilemma of Programming Languages

So, I’ve been up and laptop-ing for the last two hours or so, and all I’ve been doing is reading one article after another centered on programming languages. A good fraction of those articles lashed on how Javascript sucks. It’s been a lot of continuous reading, all thanks to catchy links located throughout the articles, and at any given time there have been at least 7 tabs (with articles) open at all times. Even as of now, there are exactly seven other tabs, besides Facebook, open in my browser.
It all started when I, scrolling through my news feed, out of sheer boredom, landed on a post by Richard Eng, an evangelist for SmallTalk whom I first came across on Quora, and while reading, I clicked on some other links and opened them in new tabs, and then the process repeated itself. The more I read, the clearer became Eng’s views on the different languages. It was clear that he hates JavaScript, and just the first part of this post by him shows that he definitely has a point.
While going through his list of his JavaScript alternatives, which are all basically Transpilers, I also discovered Brython, which is basically Python running in JavaScript’s place as the client-side scripting language. It’s pretty cool actually, although apparently the code is transpiled from Python to JS during runtime and that’s gotta take a toll on the performance.
Also, like countless other people, Mr. Smalltalk was kind enough to tell us that PHP is one of the three languages he can’t stand. While I agree that JS deserves to be on that list, my love for and comfort with PHP is too strong. It hurts when people constantly tell me that PHP sucks, and that it shouldn’t be used on any serious projects. Maybe they are right, maybe they aren’t, but I do believe that PHP is going to be around for a long time.
While reading about the desire for improvements to JavaScript, I wondered about what would be the best way to work towards them. They can’t just change everything like Angular did with the 2.0, that could break the web. But if they offer backwards compatibility, it would just keep growing and ultimately become a huge confusing mess.
I guess that it’s fairly safe to say, that with programming languages, it’s important to make the correct decisions during the initial stages of development.
You might be wondering what the point of this article is, or what exactly is the actual “dilemma.” To be honest, there isn’t a point. I’m just thinking out loud. Incoming opinions lead to indecisiveness, which in turn results in time going wasted.