{"id":3305,"date":"2022-10-29T05:00:04","date_gmt":"2022-10-28T23:30:04","guid":{"rendered":"https:\/\/cthecosmos.com\/?p=3305"},"modified":"2022-10-29T05:46:53","modified_gmt":"2022-10-29T00:16:53","slug":"peeking-into-the-assembly","status":"publish","type":"post","link":"https:\/\/cthecosmos.com\/?p=3305","title":{"rendered":"Peeking Into The Assembly"},"content":{"rendered":"\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<p class=\"has-light-gray-background-color has-text-color has-background\" style=\"color:#030000;\">Assembly is tough. How does the processing of c code takes place in assembly? What are the kinds of instructions that we could see? What is actually happening in this gigantic mesh of lines?<br><br>This is my small step to look into the assembly code of a small program\/ replicating a small segment of code that was explained very well by Ben Eater\u2019s Comparing C to Machine Language Yt video (link in the reference section).<br><br>Functions involved from the start of execution to the end: <br><\/p>\n\n\n\n<p><\/p>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"363\" data-attachment-id=\"3318\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3318\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/image-3.png?fit=592%2C363&amp;ssl=1\" data-orig-size=\"592,363\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/image-3.png?fit=592%2C363&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/image-3.png?resize=592%2C363&#038;ssl=1\" alt=\"\" class=\"wp-image-3318\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-light-gray-background-color has-text-color has-background\" style=\"color:#030000;\">The code: <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"745\" height=\"477\" data-attachment-id=\"3315\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3315\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/image-1.png?fit=745%2C477&amp;ssl=1\" data-orig-size=\"745,477\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/image-1.png?fit=745%2C477&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/image-1.png?resize=745%2C477&#038;ssl=1\" alt=\"\" class=\"wp-image-3315\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-light-gray-background-color has-text-color has-background\" style=\"color:#030000;\">In assembly: <\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3316\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3316\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/image-2.png?fit=1125%2C1056&amp;ssl=1\" data-orig-size=\"1125,1056\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/image-2.png?fit=1000%2C939&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/image-2.png?resize=592%2C555&#038;ssl=1\" alt=\"\" class=\"wp-image-3316\" width=\"592\" height=\"555\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-light-gray-background-color has-text-color has-background\" style=\"color:#030000;\">Before we jump into that, let&#8217;s just look at some common instruction sets that we encounter through this blog.<br><br><strong>%rbp, %rsp<\/strong> are special purpose registers<br><br><strong>%rbp <\/strong>is the base pointer which points to the base of the current stack frame<br><br><strong>%rsp<\/strong> is the stack pointer which points to the top of the current stack frame<br><br><strong>%rbp<\/strong> always has a higher value than <strong>%rsp<\/strong> because the stack starts at high memory address and grows downwards.<br><br><strong>%eax, %ecx<\/strong> are general purpose registers<br><br><strong>eax<\/strong>= Extended AX register ( a 32 bit register )<br><br>AX is 16 bits wide, the high byte can be accessed with Ah and the low byte with AL<br><br>RAX&nbsp; is 64-bit register<br><\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"836\" height=\"197\" data-attachment-id=\"3308\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3308\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/image.png?fit=836%2C197&amp;ssl=1\" data-orig-size=\"836,197\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/image.png?fit=836%2C197&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/image.png?resize=836%2C197&#038;ssl=1\" alt=\"\" class=\"wp-image-3308\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-light-gray-background-color has-text-color has-background\" style=\"color:#030000;\">Now coming to the code\u2026 (Please refer to the image and code parallelly.)<br><br><strong><em>push %rbp<br>mov %rsp, %rbp<\/em><\/strong><br><br>These are function prologue or preamble.<br>First, push the old base pointer onto the stack and save it for later.<br>Then copy the value of the stack pointer to the base pointer.<br>now <strong>%rbp<\/strong> points to the base of the main&#8217;s stack frame.<br><br><strong><em>sub $0x10,%rsp<\/em><\/strong><br><br>This instruction allocates space on the stack,<br><br><strong><em>movl&nbsp; $0x0, -0xc(%rbp)<br>movl&nbsp; $0x1, -0x8(%rbp)<\/em><\/strong><br><br>The parentheses indicate the memory address. Here <strong>rbp <\/strong>is the base register with <strong>-0xc<\/strong> displacement. This simply means <strong>%rbp<\/strong>&nbsp; <strong>+ -0xc<\/strong> i.e subtracting <strong>c(12)<\/strong> from the base pointer which moves to the current stack frame where the value <strong>0<\/strong> is stored. Comparing it with the assembly code, we find that the value of <strong>x<\/strong> is <strong>0<\/strong> and the value of <strong>y<\/strong> is <strong>1<\/strong>, i.e at <strong>%rbp &#8211;&nbsp; 0xc<\/strong> the value of <strong>x<\/strong> is stored and at <strong>%rbp&nbsp; &#8211; 0x8<\/strong> the value of <strong>y<\/strong> is stored.<br><br><strong><em>mov -0xc(%rbp),%eax<br>mov %eax,%esi<br>lea 0xe95(%rip),%rax<br>mov %rax %rdi<br>mov $0x0,%eax<br>call 0x1050 &lt;printf@plt&gt;<\/em><\/strong><br><br><br>In the first line, we can see that the value stored in <strong>0xc<\/strong> i.e value of <strong>x<\/strong> is being brought into <strong>eax<\/strong>. Here <strong>eax<\/strong> is one of the general-purpose registers.<br><br>In the Second Step, <strong>esi<\/strong> is a source index pointer that is used for strings and memory array copying. In this case, the memory array of <strong>eax<\/strong> is being copied into <strong>esi<\/strong>.<br><br>In the third line, <strong>lea<\/strong> means <strong>load effective address<\/strong>. <strong>lea<\/strong> moves the contents of the designated memory into the target location. Here the target location is <strong>rax<\/strong> i.e it will move the contents of <strong>0xe95(%rip)<\/strong> to <strong>rax<\/strong>. Here <strong>rip<\/strong> is a special-purpose <strong>register and instruction pointer<\/strong>. So this instruction says, take the value stored at <strong>0xe95<\/strong> and load it into <strong>rax<\/strong>.<br><br>In the fourth line, registers such as <strong>%rdi are<\/strong> commonly known as <strong>caller-save registers<\/strong> i.e they are not necessarily saved across function calls. <strong>%rdi<\/strong> is also used to pass the first six integer or pointer parameters to called functions. &nbsp;<strong>%rax<\/strong> is usually used to store the function return value. These instructions are used to call a function. To call a function the program should place the first six integer or pointer parameters in the register. Here <strong>%rdi<\/strong> is doing the job.<br><br>In the fifth line, <strong>0<\/strong> is being written into <strong>eax<\/strong> register. Here <strong>eax<\/strong> is a <strong>general-purpose register<\/strong>. The x86 calling convention dictates that a function\u2019s return value is stored in <strong>%eax<\/strong>, so the above instruction sets us up to return <strong>0<\/strong> at the end of our function.<br><br>In the sixth line, This instruction calls the <strong>printf<\/strong> function<br><br><strong><em>mov -0xc(%rbp),%edx<br>mov -0x8(%rbp),%eax<br>add %edx,%eax<br>mov %eax, -0x4(%rbp)<\/em><\/strong><br><br>Now we are doing the <strong>z=x+y;<\/strong> operation. Here the values stored in <strong>x<\/strong> i.e at <strong>-0xc(%rbp)<\/strong> and <strong>y<\/strong> at <strong>-0x8(%rbp)<\/strong> are copied into <strong>edx<\/strong> and <strong>eax<\/strong> respectively.<br><br>Once that\u2019s done, the next operation adds the values and stores the result in <strong>%eax<\/strong>. Once the addition is done, the result that was stored in <strong>eax<\/strong> is copied to <strong>-0x4(%rbp)<\/strong>.<br><br><strong><em>mov -0x8(%rbp),%eax<br>mov %eax,-0xc(%rbp)<br><br>mov -0x4(%rbp),%eax<br>mov %eax,-0x8(%rbp)<\/em><\/strong><br><br>In these instructions we are loading the values into <strong>eax<\/strong> from respective memory locations and placing them in <strong>-0xc(%rbp)<\/strong> and <strong>-0x8(%rbp)<\/strong> respectively. They are nothing but the locations of <strong>x<\/strong> and <strong>y<\/strong>, and hence the following segment of code is being executed.&nbsp; Here <strong>-0x4(%rbp)<\/strong> is where <strong>z<\/strong> is stored.<br><br><em><strong>x=y;<\/strong><br><strong>y=z;<\/strong><\/em><br><br>Once that\u2019s done, the compiler would be moving to the next step of execution, that is <strong>while(x&lt;255);<\/strong> . The assembly instruction for the above line of code is <strong><em>cmpl $0xfe, -0xc(%rbp)<\/em><\/strong>. Here <strong>cmpl <\/strong>is an instruction that indicates to comparison of double word.<br><br><strong><em>jle 0x1163 &lt;main+26&gt;<br>jmp 0x1155 &lt;main+15&gt;<\/em><\/strong><br><br>The last 2 instructions are <strong>jle<\/strong>, i.e jump if less than or equal and <strong>jmp<\/strong> where it jumps to <strong>label<\/strong><br><br>So, that was some description on assembly code of a small code. I do agree there are many stones left unturned here. It was a good exploration for me. Hope you find this helpful.<br><br>The links in the reference section would definitely help you to dive more deeply.<\/p>\n\n\n\n<p class=\"has-white-color has-vivid-cyan-blue-to-vivid-purple-gradient-background has-text-color has-background\">Thanks Y&#8217;all. Do share your opinion and don&#8217;t forget to follow to grab the first notification of my new blog posts.<\/p>\n\n\n\n<p class=\"has-black-color has-blush-bordeaux-gradient-background has-text-color has-background\"><strong>References:<\/strong><br>1) Ben Eater&nbsp; &#8211; &nbsp;Comparing C to Machine Language: <a href=\"https:\/\/www.youtube.com\/watch?v=yOyaJXpAYZQ\">https:\/\/www.youtube.com\/watch?v=yOyaJXpAYZQ<\/a><br>2) x64&nbsp; cheat sheet &#8211; <a href=\"https:\/\/cs.brown.edu\/courses\/cs033\/docs\/guides\/x64_cheatsheet.pdf\">https:\/\/cs.brown.edu\/courses\/cs033\/docs\/guides\/x64_cheatsheet.pdf<\/a><br>3) <a href=\"https:\/\/www.recurse.com\/blog\/7-understanding-c-by-learning-assembly\">https:\/\/www.recurse.com\/blog\/7-understanding-c-by-learning-assembly<\/a><br>4) <a href=\"https:\/\/medium.com\/swlh\/how-does-hello-world-actually-work-73a557be16eb\">https:\/\/medium.com\/swlh\/how-does-hello-world-actually-work-73a557be16eb<\/a><br>5) <a href=\"https:\/\/cs61.seas.harvard.edu\/site\/2018\/Asm1\/\" rel=\"nofollow\">https:\/\/cs61.seas.harvard.edu\/site\/2018\/Asm1\/<\/a><br>6) A good number of tabs of stack overflow.<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Assembly is tough. How does the processing of c code takes place in assembly? What are the kinds of instructions that we could see? What is actually happening in this gigantic mesh of lines? This is my small step to look into the assembly code of a small program\/ replicating a small segment of code [&hellip;]<\/p>\n<a href=\"https:\/\/cthecosmos.com\/?p=3305\" class=\"more-link\">Read More <span class=\"screen-reader-text\">Peeking Into The Assembly<\/span><\/a>","protected":false},"author":120055267,"featured_media":3385,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false},"categories":[1342,28627],"tags":[10544,6595,2426,772321195,772321191,320749,12068,756676048,34922710,1727050,34920936,88026196,868553],"class_list":{"0":"post-3305","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-education","8":"category-embedded","9":"tag-analysis","10":"tag-assembly","11":"tag-c","12":"tag-c-programming","13":"tag-embedded","14":"tag-gdb","15":"tag-intel","16":"tag-intel-registers","17":"tag-linux-2","18":"tag-registers","19":"tag-ubuntu-2","20":"tag-ubuntu-app","21":"tag-x86","23":"fallback-thumbnail"},"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2022\/10\/assembly_analysis.png?fit=541%2C510&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8CiEf-Rj","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/users\/120055267"}],"replies":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3305"}],"version-history":[{"count":24,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3305\/revisions"}],"predecessor-version":[{"id":3387,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3305\/revisions\/3387"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/media\/3385"}],"wp:attachment":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}