{"id":955,"date":"2011-02-14T15:49:20","date_gmt":"2011-02-14T05:49:20","guid":{"rendered":"http:\/\/brnz.org\/hbr\/?p=955"},"modified":"2011-03-01T23:09:51","modified_gmt":"2011-03-01T13:09:51","slug":"assembly-primer-part-4-%e2%80%94-hello-world-%e2%80%94-arm","status":"publish","type":"post","link":"https:\/\/brnz.org\/hbr\/?p=955","title":{"rendered":"Assembly Primer Part 4 \u2014 Hello World \u2014 ARM"},"content":{"rendered":"<p>On to <a href=\"http:\/\/www.securitytube.net\/Assembly-Primer-for-Hackers-(Part-4)-Hello-World-video.aspx\">Assembly Primer &#8212; Part 4<\/a>. This is where we start writing a small assembly program for the platform. In this case, I don&#8217;t know the language and I don&#8217;t know the ABI. Learning these from scratch ranges from interesting to tedious :)<\/p>\n<p>Regarding the language (available instructions, mnemonics and assembly syntax): I&#8217;m using the <a href=\"http:\/\/infocenter.arm.com\/help\/index.jsp?topic=\/com.arm.doc.ddi0406b\/index.html\">ARM Architecture Reference Manual<\/a> as my reference for the architecture (odd, I know). It&#8217;s very long and the documentation for each instruction is extensive &#8212; which is good because there are a lot of instructions, and many of them do a lot of things at once.<\/p>\n<p>Regarding the ABI (particularly things like argument passing, return values and system calls): there&#8217;s the <a href=\"http:\/\/www.google.com\/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CBIQFjAA&amp;url=http%3A%2F%2Finfocenter.arm.com%2Fhelp%2Ftopic%2Fcom.arm.doc.ihi0042d%2FIHI0042D_aapcs.pdf&amp;ei=HbxYTafHCIXCvgPUkun2BA&amp;usg=AFQjCNGR3tBewfkIff4HMlI6mz2s_d4CsA\">Procedure Call Standard for the ARM Architecture<\/a>, and there are a few other references I&#8217;ve found, such as the <a href=\"http:\/\/wiki.debian.org\/ArmEabiPort\">Debian ARM EABI Port wiki page<\/a>.<\/p>\n<blockquote><p>&#8220;EABI is the new &#8220;Embedded&#8221; ABI by\u00a0<a href=\"http:\/\/www.arm.com\/\">ARM ltd<\/a>. EABI is actually a family of ABI&#8217;s and one of the &#8220;subABIs&#8221; is GNU EABI, for Linux.&#8221;<\/p>\n<p>&#8211; from Debian ARM EABI Port<\/p><\/blockquote>\n<h2>System Calls<\/h2>\n<p>To perform a system call using the GNU EABI:<\/p>\n<ul>\n<li>put the system call number in r7<\/li>\n<li>put the arguments in r0-r6 (64bit arguments must be aligned to an even numbered register i.e. in r0+r1, r2+r3, or r4+r5)<\/li>\n<li>issue the <strong>Supervisor Call<\/strong> instruction with a zero operand &#8212; <strong>svc #0<\/strong><\/li>\n<\/ul>\n<p>(<strong>Supervisor Call<\/strong> was previously named <strong>Software Interrupt<\/strong> &#8212; <strong>swi<\/strong>)<\/p>\n<h2>Just Exit<\/h2>\n<p>Based on the above, it&#8217;s not difficult to reimplement <strong>JustExit.s <\/strong>(<a href=\"http:\/\/code.securitytube.net\/JustExit.s\">original<\/a>) for ARM.<\/p>\n<pre style=\"padding-left: 30px;\" lang=\"asm\">.text\r\n\r\n.globl _start\r\n\r\n_start:\r\n        mov r7, #1\r\n        mov r0, #0\r\n        svc #0\r\n<\/pre>\n<p><strong>mov<\/strong> here is <strong>Move (Immediate)<\/strong> which puts the <strong>#<\/strong>-prefixed literal into the named register.<\/p>\n<h2>Hello World<\/h2>\n<p>Likewise, the conversion of <strong>HelloWorldProgram.s<\/strong> (<a href=\"http:\/\/code.securitytube.net\/HelloWorldProgram.s\">original<\/a>) is not difficult:<\/p>\n<pre style=\"padding-left: 30px;\" lang=\"asm\">.data \r\n\r\nHelloWorldString:\r\n      .ascii \"Hello World\\n\"\r\n\r\n.text \r\n\r\n.globl _start \r\n\r\n_start:\r\n      # Load all the arguments for write () \r\n\r\n      mov r7, #4\r\n      mov r0, #1\r\n      ldr r1,=HelloWorldString\r\n      mov r2, #12\r\n      svc #0\r\n\r\n      # Need to exit the program \r\n\r\n      mov r7, #1\r\n      mov r0, #0\r\n      svc #0\r\n<\/pre>\n<p>This includes the <strong>load register<\/strong> pseudo-instruction, <strong>ldr<\/strong> &#8212; the compiler stores the address of <strong>HelloWorldString<\/strong> into the literal pool, a portion of memory located in the program text, and the 32bit address is loaded from the literal pool (<a href=\"http:\/\/infocenter.arm.com\/help\/index.jsp?topic=\/com.arm.doc.kui0100a\/armasm_bgbbfgia.htm\">more details<\/a>).<\/p>\n<p>When compiling a similar C program with <strong>-mcpu=cortex-a8<\/strong>, I notice that the compiler generates <strong>Move (immediate)<\/strong> and <strong>Move Top<\/strong> &#8212; <strong>movw<\/strong> and <strong>movt<\/strong> &#8212; instructions to load the address directly from the instruction stream, which is presumably more efficient on that architecture.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On to Assembly Primer &#8212; Part 4. This is where we start writing a small assembly program for the platform. In this case, I don&#8217;t know the language and I don&#8217;t know the ABI. Learning these from scratch ranges from interesting to tedious :) Regarding the language (available instructions, mnemonics and assembly syntax): I&#8217;m using &hellip; <a href=\"https:\/\/brnz.org\/hbr\/?p=955\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Assembly Primer Part 4 \u2014 Hello World \u2014 ARM&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,26],"tags":[45,38],"_links":{"self":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/955"}],"collection":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=955"}],"version-history":[{"count":17,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/955\/revisions"}],"predecessor-version":[{"id":977,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/955\/revisions\/977"}],"wp:attachment":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}