Why I love YOLOv5?

I am a big fan of Nicholas Renotte’s channel on YouTube. I also love computer vision and its combination with deep learning. A few months ago, Nicholas posted this video, which is about YOLOv5. I usually am too lazy to watch videos which are longer than 15 minutes and I watch them in a few episodes. But this video made me sit behind the laptop screen for over an hour and I’m sure I won’t regret it.

So let’s start the article and see where this story begins. As I mentioned earlier, I love computer vision specially when it’s combined with deep learning. I believe it can help us solve very complex problems of our projects with ease. My journey in world of these YOLO models have started almost a year ago, when I wanted to develop a simple object detection for detecting street signs.

Firstly, I found a lot of tutorials on darknet based training but l did not manage to get it to the work, specially since I have a mac, it could be a very realistic nightmare. So I guess YOLOv5 was a miracle. In this article, I am going to explain why I love YOLOv5 and why I prefer it to other YOLO versions.

What is YOLOv5?

According to their github repository, YOLOv5 is a family of deep learning models which is essentially trained on Microsoft’s COCO dataset. This makes it a very very general-purpose object detection tool which is fine for basic research and fun projects.

But I also needed to have my own models because I wanted to develop some domain-specific object detection software. So I realized they also provide a python script which helps you fine-tune and train your own version of YOLOv5.

So I basically fell in love with this new thing I have discovered. In the next sections, I will explain why I love YOLOv5!

Why I love YOLOv5?

Firstly, I invite you to see this chart, which shows the comparison of YOLOv5 with other commonly used object detection models:

And since there’s been a controversy about YOLOv5 claims about training time, inference time, model storage size, etc. I highly recommend you read this article on Roboflow’s blog.

So we can conclude the very first thing which made me happy is the speed and that’s right. The second thing by the way is the fact I am lazy. Yes, I am lazy and I know it.

I always tried to compile darknet and use it for having a YOLOv4 model and make my projects on top of YOLOv4 but when I saw how hard it can get and since I have a mac and I didn’t really want to fire-up an old computer for these projects, I was looking for something which does everything with a bunch of python scripts.

Since I discovered the YOLOv5, I started working with it and the very first project I have done was this pedestrian detection for a self-driving car.

Then, I started doing a lot of research and asking about what I can do with YOLOv5. I find out I can do pretty much anything I want with ease as they provided a lot of stuff themselves. Isn’t that good enough? Fine. Let me show you another youtube video of mine which I solved my crop problem with their internal functions.

If you’re not convinced yet, I have to tell you there is a great method which is called pandas in this family of models.

As the name tells us, it really outputs a pandas dataframe which you can easily use data from that dataframe. Let me set a better example for you. Considering we want to find out which plants are afflicted and which ones are not in a drone footage.

By using this method, we can simply make an algorithm which counts the amount of afflicted ones in a single frame, so we can easily find out how many afflicted plants we have in a certain area. The whole point here is that we have statistically right data for most of our researches.

The other example would be the same as my pedestrian detection system. We can command the car to get data first from the cameras to make sure we’re dealing with pedestrians and second get data from distance measurement system (which can be an Ultrasonic or LiDAR) to make sure when it should send braking command.

Conclusion

Let’s make a conclusion on the whole article. I love YOLOv5 because it made life easier for me, as a computer vision enthusiast. It provided the tools I wanted and honestly, I am really thankful to Ultralytics for this great opportunity they have provided for us.

In general I always prefer easy-to-use tools and YOLOv5 was this for me. I need to focus on the goal I have instead of making a whole object detection algorithm or model from scratch.

I finally can conclude that having a fast, easy-to-use and all-python tool for object detection was what I was always seeking and YOLOv5 was my answer.

I am glad to have you as a reader on my blog and I have to say thank you for the time you’ve spent on my blog reading this article. Stay safe!

A to Z of making an intelligent voice assistant

It was 2011, a sad year for a lot of apple fans (me included) because Steve Jobs, one of original co-founders of Apple Computers died October that year. Also, it could become sadder if there was no iPhone 4S and its features that year.

A few years prior to the first introduction of Siri (which introduced with iPhone 4S), a movie called Iron Man came out from Marvel Studios. Unlike comic books, Jarvis wasn’t an old man in this movie. Jarvis was an A.I. I’m not sure if the movie inspired companies to add the voice assistant to their systems or not, but I’m sure a lot of people just bought those phones or tablets to have their own version of Jarvis!

Long story short, a lot of engineers like me, were under the influence of the MCU (Marvel’s cinematic universe) and Apple and wanted to have their voice assistant a little bit differently! Instead of buying an iPhone 4S, we preferred to start making our own voice assistants.

In this article, I’m discussing the basics you need to learn for making your very own version of Siri. I warn you here, there wil be no codes at least in this one!

How does a voice assistant work?

In order to make something, we first need to learn how on earth that thing works! So, let’s discuss about voice assistants and how they work. They’re much simpler than what you think. It’s guaranteed your mind will be blown by their simplicity!

  • Listening: a voice assistant, as called, needs to listen to the voices and detects what is a decent human voice. For this, we need speech recognition systems. These systems will be discussed further. We just can make one, or we can use one that’s already made.
  • Understanding: In the 2015 movie Avengers: Age of Ultron, Tony Stark (a.k.a Iron Man) says “Jarvis is only a natural language understanding matrix” not considering the matrix part, other part of this sentence makes sense to me. Voice assistants need to understand what we tell them. They can have A.I or hard coded answers or a little bit of both.
  • Responding: after processing what we’ve said, the voice assistant needs to provide the responses that fit our request. For example, you say “Hey Alexa, play music” and your Alexa device will ask you for the title, you say “Back in Black” and she’ll play the song from spotify or youtube music.

Now, we know about the functionality. What about the implementation? It’s a whole other story. The rest of the article, is more about the technical side of making an intelligent chatbot…

Implementation of a Voice Assistant

Speech Recognition

Before we start to make our voice assistant, we have to make sure it can hear. So we need to implement a simple speech recognition system.

Although it’s not really hard to implement a speech recognition system, I personally prefer to go with something which is already made, like Python’s speech recognition library (link). This library sends the audio signal directly to IBM, Microsoft or Google API’s and shows us the transcription of our talk.

In the other hand, we can make our own system with a dataset, which has tons of voices and their transcriptions. But as you may know, you need to make your data diverse af. Why? Let me explain it a little bit better.

When you have your own voice only, your dataset doesn’t have the decent diversity. If you add your girlfriend, sister, brother, co-workers, etc. You still have no diversity. The result may be decent, but it only limits itself to your own voice, or the voices of your family members and friends!

The second problem is that your very own speech recognition, can’t understand that much. Because your words and sentences might be limited to the movie dialogues or books you like. We need the diversity to be everywhere in our dataset.

Is there any solution to this problem? Yes. You can use something like Mozilla’s dataset (link) for your desired language and make a speech recognition system. These data provided by the people around the world and it’s as diverse as possible.

Natural Language Understanding

As I told you, a voice assistant should process what we tell her. The best way of processing is artificial intelligence but we also can do a hard coded proof-of-concept as well.

What does that mean? hard coding in programming means when we want some certain input to have a fixed output, we don’t rely on our logic for that answer, but we just write code like if the input is this, give the user that, with no regard of the logic. In this case, the logic can be A.I, but we tell the machine if user said Hi, you simply say Hi!

But in the real world applications we can’t just go with the A.I. or hard coded functions. A real voice assistant is usually a combination of both. How? When you ask your voice assistant for the price of bitcoin, it’s a hard coded function.

But when you just talk to your voice assistant she’ll may make some answers to you, which may have a human feel and that’s when A.I. comes in.

Responding

Although providing responses can be considered a part of the understanding process, I prefer to talk about the whole thing in a separate section.

A response is usually what the A.I. will tell us, and the question is how that A.I. knows what we mean? and this is an excellent question. Designing the intelligent part of the voice assistant or in general chatbots, is the trickiest part.

The main backbone of responses, is your intention. What is your chatbot for? Is it a college professor assistant or it’s just something that will give you a Stark feeling? Is it designed to flirt with lonely people or it’s designed to help the elderly? There are tons of questions you have to answer before designing your own assistant.

After you asked you those questions, you need to classify what people would say to your bot under different categories. These categories are called intents. Let me explain by example.

You go to a Cafe, the waiter gives you the menu and you see the menu, right? Your intention is now clear. You want some coffee. So, how you ask about coffee? I will say Sir, a cup of espresso please. And that’s this simple. In order to answer all coffee related questions, we need to consider different states, as much as possible. What if customer asks for Macchiato? What if they ask for Mocha? What if they ask for a cookie with their coffee? and this is where A.I. can help.

A.I. is nothing other than making predictions using math. A long time ago, I used to write the whole A.I. logic myself. But later a YouTuber called NeuralNine developed a library called neural intents and it’s for this purpose! How does this library work?

It’s simple. We give the library a bunch of questions and our desired answers. The model we train, can classify questions and then simply predict what category our sayings belong to. Let me show you the example.

When you say a cup of espresso please, the A.I. sees words cup and espresso. What happens then? she’ll know these words belong to the coffee category, so she’ll give you one of those fixed answers from that category.

Keeping answers fixed by the way, is not always a good thing. For some reasons, we may need to make a generative chatbot which also can make responses like a human. Those bots are more complex and require more resources, studies and time.

Final Thoughts

The world of programming is beautiful and vast. When it comes to A.I. it becomes more fun of course. In this article, I tried to explain how a voice assistant can be constructed but I actually didn’t dig deep to the implementation.

Why so? I guess implementation is good, but in most cases, like every other aspect of programming, it’s just putting together some tools. So learning the concept, is much more important in most cases, like this.

I hope the article was useful for you. If it is, please share it with your friends and leave a comment for me. I’d be super thankful.

How to make video games like movies!

It was a long time that I did not write any thing in this blog. Now, I decided to write a topic about “video games” (as I wrote in my Persian blog). I was member of a game development team for about three months and I learned a lot. At least, I know the way they were doing the job was “How to not make a video game”. So, When I left the team, I decided to research about game development process. In this topic, I explain everything I found (experience and research result!)

When I was in the team…

It was in October (2017), a person sent me a message in Telegram, and the message was like this :

Mr. Haghiri, we need a musical composer for our game, we did a search and we found you. Please come here Wednesday 4:00 PM to talk about the project and your role.

Wednesday, I went to their office. That guy greeted me so nicely and started talking to me, about their project. I found the game is a horror game (horror games are popular in Iran, but there’s no “good” horror game “made in Iran”.) and it made me happy! Because it was the first time I heard about an indie team decided to make such a great game. They let me two weeks to research about “Sound and Music” in Unity Engine and I did it. I composed two pieces and also I tried to learn some tools for mixing and mastering sounds in the game (But it wasn’t actually my role, I just did that as a sample!).

After two and half months, they said “Mr. Haghiri, you don’t do what we wanted you to do”, anyway, they haven’t paid me even a “rial” in those days and expected me do great music composition, and they also wanted me to do what wasn’t my actual role. And this was not a good experience actually. But in those two months, I learned Unity game engine and I also met other “game developers”.  And, I decided to publish my experience on my blog.

Why movies?

Recently, I read a book called Making Short Films : Complete Guide from Script to Screen, by Clifford Thurlow. In the book, I found great names like “Salvador Dali” or “Charlie Chaplin”, and also great movies and books also mentioned in the book, like “One Flew Over Cuckoo’s Nest”. Everything was perfect, In I think about process of making a video game! It’s so similar to process of making a movie!

But I realize game and movie have lots of differences. The biggest difference is that games are interactive and players interact with the environment or other characters, but movies are not. Anyway, the main procedure – I mean “writing” – is %90 the same! So, I decided to mention some movies I watched, then tell you how I make a video game like them!

Great movies gave me ideas!

An Andalusian Dog

Movie is written by Louis Bunuel and Salvador Dali. I think these names are enough. But, after I watched the movie (it’s now available on YouTube and other video-sharing platforms, you can easily find and watch it), I discovered “product of a melancholic and depressed mind”. And both “Melancholia” and “Depression” are good subjects for a story or game.

Phantom of The Opera (Musical)

The book is about a musician, in a better word, a “genius” and smart person who hided himself. Andrew Lloyd Webber, just made that sad and creepy story to a romantic story by his music! First time I watched the movie, I could not understand it well. Because it’s not following the book’s story, and I’m not a native English speaker! I watched that movie 4 times. I watched the live performance 2 or 3 times and finally, I got the concept.

It has “Misanthropy” and “Romance” at once. I think these two things are also good for people who want to make video games (Please go and check “INSIDE” by Playdead! It’s the Misanthropy! Pure Misanthropy!)

School of Rock

It’s a bit different, School of Rock is comedy, and it’s also attractive for children. Because the topic is about a guy who teaches a bunch of 4th grade children to ROCK! Yes, he teaches them how to play electric guitars, bass, drums and keys. I think the concept of “Music” can be a good idea, too! Specially if you plan for a game like “Guitar Hero”.

Only Lovers Left Alive

If you like Vampires, please watch this movie. This movie has no “teenager” content, but it still tells stories about two vampires who married for centuries. Movie is directed by Jim Jarmusch and he also made music for his movie, in his rock trio SQRL. This movie has two things for game developers. It’s and independent movie and it can be a good idea bank for indie developers and also, it has “Romance” and “Fear” and “History”. All of these three factors can make a game great!

Ok, I talked a lot about movies! Let’s find “how can I make a game like a movie?”

Finally, let’s make a game like a movie!

The most basic thing you must have for a movie is “plot”. Plot is the main idea, it’s developed and tells people your concept, but it’s not a completed “script”. Writing plot in both games and movies is the same. To write a good plot, you need to study and read books, scripts and other plots. I prefer printed (and Persian) books in this case.

You will need research on the topic you want to write a plot about. For example, if you want to write about a middle eastern civilization (for example : Sassanid Empire or Ottoman Empire), you have to read history of Iran, Turkey, Afghanistan, Iraq, etc. If you want to write a plot about Satan, you have to search about Satan’s role in Judaism, Christianity and Islam, and find which belief is closer to what you want. So, you need to have a background.

After you wrote the plot, you have to write the script. But, writing script is different here, you have to clarify where and when the scene is interactive and when and where is not. The best example, is “Bioshock Infinite”. You know why? Because when you’re not interacting with the environment, you still can move camera and see what happens around you. I’m not a good script writer (But I try to be!) and I will write a post about how to write script from plot, when I manage to do that.

After you wrote the script, you have to make your basic ideas in the engine. Please! Please! Please! Call an experienced game developer before doing that, because the experienced one can help you find experienced character/concept and environment artist and game-play designer. After that, I can say you’re ready to start making your game! With a good team, you can make a good game!

Finally, I wrote this article but it wouldn’t be the last article about games in my English blog. I try to continue this, because I even couldn’t find good articles about being “game script writer” or “game director” even in English! I hope you like my post 🙂

 

 

How to be a hardware engineer

A computer, is made up of hardware and software. Lots of people like to write and develop software, so the internet is full of topics like “How to be a software engineer” or “how to write a computer program” and also, there is a lot of great and free tutorials on programming languages and frameworks. But, hardware engineering guides on the internet, are not as many as software’s.

In this post, I really want to write about “How to” be “a hardware engineer”. As it’s my favorite field in computer science and I study it in the university. So, if you want to be a computer hardware engineer, just read this and start searching about your favorites.

Definition

A computer engineer in general, is an engineer who designs, develops and tests computers. Both hardware and software. But, there are different types of “Computer engineer”. For example, I am a hardware engineer, my friend is software engineer, my other friend is network engineer, etc.

So, a hardware engineer is a computer engineer, who knows electronic and electrical engineering (I know, some of electronics/electrical engineering courses are taught to students of software engineering). But, if we go further, we’ll find that the “Hardware Engineer” is who “combines computer science and electronics”. So, this is what he/she does, learns computer science and electronics, then combines them and makes computer hardware.

Expertise and Fields

There are these fields in hardware engineering, and people can learn one of them to find a job or start their own business.

  1. Computer architecture :
    a computer architect, is a computer scientist who knows how to design logical level of a computer. In my opinion it’s the most-needed role in a team who design a new computer. When you study computer architecture, you learn mathematics behind the computer hardware, and also you learn how to design different logical circuits, and connect them together and make a computer at the logical level.
    So, if you love mathematics, and love computer programming and in general, computer science, this is your field. to learn more, you also can read my book (link)
  2. Digital Electronics :
    This one, is actually my favorite. Digital electronics is about implementation of logical designs. So, with digital electronics, you can make what you have designed using logic and mathematics.
    But for this field of hardware engineering, you need to know electronics and electrical engineering. So, if you like digital electronics, start studies on electrical engineering today!
  3. Digital Design :
    This part, is also my favorite, and I really like it more than digital electronics. It’s about simulation and synthesis, so you can really ‘feel’ the hardware you have designed.
    But how is it possible? The “Digital Designer” is a person who’s familiar with simulation and synthesis tools. Such as hSpice, LTSpice, ModelSim and languages such as Verilog or VHDL. It’s about design and programming, so if you’re a software engineer who wants to learn hardware engineering, this field is yours! But, usually hardware engineers learn this field after learning digital electronics.
    If you like to be a digital designer, start improving your programming and algorithm design today, it’s important to know how to code, and how to write an algorithm.
  4. Microcontrollers and Programmable devices :
    This field is not actually “Hardware Engineering”. But, it’s still dealing with hardware. Actually, it’s not “design and implementation” of the hardware, it’s “making hardware usable”. When you program an ATMega32 chip for example, you make a piece of silicon usable for driving motors and sensors.
  5. Artificial Intelligence :
    And finally, the common field! Artificial intelligence is the widest field in computer science. It’s applicable and functional in all fields of computer science.
    But how we can use it in hardware engineering? We can use AI to improve our hardware, for example our architectures can be analyzed by AI. We can make a hardware which has AI to solve problems (a.g. ZISC processors). And finally, we can use AI in Signal processing, which is usually referred as a field of hardware/electronics/audio engineering.

Start point

I tried to explain start points in previous part, but now, I explain general start point. To learn hardware engineering, you need to know discrete mathematics and logic. Also, you need to know how to show boolean functions like electrical circuits (a.k.a logical circuits). After learning these, you need to get familiar with computer architecture, and at the end, you can choose your favorite field. I have experiences in all fields (except AI and Signal processing) but my most favorites are digital design and digital electronics.

It all depends on you, to choose which field, but you need those basics and fundamentals to understand it. Also, if you improve your knowledge about computer science in general, you will be a successful computer engineer!

Good Luck 🙂

 

Importance of IoT

For the last 25 years, a ghost is flying over our world, it’s the ghost of “Internet”. From 80’s, internet became one of the most used tools for international communications, such as knowledge sharing. So, a lot of people started using internet to share knowledge, to do research or even to have fun. We can use internet in many different ways and this is why internet is important. You know, humans can’t live without air and water, but actually, electricity and internet are as important as them!

In 1982, a truck connected to the internet, it was the first attempt to connect non-digital things to the internet. Years after that, someone asked people of the world to call “connecting non-digital things to the internet”, Internet of Things or IoT for short. Actually a computer or a mobile phone is designed to be connected to internet, so we can’t call that IoT. But a wheelchair, a cigarette lighter, a coffee maker or even an oven are not designed to be connected to internet, so if we connect them to the internet, it’s IoT!

Concepts and Applications

The concept of IoT is to make everything around us online. You can control your coffee maker from work, and when you arrive home, you can have a cup of warm coffee. But, This is not the only concepts. It comes to make our lives safe, it comes to make our things accessible from everywhere. And about applications? As I told, the online-coffee maker is one of the best examples of IoT applications.

The main application of IoT is controlling everything remotely. Before IoT, remote controls used infrared to control lamps, outlets, TV’s, etc. But today, thanks to IoT, we can do that from longer distances, using the internet. For example, your home is in Iran, and you are in a vacation in Armenia, but you can control everything from Armenia! Everything you need is a reliable internet connection.

The importance

Now, let’s talk about the importance. Why IoT is important? is it anything except connecting devices like ovens and coffee makers to the internet? of course it is! It is actually the future of the hardware engineering and even the online world. Hardware engineers like me should learn IoT, because in near future, every single home accessory will become online, every streets of cities will be online, all farms will be online!

So, we need to learn IoT, but how? learning IoT is easy. You only need to know how to program microcontrollers, how to setup a development board (at all, hardware engineering), how to connect your board to the network and how to implement remote controls and connections. For example, you can use a NodeMCU board and a bunch of LED’s to make a bedside lamp, it’s even easier than writing a simple computer program for daily accounting.

Communication with other fields of computer science

IoT can’t be a complete knowledge/technology alone. All IoT experts have to know about one of fields of computer science. For example, a person who designed a system for traffic control, needs to store data from traffic lights or cameras, and then he/she needs to do data mining on that. Because this thing can be helpful in predictions about traffic. Or, if you design that simple bedside lamp, you have to write at least a good single-page interface for it. These are not basic things, and you can’t do all of them alone. To be honest, an IoT team is made up of some professionals/experts. What they do is cooperating about these issues.

Good Luck 🙂

Composing using relative scales!

In this topic, I’m going to show you how to compose a minimal music piece using “relative” scales. But first, let’s talk about music theory. A relative scale, is simply “a scale with the same note as main scale”, there are two kinds of relatives, relative minor and relative major. So, if we consider this as D minor :

D – E – F – G – A – A# – C – D

We need another scale with the same notes. Let’s start from 3rd note of our main scale :

F – G – A – A# – C – D – E – F

This is “F Major” scale, and it’s “relative major” of our D minor scale. As you can see, if you start from 3rd note of a minor scale, you will have its relative major, and if you start from 6th note of a major scale, you’ll have its “relative minor”.

Now, let’s make some piece! In this topic, I use Ableton Live 9 as my DAW, and DSK Overture and ZynAddSubFX as my plugins. Let’s make music!

First, I play an orchestral piece in D minor using DSK overture, with “Strings section”, “Flute”, “Violin” and “Cello”. It will be D-F-A-F-D-A-E.

Sounds good, but It needs some decorations! We can do that using F major! in F major section, I want to use ZynAddSubFX. And my notes will be F – A- C – A – F – C – G.  So, this is our relative major :

Now, we can mix them! This is the orchestral piece with its electronic relative major :

And, I repeat this piece for 4 or 5 times, then make a song (with some audio engineering ), here is our final song :

I hope you enjoy this topic and song! Remember that this “relative” technique is useful when you want to use Ostinatos, specially on piano, you can play the ostinato on right hand and its relative on left hand.

Ambient Music, Using usual instruments

In previous article about music,  I just explained what ambient music is, and we learned that this genre, is famous because its minimal, relaxing and unusual form.  But, in this article, I’m going to explain how we can use a usual instrument and classical music technique, to make this kind of music.

Before we start, I’m going to explain one important thing, ambient is a minimalistic genre of music, so we have to keep it as simple as we can. minimalism means we have to use only maximum three chords, and repeat shapes and melodies for a while. Minimalism is the most important characteristic of ambient music!

Now, let’s discuss about our goal : making unusual music with a usual instrument. I choose piano in this case, because I have a lot of good piano sounds in my laptop, I personally play guitar, but I have no good guitar sound (VST or soundfont or … ). I chose piano, you know, piano is used to be one of the most known instrument in classical music. Also, people used this instrument to play Jazz, Rock, Symphonic Metal, etc. You only need to install “Piano tiles” game and play it, to realize how people used piano in different genres and styles! So, when people used piano even in rap music, we also can use it in our modern and minimalistic style!

Let’s talk about technique, in this case, I want to use Ostinato , which means playing a melodic shape repeatedly for a while. And if you have read this article carefully, you will realize that ambient (and any other minimalistic style of music) has this characteristic. And again, we can find that every musical genre, even these modern, minimal and unusual ones, have roots in the classical music.

As an example, I’m going to write a simple song. First, we need to decide about progression. I want to use 1-3-5 in key of D minor, which means I need these chords :
Dm – F – Am
So, we need to play these chords, each bar (or measure, I really don’t know what it’s called in different parts of the world :D) , includes one chord. This can be our sample :

So, We just played chords, it makes no sense at all. If you play piano (or any other classical instrument) you know when we want to play a piece, we need to decorate our main line. So, I add some decorations to my piece, which will be my ositnato :

And for now, I just play this piece for 5 times, then I do some sound engineering (adding effects, etc) and what you will hear is :

Yes, this is a simple ambient song! A simple ostinato, with reverb and pitch shifting, fades in and then fades out. I used LMMS for writing song, and playing it on a piano, and I also used Audacity for sound engineering.

I try to write more about ambient music in my blog, because I know it’s one of the most popular musical styles, and I know a lot of people who want to play this kind of music, but they don’t know how to start.

Good Luck!

Reverse engineering of 8086, from a calculator to the most used processor

If you have a laptop or desktop computer, you probably use a 8086-based CPU, or one implementation of x86 family to be exact. For example, I have a Lenovo laptop with a Core i5 CPU, which is based on x86 architecture. In this article, I want to talk about x86 architecture, and to explain how it works, I just start with the simplest one : 8086.

8086, is probably the first general-purpose processor made by Intel. This is why it’s famous, and in a lot of cases, people prefer to use it or study it. Everything is well-documented and also there are billions of tutorials and examples on how to use it! For example, if you search for “Interfacing Circuits”, you will find a lot of 8086-based computers made by people, connected to interface devices such as monitors, keyboards or mice.

Before we start reverse engineering, and make our simple x86-compatible computer, let’s take a look on the machine code structure of 8086. In this case, we just review Register addressing mode, because this mode is easier to understand or re-implement.

In this case, we can only take a look on a two-byte (or 16 bit) instruction code. Our instruction code looks like this :

Byte 1 Byte 0
|Opcode|D|W| |MOD|REG|R/M|

What are these? and why we should learn this? As we want to reverse engineer 8086 architecture and learn how it works, we need to know how this processor can understand programs! So, let’s check what are those fields in these bytes :

  • Opcode : a 6-bit number, which determines about operation (for example ADD, SUB, MOV, etc. )
  • D : Determines source or destination operand. To make reverse engineering process simple, we consider that as constant 1. So, REG field in byte 0 is always destination.
  • W : Determines data size, and like D, to make reverse engineering process simple, we consider it as a constant 1. So, we only can do operations on 16 bit numbers.
  • MOD : Determines mode, as we decided before, we only model the register addressing mode, so we need to consider mode as constant 11.
  • REG and R/M : REG shows us source, R/M shows us destination. Please pay attention, we made this special case because we are going to model register addressing mode. For other modes, we can’t consider R/M as destination.

Now, we learned how 8086 can understand programs, for now, we have some instruction code like this :

Opcode D W MOD REG R/M
xxxxxx 1 1 11 xxx xxx

Let’s assign codes to our registers. As we decided to simplify our reverse engineering process, and also we decided to use only 16 bit registers, I prefer to model four main registers, AX, BX, CX and DX. This table shows us codes :

Code Register
000 AX
001 CX
010 DX
011 BX

As you can see, now we are able to convert instructions to machine code. To make reverse engineering process even simpler, we can ignore “MOV”, but I prefer to include MOV in my list. So, let’s find opcodes for MOV, ADD and SUB.

Opcode Operation
100010 MOV
000001 ADD
010101 SUB

Now, we can convert every 8086 assembly instruction using the format we have. For example this piece of code :

MOV AX, CX 
MOV BX, DX
ADD AX, BX

Now, if we want to convert this piece of code to machine code, we have to use the tables we made. Now, I can tell you these codes will be :

1000101111000001
=> 0x8bc1

1000101111011010
=> 0x8bda

0000011111000011
=> 0x07c3

Now, we can model our very simple x86-based computer. But a note to mention, this has a lot of bugs! For example, we can’t initialize registers, so we need to study and implement immediate addressing mode, also, we can’t read anything from memory (x86 is not a load/store architecture, and it lets people work with data stored in memory directly!). But I think, this can be helpful if you want to study this popular processor or do any projects with it.

How to make a computer program

This is a cliché in IT and computer related blogs. You can find at least one topic on How to make a computer program in every blog written by a computer expert (scientist, engineer or experimental expert). So, I also decided to write about it. In this topic, I’m going to explain how your idea can be a program.

I’m not a startup or business person, and I hate when someone wants to teach other people how to have an idea so I consider you already have an idea, and you want to implement your idea as a computer program. Let’s start!

Choose your target hardware

Unfortunately, a lot of programmers ignore this important step, but if you consider a special hardware to develop and implement your ideas, you will have two points :

  1. You learn a new hardware architecture (and maybe organization)
  2. You help someone who wants a specific application on that hardware.

Sometimes, you realize that writing a calculator is pretty stupid. Of course it is when you write a calculator for Windows or macOS. But, when you write a calculator for Arduino, which can interact with a keypad and displays, it’s not.

Write Algorithm

Actually, algorithm is explaining the way we solve the problems. So, we need to write the steps of our solution and test it. Sometimes, when we write a simple algorithm, it’s not efficient at all, and needs a lot of improvements. Imagine this (This algorithm makes all even numbers lesser than 100:

while( a < 100){
 if(a%2 == 0){
  puts(a);
 }
 a++;
}

This is a piece of larger code. But wait,  how can we improve that? That if there can make this piece of code slower. But, if we consider a = 0, we can write something like this :

for(int a = 0; a < 100; a + 2){
 puts(a);
}

You know, I wrote a shorter code here. Also, this short code has a better structure of making all even numbers lesser than 100.  But I think both of these codes have the same time complexity, so there is no difference. If we had two nested loops, and the inner loop’s condition had effects on the outer loop’s condition, we had to spend time on calculating time complexity and optimizing it.

Finally, you will realize there are some “classic” algorithms, which are already optimized, and you can just use them, and model your idea with them.

Choose the language

This step is also one of the most important ones. Imagine if you want to program an AVR chip, of course JavaScript is not the best choice. There are tools which allow you to write programs for those chips in JS, but the language is not made for communication with AVR! But, when you want to program a website, specially when you’re dealing with front-end stuff, C is not your best choice! But wait, if the program we want to make is a general purpose desktop program or a school/university project, we are actually free to choose the language!

Imagine we want to write a simple program, which does Addition with bitwise operations. We can write our program in C/C++ like this :

#include <stdio.h>

int bitwiseAdd(int x, int y){
 while(y != 0) {
  int carry = x & y;
  x = x ^ y;
  y = carry << 1; 
 }
 return x;
}

int main(){
 printf("%d\n", bitwiseAdd(10 , 5));
 return 0;
}

And you can write it in Ruby like this :

def bitwiseAdd(x, y)
 while y != 0
  carry = x & y
  x = x ^ y
  y = carry << 1
 end
 return x
end

puts bitwiseAdd(10, 15)

But, when you want to directly communicate with hardware, you’ll need a low-level language. C/C++ are actually mid-level languages. They can help you communicate with hardware (like this piece of AVR code) :

while(1){
 PORTC.1 = 0;
 delay_ms(1000);
 PORTC.1 = 1;
 delay_ms(1000);
}

or like that bitwiseAdd(x, y) function , they can help us write normal programs. But Assembly language is a really low-level language. We can use it when we need to talk to our hardware directly.

You see, all programming languages can help us, but depending on the conditions, we can use different languages.

And …?

And now, you probably know how a computer program is made. But, if you really want to become a developer, you have to study about paradigms, methodologies, etc. I tried to keep it simple in this article, but later, I’ll write about those topics more.

A very short introduction to Ambient Music

In my Persian blog, I had written a lot about operating systems, computer architecture and digital electronics. I have plans for English blog and I’ll write about computer science and engineering in future, but, I decided to explain my experiences in music for now. In this article, I’m going to talk about ambient music, and how it’s produced. Of course, this is not a music theory or musical software tutorial.

Let’s talk about ambient music, what is ambient music? Ambient music is a minimalistic, modern and electronic genre, which is invented by “Brian Eno” in early 70’s. Ambient is actually a subgenre of electronic music, but after years of evolution, it’s known as an independent musical genre.

Characteristics

Ambient music, is highly dependent on the environment. You will realize this when you hear the name. Actually, this genre is based on John Cage’s theory, Everything we do is music and his 4’33” is one of the best ambient songs ever! Four minutes and thirty three seconds of silence, the composer asks you to listen to ambient noises. It means, you can record any sounds and then make it ambient music. This is true, but, not every sounds. A lot of ambient tracks are just recordings from nature, and a simple melody is played over that sounds. Some others are electronic productions, based on natural sounds and atmospheres. This means, ambient is some kind of avant-garde music, you are free to do everything you want!

Styles

You know every musical genre, can be played in different styles. In this section, I just explain some of ambient music styles. I’m sure there are more styles, but these styles are my favorites :

  • Dark Ambient :
    This is one of the most known styles of the ambient music. Sometimes people think that dark ambient is a subgenre of ambient music, but it’s actually not. Because it’s the same concept, but with scary, depressing or dark atmosphere. Sounds like someone plays his/her music in an abandoned and haunted place 😀
  • Space Ambient :
    This is another style. If you’re a fan of outer space life, science fictions and movies like Star Wars or Star Trek, this is your kind of music. In this style, musicians use effects which can make you feel aliens are in your home! And this is what makes this style awesome!

There are more, but I usually listen to these styles. So, I can explain these two better. For more information, you can find ambient musicians on YouTube, Soundcloud, Jamendo, etc. And ask them about their style!

Subgenres

And now, we are going to take a look on subgenres of ambient music. These genres are created to show us how minimal music can be perfect!

  • Drone :
    My most favorite subgenre of ambient music, drone music is just sustained chord, note or sound. Also, artists may decorated the sustained sounds using small melodies, or a single melody is repeated on the drone sound. I’ll explain drone music in future.
  • Lowercase :
    This is the most artistic form of ambient music. Artists record sounds from nature, or daily activities, and then, amplify them and edit them to make a melody. Lowercase music is one of the most minimalistic genres, and one of the most amazing ones, too!

In this article, we talked about ambient music and which kind of music we can call ambient. In future, I’ll explain more about making an ambient track and I’ll introduce my favorite ambient artists.

Good luck!