{"id":553,"date":"2023-12-21T19:23:20","date_gmt":"2023-12-21T19:23:20","guid":{"rendered":"https:\/\/maboc.nl\/?p=553"},"modified":"2023-12-21T19:23:20","modified_gmt":"2023-12-21T19:23:20","slug":"assembly-and-opcodes","status":"publish","type":"post","link":"https:\/\/maboc.nl\/?p=553","title":{"rendered":"Assembly and opcodes"},"content":{"rendered":"<h2>Intro<\/h2>\n<p>When a program is compiled (and linked) it will no longer contains readable lines of code. Instead it will consist of opcodes (operation codes). I would like to see whether I can find out what dome opcodes mean. You might say &#8220;just look at the intel or AMD manual and there you go&#8221;&#8230;but where is the fun in that.<\/p>\n<p>Take care&#8230;I&#8217;m not an assembly professional!!! I&#8217;m an enthusiastic hobbyist. If you see errors\/omissions\/&#8230;. do not hestiate to point that out. (but please be nice ( \ud83d\ude42 )<\/p>\n<h2>Approach<\/h2>\n<ol>\n<li>\u00a0Make a very small assembly program (you will see in a second)<\/li>\n<li>Assemble it<\/li>\n<li>link it<\/li>\n<li>Make a hexdump (to make it a more easy read)<\/li>\n<li>Change one instruction in the program<\/li>\n<li>Assemble<\/li>\n<li>Link<\/li>\n<li>Make hexdump<\/li>\n<li>Search for differences in the two hexdumps<\/li>\n<li>See if you can learn something about opcodes<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h2>Do the work<\/h2>\n<h3>1. Make a very small assembly program (you will see in a second)<\/h3>\n<p>I created the following small program<\/p>\n<pre>[martijn@fedora asm]$ cat mov_al_1.asm\r\nsection .data    ;I have no data\r\n\r\nsection .text    ;Here we start the coding\r\n\r\nglobal _start    ;Define our entry point\r\n\r\n_start:          ;Start\r\nmov al, 1        ;Our instruction of interest\r\n\r\nmov rax, 60      ;Syscall for exiting the program\r\nmov rdi, 0       ;Return code is zero\r\nsyscall          ;Make the call\r\n\r\n[martijn@fedora asm]$<\/pre>\n<p>As you might see. The program does nothing of interest. Only thing it does is moving the value 1 into the register al (1 byte width). The idea is only changing this mov instruction. If everything else stays the same&#8230;we should be able to see what changed and then learn something.<\/p>\n<h2>2. Assemble it<\/h2>\n<pre> [martijn@fedora asm]$ nasm -o mov_al_1.o -f elf64 mov_al_1.asm<\/pre>\n<h3>3. Link it<\/h3>\n<pre>[martijn@fedora asm]$ ld -o mov_al_1 mov_al_1.o<\/pre>\n<h3>\u00a04. Make a hexdump (to make it a more easy read)<\/h3>\n<pre>[martijn@fedora asm]$ hexdump -C mov_al_1 &gt; hd_mov_al_1<\/pre>\n<h3>5. Change one instruction in the program<\/h3>\n<pre>[martijn@fedora asm]$ cat mov_bl_1.asm\r\nsection .data\r\n\r\nsection .text\r\n\r\nglobal _start\r\n\r\n_start:\r\nmov bl, 1\r\n\r\nmov rax, 60\r\nmov rdi, 0\r\nsyscall\r\n[martijn@fedora asm]$<\/pre>\n<p>Only thing changed is &#8220;mov al, 1&#8221; to &#8220;mov bl, 1&#8221;<\/p>\n<h3>6. Assemble<\/h3>\n<pre>[martijn@fedora asm]$ nasm -o mov_bl_1.o -f elf64 mov_bl_1.asm<\/pre>\n<h3>7. Link<\/h3>\n<pre>[martijn@fedora asm]$ ld -o mov_bl_1 mov_bl_1.o<\/pre>\n<h3>8. Make hexdump<\/h3>\n<pre>[martijn@fedora asm]$ hexdump -C mov_bl_1 &gt; hd_mov_bl_1<\/pre>\n<h3><strong>9. Search for differences in the two hexdumps<\/strong><\/h3>\n<p>The hexdump of the &#8220;mov al, 1&#8221; program<\/p>\n<pre>[martijn@fedora asm]$ cat hd_mov_al_1 \r\n00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|\r\n00000010 02 00 3e 00 01 00 00 00 00 10 40 00 00 00 00 00 |..&gt;.......@.....|\r\n00000020 40 00 00 00 00 00 00 00 e8 10 00 00 00 00 00 00 |@...............|\r\n00000030 00 00 00 00 40 00 38 00 02 00 40 00 05 00 04 00 |....@.8...@.....|\r\n00000040 01 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n00000050 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 |..@.......@.....|\r\n00000060 b0 00 00 00 00 00 00 00 b0 00 00 00 00 00 00 00 |................|\r\n00000070 00 10 00 00 00 00 00 00 01 00 00 00 05 00 00 00 |................|\r\n00000080 00 10 00 00 00 00 00 00 00 10 40 00 00 00 00 00 |..........@.....|\r\n00000090 00 10 40 00 00 00 00 00 0e 00 00 00 00 00 00 00 |..@.............|\r\n000000a0 0e 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 |................|\r\n000000b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n*\r\n00001000 b0 01 b8 3c 00 00 00 bf 00 00 00 00 0f 05 00 00 |...&lt;............|\r\n00001010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n00001020 00 00 00 00 00 00 00 00 01 00 00 00 04 00 f1 ff |................|\r\n00001030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n00001040 13 00 00 00 10 00 01 00 00 10 40 00 00 00 00 00 |..........@.....|\r\n00001050 00 00 00 00 00 00 00 00 0e 00 00 00 10 00 01 00 |................|\r\n00001060 00 20 40 00 00 00 00 00 00 00 00 00 00 00 00 00 |. @.............|\r\n00001070 1a 00 00 00 10 00 01 00 00 20 40 00 00 00 00 00 |......... @.....|\r\n00001080 00 00 00 00 00 00 00 00 21 00 00 00 10 00 01 00 |........!.......|\r\n00001090 00 20 40 00 00 00 00 00 00 00 00 00 00 00 00 00 |. @.............|\r\n000010a0 00 6d 6f 76 5f 61 6c 5f 31 2e 61 73 6d 00 5f 5f |.mov_al_1.asm.__|\r\n000010b0 62 73 73 5f 73 74 61 72 74 00 5f 65 64 61 74 61 |bss_start._edata|\r\n000010c0 00 5f 65 6e 64 00 00 2e 73 79 6d 74 61 62 00 2e |._end...symtab..|\r\n000010d0 73 74 72 74 61 62 00 2e 73 68 73 74 72 74 61 62 |strtab..shstrtab|\r\n000010e0 00 2e 74 65 78 74 00 00 00 00 00 00 00 00 00 00 |..text..........|\r\n000010f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n*\r\n00001120 00 00 00 00 00 00 00 00 1b 00 00 00 01 00 00 00 |................|\r\n00001130 06 00 00 00 00 00 00 00 00 10 40 00 00 00 00 00 |..........@.....|\r\n00001140 00 10 00 00 00 00 00 00 0e 00 00 00 00 00 00 00 |................|\r\n00001150 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 |................|\r\n00001160 00 00 00 00 00 00 00 00 01 00 00 00 02 00 00 00 |................|\r\n00001170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n00001180 10 10 00 00 00 00 00 00 90 00 00 00 00 00 00 00 |................|\r\n00001190 03 00 00 00 02 00 00 00 08 00 00 00 00 00 00 00 |................|\r\n000011a0 18 00 00 00 00 00 00 00 09 00 00 00 03 00 00 00 |................|\r\n000011b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n000011c0 a0 10 00 00 00 00 00 00 26 00 00 00 00 00 00 00 |........&amp;.......|\r\n000011d0 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|\r\n000011e0 00 00 00 00 00 00 00 00 11 00 00 00 03 00 00 00 |................|\r\n000011f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n00001200 c6 10 00 00 00 00 00 00 21 00 00 00 00 00 00 00 |........!.......|\r\n00001210 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|\r\n00001220 00 00 00 00 00 00 00 00 |........|\r\n00001228\r\n[martijn@fedora asm]$<\/pre>\n<p>And the hexdump of the &#8220;mov bl, 1&#8221; program<\/p>\n<pre>[martijn@fedora asm]$ cat hd_mov_bl_1 \r\n00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|\r\n00000010 02 00 3e 00 01 00 00 00 00 10 40 00 00 00 00 00 |..&gt;.......@.....|\r\n00000020 40 00 00 00 00 00 00 00 e8 10 00 00 00 00 00 00 |@...............|\r\n00000030 00 00 00 00 40 00 38 00 02 00 40 00 05 00 04 00 |....@.8...@.....|\r\n00000040 01 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n00000050 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 |..@.......@.....|\r\n00000060 b0 00 00 00 00 00 00 00 b0 00 00 00 00 00 00 00 |................|\r\n00000070 00 10 00 00 00 00 00 00 01 00 00 00 05 00 00 00 |................|\r\n00000080 00 10 00 00 00 00 00 00 00 10 40 00 00 00 00 00 |..........@.....|\r\n00000090 00 10 40 00 00 00 00 00 0e 00 00 00 00 00 00 00 |..@.............|\r\n000000a0 0e 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 |................|\r\n000000b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n*\r\n00001000 b3 01 b8 3c 00 00 00 bf 00 00 00 00 0f 05 00 00 |...&lt;............|\r\n00001010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n00001020 00 00 00 00 00 00 00 00 01 00 00 00 04 00 f1 ff |................|\r\n00001030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n00001040 13 00 00 00 10 00 01 00 00 10 40 00 00 00 00 00 |..........@.....|\r\n00001050 00 00 00 00 00 00 00 00 0e 00 00 00 10 00 01 00 |................|\r\n00001060 00 20 40 00 00 00 00 00 00 00 00 00 00 00 00 00 |. @.............|\r\n00001070 1a 00 00 00 10 00 01 00 00 20 40 00 00 00 00 00 |......... @.....|\r\n00001080 00 00 00 00 00 00 00 00 21 00 00 00 10 00 01 00 |........!.......|\r\n00001090 00 20 40 00 00 00 00 00 00 00 00 00 00 00 00 00 |. @.............|\r\n000010a0 00 6d 6f 76 5f 62 6c 5f 31 2e 61 73 6d 00 5f 5f |.mov_bl_1.asm.__|\r\n000010b0 62 73 73 5f 73 74 61 72 74 00 5f 65 64 61 74 61 |bss_start._edata|\r\n000010c0 00 5f 65 6e 64 00 00 2e 73 79 6d 74 61 62 00 2e |._end...symtab..|\r\n000010d0 73 74 72 74 61 62 00 2e 73 68 73 74 72 74 61 62 |strtab..shstrtab|\r\n000010e0 00 2e 74 65 78 74 00 00 00 00 00 00 00 00 00 00 |..text..........|\r\n000010f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n*\r\n00001120 00 00 00 00 00 00 00 00 1b 00 00 00 01 00 00 00 |................|\r\n00001130 06 00 00 00 00 00 00 00 00 10 40 00 00 00 00 00 |..........@.....|\r\n00001140 00 10 00 00 00 00 00 00 0e 00 00 00 00 00 00 00 |................|\r\n00001150 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 |................|\r\n00001160 00 00 00 00 00 00 00 00 01 00 00 00 02 00 00 00 |................|\r\n00001170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n00001180 10 10 00 00 00 00 00 00 90 00 00 00 00 00 00 00 |................|\r\n00001190 03 00 00 00 02 00 00 00 08 00 00 00 00 00 00 00 |................|\r\n000011a0 18 00 00 00 00 00 00 00 09 00 00 00 03 00 00 00 |................|\r\n000011b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n000011c0 a0 10 00 00 00 00 00 00 26 00 00 00 00 00 00 00 |........&amp;.......|\r\n000011d0 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|\r\n000011e0 00 00 00 00 00 00 00 00 11 00 00 00 03 00 00 00 |................|\r\n000011f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n00001200 c6 10 00 00 00 00 00 00 21 00 00 00 00 00 00 00 |........!.......|\r\n00001210 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|\r\n00001220 00 00 00 00 00 00 00 00 |........|\r\n00001228\r\n[martijn@fedora asm]$<\/pre>\n<p>Here in wordpress I do not know how to display the two dump alongside eachother, so to see the changes I use diff:<\/p>\n<pre>[martijn@fedora asm]$ diff --suppress-common-lines hd_mov_al_1 hd_mov_bl_1\r\n14c14\r\n&lt; 00001000 b0 01 b8 3c 00 00 00 bf 00 00 00 00 0f 05 00 00 |...&lt;............|\r\n---\r\n&gt; 00001000 b3 01 b8 3c 00 00 00 bf 00 00 00 00 0f 05 00 00 |...&lt;............|\r\n24c24\r\n&lt; 000010a0 00 6d 6f 76 5f 61 6c 5f 31 2e 61 73 6d 00 5f 5f |.mov_al_1.asm.__|\r\n---\r\n&gt; 000010a0 00 6d 6f 76 5f 62 6c 5f 31 2e 61 73 6d 00 5f 5f |.mov_bl_1.asm.__|\r\n[martijn@fedora asm]$<\/pre>\n<h3><\/h3>\n<h3>10. See if you can learn something about opcodes<\/h3>\n<p>First thing I notice: There are differences on two lines (rather much apart from each other). I you look closely at the the last difference you see that the filename of the original assembly file is in the hexdump of the resulting executable (As a newbee that suprises me a bit). Since the first name (mov_al_1.asm) differs from the second (mov_bl_1.asm), it is no suprise they show up in the compare.<\/p>\n<p>The first diffrence is more exciting (to me at least):<\/p>\n<p>Codes I see in the first dump (mov al,1) :<\/p>\n<p>b0 01<\/p>\n<p>In the second snippet I see\u00a0 (mov bl,1):<\/p>\n<p>b3 01<\/p>\n<p>Now I cheat a little&#8230;I do have a look at the instruction manual from intel. See following snippet:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-563\" src=\"https:\/\/maboc.nl\/wp-content\/uploads\/2023\/12\/Screenshot_20231221_200502-1-300x53.png\" alt=\"Snippet of mov instruction manual\" width=\"679\" height=\"120\" srcset=\"https:\/\/maboc.nl\/wp-content\/uploads\/2023\/12\/Screenshot_20231221_200502-1-300x53.png 300w, https:\/\/maboc.nl\/wp-content\/uploads\/2023\/12\/Screenshot_20231221_200502-1-768x135.png 768w, https:\/\/maboc.nl\/wp-content\/uploads\/2023\/12\/Screenshot_20231221_200502-1.png 971w\" sizes=\"auto, (max-width: 679px) 100vw, 679px\" \/><\/p>\n<p>B0 + something and then an immediate byte value is the combination of opcodes to add a value to a (byte) register.<\/p>\n<p>I now say:<\/p>\n<p>b0 + 0 = b0 : code for addeing a direct value to (byte) register al<\/p>\n<p>b0 + 3 = b3 : code for adding a direct value to (byte) register bl<\/p>\n<p>I also tried this for mov cl,1<\/p>\n<p>There I find:<\/p>\n<p>b1 01<\/p>\n<p>b0 + 1 = b1 : code for adding a direct value to (byte) cl<\/p>\n<p>&nbsp;<\/p>\n<h2>Conclusion<\/h2>\n<p>Remeber..I&#8217;m an enthusiastic amateur. Maybe above is very wrong. Also&#8230;I do not know to much about addressing modes etc. I do not know whether (anf iyes how much) this effects the code. So&#8230;if you feel I&#8217;m in error..please point it out and bring me some knowledge.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Intro When a program is compiled (and linked) it will no longer contains readable lines of code. Instead it will consist of opcodes (operation codes). I would like to see whether I can find out what dome opcodes mean. You might say &#8220;just look at the intel or AMD manual and there you go&#8221;&#8230;but where [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[94],"class_list":["post-553","post","type-post","status-publish","format-standard","hentry","category-linux","tag-assembly"],"_links":{"self":[{"href":"https:\/\/maboc.nl\/index.php?rest_route=\/wp\/v2\/posts\/553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/maboc.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/maboc.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/maboc.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/maboc.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=553"}],"version-history":[{"count":13,"href":"https:\/\/maboc.nl\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":568,"href":"https:\/\/maboc.nl\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions\/568"}],"wp:attachment":[{"href":"https:\/\/maboc.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/maboc.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/maboc.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}