Calling C printf from ARM ASM

A while back I started to learn some ARM assembly on the Raspberry Pi. In my previous snippets, I was using SWI to do a system call to write to stdout, but it appears syscall 4 to write to stdout only writes Strings – if you have a byte value, this approach doesn’t work (without converting to a String first?)

From this question here, it appears you can call C functions like printf, which seems like cheating a bit, but I guess it gets the job done.

Here’s a quick snippet to add two numbers, and then print the result using printf with a string containing %d to substitute the result into the String:

[code]
.global main
.extern printf
main:
push {ip, lr}
mov r3, #1 /* move 1 to r3 */
mov r4, #2 /* move 2 to r4 */
add r4, r3, r4 /* r4 = r3 + r4 */
_ouput:
ldr r0,=output /* load address of output string */
mov r1, r4 /* move r4 result to r1 to include as param in string */
bl printf /* call printf */
_exit:
MOV R1, #0
MOV R7, #1
SWI 0
.data
output:
.asciz "Result: %d\n"
[/code]

Where previously I was compiling and linking in two steps with as and ld, it seems this doesn’t work if you are referencing C functions too, so for this example, compile with: ‘gcc add.s -o add’

This snippet and a few others I’ve started to collect I’ve shared in a github repo here: https://github.com/kevinhooke/learning-arm-asm

Recovering MondoDB from an unclean shutdown

If MongoDB is refusing to start up and you see this message:

**************
 old lock file: \data\db\mongod.lock.  probably means unclean shutdown,
 but there are no journal files to recover.
 this is likely human error or filesystem corruption.
 found 1 dbs.
 see: http://dochub.mongodb.org/core/repair for more information
 *************

… see the instructions pointed to by the suggested URL.

Try starting up with the –repair option. When it completes, restart the server process as normal.

If you have the ‘probably means unclean shutdown’ message, remove the mongod.lock file by hand and then restart with –repair

Creating a Java SE Embedded 8 JRE using jrecreate.sh

Java SE 8 is the first Java release to incorporate JRE ‘profiles’, customized subsets of JRE libraries according to the needs of your app.

From the SE Embedded you need to create the JRE based on a selected profile and then copy it across to your device:

./bin/jrecreate.sh --help

to get a list of the options (more notes here).

To run the above on a Mac, you’ll need to set your JAVA_HOME first, assuming you already have an SE JDK installed. The Java install on Macs is different from other platforms, to handle having multiple versions installed. I have notes in a previous post on how to do this here.

Create a compact1 profile jre with:

./bin/jrecreate.sh --dest jre8 -p compact1

Zip the created dir with

zip -r jre8.zip jre8/*

Scp it over to your target device, unzip and you’re ready to go!