Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 My first ARM Assembly program! - fliermate, In armv6l architecture

views
     
TSjunyian
post Dec 5 2023, 08:57 PM, updated 2y ago

Casual
***
Junior Member
401 posts

Joined: Jan 2003


Posting on behalf of fliermate who shared this to me in private.

-------------------------------------------------------------------

My first ARM Assembly program! (In armv6l architecture)

Hi everyone! It is been a month since I last active on here.

During my leisure time, I pick up ARM Assembly, and compare it with x86 Assembly which I have learned in the past.

Since I don't have an ARM machine, I need VM to emulate, and QEMU has it: "qemu-system-arm" (32-bit) or "qemu-system-aarch64" (64-bit)

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).

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.

To look up system call number, check out "unistd_32.h" or "unistd_64.h" in /usr/include folder of your Linux distribution.

So how to do a "Hello, world" program in ARM Assembly? I referred to [2] for clue after learning the basics on [1].

CODE

.text            
.global _start
_start:
   mov r0, #1
   ldr r1, =message
   ldr r2, =len
   mov r7, #4
   swi 0

   mov r7, #1
   swi 0

.data
message:
   .asciz "Hello World"
len = .-message      


user posted image

user posted image

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.

In ARM Assembly, numerical value needed to be prefixed with "#" (e.g. #222), and constant needed to be prefixed with "=" (e.g. =msg).
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).

user posted image

And unlike x86, we save Assembly source file as ".asm", in ARM Assembly, the source file is saved with ".s" file extension.

To compile, use GNU Assembler (as) and linker (ld). It is as simple as:
CODE

as b.s -o b.o
ld b.o -o b


Then just run with "./b". Upon checking with "file b", it is a 32-bit Linux ELF ARM. Check with "readelf -a b" will show file headers and symbol table.
To make the ELF executable smaller, can use "ld b.o -s -o b" to strip off the symbol table.

So how is it compared with x86 Assembly?

CODE

ARM (32-bit)                   x86 (32-bit)
   mov r0, #1           <----> mov ebx, 1
   ldr r1, =message     <----> mov ecx, message
   ldr r2, =len         <----> mov edx, len
   mov r7, #4           <----> mov eax, 4
   swi 0                <----> int 0x80

   mov r7, #1           <----> mov eax, 1
   swi 0                <----> int 0x80



References:

1. https://azeria-labs.com/writing-arm-assembly-part-1/

2. https://kerseykyle.com/articles/ARM-assembly-hello-world

3. https://sourceforge.net/projects/rpiqemuwindows/
flashang
post Dec 5 2023, 10:03 PM

Casual
***
Junior Member
355 posts

Joined: Aug 2021


Since Android uses Dalvik bytecode as it's "machine code",
More device manufacturers (especially mobile devices) may see the potential of using this approach.

You can use any hardware and run your application on different devices without modification.

This is how Java’s write once, run anywhere concept works.

BTW,
We don't know when OS will restrict developer using machine code,
We may not using the same OS for our "future" working devices.

P/S : Personally suggest use more on high level language.

smile.gif

This post has been edited by flashang: Dec 5 2023, 10:24 PM
MatQuasar
post Dec 7 2023, 04:01 PM

Casual
***
Validating
329 posts

Joined: Jun 2023
https://www.lowyat.net/2023/313499/we-may-s...-pcs-june-2024/

More ARM PCs, and Windows ARM.....

This post has been edited by MatQuasar: Dec 28 2023, 09:35 PM

 

Change to:
| Lo-Fi Version
0.0193sec    0.24    5 queries    GZIP Disabled
Time is now: 25th November 2025 - 03:34 AM