{"id":44,"date":"2017-04-22T23:32:20","date_gmt":"2017-04-22T20:02:20","guid":{"rendered":"http:\/\/haghiri75.com\/en\/?p=44"},"modified":"2017-04-22T23:32:20","modified_gmt":"2017-04-22T20:02:20","slug":"reverse-engineering-of-8086-from-a-calculator-to-the-most-used-processor","status":"publish","type":"post","link":"https:\/\/haghiri75.com\/en\/reverse-engineering-of-8086-from-a-calculator-to-the-most-used-processor\/","title":{"rendered":"Reverse engineering of 8086, from a calculator to the most used processor"},"content":{"rendered":"<p>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.<\/p>\n<p>8086, is probably the first general-purpose processor made by Intel. This is why it&#8217;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 &#8220;Interfacing Circuits&#8221;, you will find a lot of 8086-based computers made by people, connected to interface devices such as monitors, keyboards or mice.<\/p>\n<p>Before we start reverse engineering, and make our simple x86-compatible computer, let&#8217;s take a look on the machine code structure of 8086. In this case, we just review <em><strong>Register <\/strong><\/em>addressing mode, because this mode is easier to understand or re-implement.<\/p>\n<p>In this case, we can only take a look on a two-byte (or 16 bit) instruction code. Our instruction code looks like this :<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">Byte 1<\/th>\n<th style=\"text-align: center;\">Byte 0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">|Opcode|D|W|<\/td>\n<td style=\"text-align: center;\">|MOD|REG|R\/M|<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>What are these? and why we should learn this? As we want to <em>reverse engineer <\/em>8086 architecture and learn how it works, we need to know how this processor can understand programs! So, let&#8217;s check what are those fields in these bytes :<\/p>\n<ul>\n<li>Opcode : a 6-bit number, which determines about operation (for example ADD, SUB, MOV, etc. )<\/li>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>MOD : Determines mode, as we decided before, we only model the register addressing mode, so we need to consider mode as constant 11.<\/li>\n<li>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&#8217;t consider R\/M as destination.<\/li>\n<\/ul>\n<p>Now, we learned how 8086 can understand programs, for now, we have some instruction code like this :<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">Opcode<\/th>\n<th style=\"text-align: center;\">D<\/th>\n<th style=\"text-align: center;\">W<\/th>\n<th style=\"text-align: center;\">MOD<\/th>\n<th style=\"text-align: center;\">REG<\/th>\n<th style=\"text-align: center;\">R\/M<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">xxxxxx<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">11<\/td>\n<td style=\"text-align: center;\">xxx<\/td>\n<td style=\"text-align: center;\">xxx<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let&#8217;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 :<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">Code<\/th>\n<th style=\"text-align: center;\">Register<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">000<\/td>\n<td style=\"text-align: center;\">AX<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">001<\/td>\n<td style=\"text-align: center;\">CX<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">010<\/td>\n<td style=\"text-align: center;\">DX<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">011<\/td>\n<td style=\"text-align: center;\">BX<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, now we are able to convert instructions to machine code. To make reverse engineering process even simpler, we can ignore &#8220;MOV&#8221;, but I prefer to include MOV in my list. So, let&#8217;s find opcodes for MOV, ADD and SUB.<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">Opcode<\/th>\n<th style=\"text-align: center;\">Operation<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">100010<\/td>\n<td style=\"text-align: center;\">MOV<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">000001<\/td>\n<td style=\"text-align: center;\">ADD<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">010101<\/td>\n<td style=\"text-align: center;\">SUB<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Now, we can convert every 8086 assembly instruction using the format we have. For example this piece of code :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"asm\">MOV AX, CX \r\nMOV BX, DX\r\nADD AX, BX<\/pre>\n<p>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 :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">1000101111000001\r\n=&gt; 0x8bc1\r\n\r\n1000101111011010\r\n=&gt; 0x8bda\r\n\r\n0000011111000011\r\n=&gt; 0x07c3<\/pre>\n<p>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&#8217;t initialize registers, so we need to study and implement <strong>immediate <\/strong>addressing mode, also, we can&#8217;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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/haghiri75.com\/en\/reverse-engineering-of-8086-from-a-calculator-to-the-most-used-processor\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Reverse engineering of 8086, from a calculator to the most used processor&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[2],"tags":[11,13,6,7,12,15],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8BkKn-I","jetpack-related-posts":[{"id":16,"url":"https:\/\/haghiri75.com\/en\/microcontrollers-design-and-implementation-released\/","url_meta":{"origin":44,"position":0},"title":"Microcontrollers, Design and Implementation released!","author":"prp-e","date":"April 4, 2017","format":false,"excerpt":"It was about two years I started serious study on computer architecture. In these years, I learned a lot and I could simulate and implement a microprocessor, similar to real ones. In Summer 2016, I decided to share my experience with others. Then, I started writing this book. This book\u2026","rel":"","context":"In &quot;Computer Architecture &amp; Programming&quot;","block_context":{"text":"Computer Architecture &amp; Programming","link":"https:\/\/haghiri75.com\/en\/category\/computer\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":97,"url":"https:\/\/haghiri75.com\/en\/how-to-be-a-hardware-engineer\/","url_meta":{"origin":44,"position":1},"title":"How to be a hardware engineer","author":"prp-e","date":"October 4, 2017","format":false,"excerpt":"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\u2026","rel":"","context":"In &quot;Computer Architecture &amp; Programming&quot;","block_context":{"text":"Computer Architecture &amp; Programming","link":"https:\/\/haghiri75.com\/en\/category\/computer\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":86,"url":"https:\/\/haghiri75.com\/en\/importance-of-iot\/","url_meta":{"origin":44,"position":2},"title":"Importance of IoT","author":"prp-e","date":"August 23, 2017","format":false,"excerpt":"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\u2026","rel":"","context":"In &quot;Computer Architecture &amp; Programming&quot;","block_context":{"text":"Computer Architecture &amp; Programming","link":"https:\/\/haghiri75.com\/en\/category\/computer\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":29,"url":"https:\/\/haghiri75.com\/en\/how-to-make-a-computer-program\/","url_meta":{"origin":44,"position":3},"title":"How to make a computer program","author":"prp-e","date":"April 12, 2017","format":false,"excerpt":"This is a clich\u00e9 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\u2026","rel":"","context":"In &quot;Computer Architecture &amp; Programming&quot;","block_context":{"text":"Computer Architecture &amp; Programming","link":"https:\/\/haghiri75.com\/en\/category\/computer\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":202,"url":"https:\/\/haghiri75.com\/en\/severus-does-the-magic\/","url_meta":{"origin":44,"position":4},"title":"Severus does the magic","author":"prp-e","date":"September 11, 2022","format":false,"excerpt":"It is not too long after I told you that I was too cheap to pay $10 a month for github copilot and I came up with the idea for Severus, my own AI pair programmer. It was something that went boom. My blog usually doesn't have more than 20\u2026","rel":"","context":"In &quot;Computer Architecture &amp; Programming&quot;","block_context":{"text":"Computer Architecture &amp; Programming","link":"https:\/\/haghiri75.com\/en\/category\/computer\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/warforged_wizard_by_captdiablo_de2mwm8-pre-1024x724.jpg?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/warforged_wizard_by_captdiablo_de2mwm8-pre-1024x724.jpg?resize=350%2C200 1x, https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/warforged_wizard_by_captdiablo_de2mwm8-pre-1024x724.jpg?resize=525%2C300 1.5x"},"classes":[]},{"id":411,"url":"https:\/\/haghiri75.com\/en\/maralgpt-mythos-9b-just-released-and-this-is-why-im-proud-of-my-project\/","url_meta":{"origin":44,"position":5},"title":"MaralGPT &#8220;Mythos&#8221; 9B just released, and this is why I&#8217;m proud of my project.","author":"prp-e","date":"June 29, 2026","format":false,"excerpt":"The other day I was watching a youtube video about models such as Fable and the host of podcast mentioned people from China, Russia or Iran may distil these models and make their own versions and called it\u00a0dangerous. Honestly, what I found out - as an Iranian individual - is\u2026","rel":"","context":"In &quot;Computer Architecture &amp; Programming&quot;","block_context":{"text":"Computer Architecture &amp; Programming","link":"https:\/\/haghiri75.com\/en\/category\/computer\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/huggingface.co\/MaralGPT\/MaralGPT-Mythos-9B-2606\/resolve\/main\/maral-mythos-logo.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/huggingface.co\/MaralGPT\/MaralGPT-Mythos-9B-2606\/resolve\/main\/maral-mythos-logo.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/huggingface.co\/MaralGPT\/MaralGPT-Mythos-9B-2606\/resolve\/main\/maral-mythos-logo.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/huggingface.co\/MaralGPT\/MaralGPT-Mythos-9B-2606\/resolve\/main\/maral-mythos-logo.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/posts\/44"}],"collection":[{"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":7,"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/posts\/44\/revisions\/51"}],"wp:attachment":[{"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}