<?xml version="1.0" encoding="utf-8"?>
<!-- generator="FeedCreator 1.7.2" -->
<rss version="2.0">
    <channel>
        <title>Lowyat.NET: Latest topics by junyian</title>
        <description></description>
        <link>http://forum.lowyat.net/</link>
        <lastBuildDate>Wed, 10 Jun 2026 12:39:40 +0800</lastBuildDate>
        <generator>FeedCreator 1.7.2</generator>
        <item>
            <title>My first ARM Assembly program&amp;#33; - fliermate</title>
            <link>http://forum.lowyat.net/topic/5424842</link>
            <description>Posting on behalf of fliermate who shared this to me in private.&lt;br /&gt;&lt;br /&gt;-------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;My first ARM Assembly program&amp;#33; (In armv6l architecture)&lt;br /&gt;&lt;br /&gt;Hi everyone&amp;#33; It is been a month since I last active on here.&lt;br /&gt;&lt;br /&gt;During my leisure time, I pick up ARM Assembly, and compare it with x86 Assembly which I have learned in the past.&lt;br /&gt;&lt;br /&gt;Since I don&amp;#39;t have an ARM machine, I need VM to emulate, and QEMU has it: &amp;quot;qemu-system-arm&amp;quot; (32-bit) or &amp;quot;qemu-system-aarch64&amp;quot; (64-bit)&lt;br /&gt;&lt;br /&gt;I downloaded a Raspberry Pi OS raw image for use by QEMU from [3], the file is dated back to 2012, and it is armv6l (ARM 11).&lt;br /&gt;&lt;br /&gt;In Linux world, Assembly programming can be done in x86 or ARM, using the same system call numbers. Note that 32-bit and 64-bit system call numbers are different.&lt;br /&gt;&lt;br /&gt;To look up system call number, check out &amp;quot;unistd_32.h&amp;quot; or &amp;quot;unistd_64.h&amp;quot; in /usr/include folder of your Linux distribution.&lt;br /&gt;&lt;br /&gt;So how to do a &amp;quot;Hello, world&amp;quot; program in ARM Assembly? I referred to [2] for clue after learning the basics on [1].&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;.text &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;.global _start&lt;br /&gt;_start&amp;#58;&lt;br /&gt; &amp;nbsp; &amp;nbsp;mov r0, #1&lt;br /&gt; &amp;nbsp; &amp;nbsp;ldr r1, =message&lt;br /&gt; &amp;nbsp; &amp;nbsp;ldr r2, =len&lt;br /&gt; &amp;nbsp; &amp;nbsp;mov r7, #4&lt;br /&gt; &amp;nbsp; &amp;nbsp;swi 0&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;mov r7, #1&lt;br /&gt; &amp;nbsp; &amp;nbsp;swi 0&lt;br /&gt;&lt;br /&gt;.data&lt;br /&gt;message&amp;#58;&lt;br /&gt; &amp;nbsp; &amp;nbsp;.asciz &amp;#34;Hello World&amp;#34;&lt;br /&gt;len = .-message &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src='https://pictr.com/images/2023/12/05/EJ1Gan.png' border='0' alt='user posted image' /&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src='https://pictr.com/images/2023/12/05/EJ1n7G.png' border='0' alt='user posted image' /&gt;&lt;br /&gt;&lt;br /&gt;In 32-bit Linux, system call 4 is sys_write, system call 1 is sys_exit, the number needs to be put in register r7, all other r0 to r6 are arguments.&lt;br /&gt;&lt;br /&gt;In ARM Assembly, numerical value needed to be prefixed with &amp;quot;#&amp;quot; (e.g. #222), and constant needed to be prefixed with &amp;quot;=&amp;quot; (e.g. =msg).&lt;br /&gt;Unlike x86, ARM does not support memory to memory data processing, which in turn use a load / store architecture (LDR for load, STR for store).&lt;br /&gt;&lt;br /&gt;&lt;img src='https://pictr.com/images/2023/12/05/EJ11OJ.png' border='0' alt='user posted image' /&gt;&lt;br /&gt;&lt;br /&gt;And unlike x86, we save Assembly source file as &amp;quot;.asm&amp;quot;, in ARM Assembly, the source file is saved with &amp;quot;.s&amp;quot; file extension.&lt;br /&gt;&lt;br /&gt;To compile, use GNU Assembler (as) and linker (ld). It is as simple as:&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;as b.s -o b.o&lt;br /&gt;ld b.o -o b&lt;br /&gt;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;Then just run with &amp;quot;./b&amp;quot;. Upon checking with &amp;quot;file b&amp;quot;, it is a 32-bit Linux ELF ARM. Check with &amp;quot;readelf -a b&amp;quot; will show file headers and symbol table.&lt;br /&gt;To make the ELF executable smaller, can use &amp;quot;ld b.o -s -o b&amp;quot; to strip off the symbol table.&lt;br /&gt;&lt;br /&gt;So how is it compared with x86 Assembly?&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;ARM &amp;#40;32-bit&amp;#41; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; x86 &amp;#40;32-bit&amp;#41;&lt;br /&gt; &amp;nbsp; &amp;nbsp;mov r0, #1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;#60;----&amp;#62; mov ebx, 1&lt;br /&gt; &amp;nbsp; &amp;nbsp;ldr r1, =message &amp;nbsp; &amp;nbsp; &amp;#60;----&amp;#62; mov ecx, message&lt;br /&gt; &amp;nbsp; &amp;nbsp;ldr r2, =len &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;#60;----&amp;#62; mov edx, len&lt;br /&gt; &amp;nbsp; &amp;nbsp;mov r7, #4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;#60;----&amp;#62; mov eax, 4&lt;br /&gt; &amp;nbsp; &amp;nbsp;swi 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;#60;----&amp;#62; int 0x80&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;mov r7, #1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;#60;----&amp;#62; mov eax, 1&lt;br /&gt; &amp;nbsp; &amp;nbsp;swi 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;#60;----&amp;#62; int 0x80&lt;br /&gt;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;References:&lt;br /&gt;&lt;br /&gt;1. &lt;a href='https://azeria-labs.com/writing-arm-assembly-part-1/' target='_blank'&gt;https://azeria-labs.com/writing-arm-assembly-part-1/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;2. &lt;a href='https://kerseykyle.com/articles/ARM-assembly-hello-world' target='_blank'&gt;https://kerseykyle.com/articles/ARM-assembly-hello-world&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;3. &lt;a href='https://sourceforge.net/projects/rpiqemuwindows/' target='_blank'&gt;https://sourceforge.net/projects/rpiqemuwindows/&lt;/a&gt;</description>
            <author>junyian</author>
            <category>Codemasters</category>
            <pubDate>Tue, 05 Dec 2023 20:57:11 +0800</pubDate>
        </item>
    </channel>
</rss>
