-1

So I took up assembly programming. It's quite simple on my Ubuntu box: using NASMamd GNU ld, I were able to write more or less complicated HelloWorld-style programs in half an hour. But when it comes to the iPhone, it's so complicated. First of all, I have a JB'en iPhone 3G on 4.2.1 firmware, which means that I use the ARM port of the Darwin kernel v10. Second. I have to use GNU as, as there's no NASM for iPhone: the native toolchain (both Xcode on Mac OS X and the opensource tooolchain on linux) use GCC. So I have gathered together basic info about: - how to write assembly in GNU as language; - what are the basic ARM instructions, registers, memory access.

But even HelloWorld requires kernel calls for writing to stdout. My question is: what kernel call to use and how (what arguments go where); I should use the swi # ARM instruction, shouldn't I?

So, can you please post some info/links to tutorials, or somebody with an ARM Darwin Hello world asm code?

As of now, I could do this:

;Hello World for Linux and NASM
section data
hello db "Hello World"
helloLen equ $ - hello

section text
global _start
_start:
    mov eax, 4 ; sys_write
    mov ebx, 1 ; to stdout
    mov ecx, hello ; address of string
    mov edx, helloLen ; value (because of eq!!!) of strLen
    int 0x80 ; call awesome Linux kernel

    mov eax, 1 ; sys_exit
    mov ebx, 0 ; "return 0; " if you like C
    int 0x80 ; call kernel to end program

on ARM, however, I could only do like this:

.text
start:
    mov r0, #0
    mov r1, #234
    add r2, r0, r1
@all mov and add and other stuff works fine
    swi #0xc00
@all that I get is Bad system call error

So, anybody please?

H2CO3
  • 23
  • 1
  • 2
  • 2
    One good thing you can do is run GCC with the -S flag, which will give you assembly output that you can look at to see how it does it. – Anon. Dec 06 '10 at 23:13
  • Thanks, unfortunately I tried this, but I only got quite "obfuscated" code what was referencing _printf in libgcc. May I use __asm__() C function to convert inline Intel-style asm into ARM code? (I'll give it a try). – H2CO3 Dec 07 '10 at 05:10

2 Answers2

1

Best I can find right quick and yea I realize the initial post is old

http://blog.softboysxp.com/post/7888230192/a-minimal-168-byte-mach-o-arm-executable-for-ios

.text
.globl start

start:
mov r2, #14
adr r1, hello_str
mov r0, #1
mov r12, #4
swi 0x80

mov r0, #0
mov r12, #1
swi 0x80

hello_str:
.ascii  "Hello, World!\n"

compile:
as new.asm -o new.o
ld new.o -o new
./new
j0k
  • 22,600
  • 28
  • 79
  • 90
jmc31337
  • 11
  • 1
1

Here's how libc (libSystem) does it:

; ssize_t read(int, void *, size_t)
                EXPORT _read
_read
                MOV     R12, #3         ; SYS_read
                SVC     0x80 ; 'А'      ; do a syscall
                BCC     _ok             ; carry clear = no error
                LDR     R12, =(cerror_ptr - . - 8) ; otherwise call error handler
                LDR     R12, [PC,R12]   ; load pointer
                B       _call_error
                DCD cerror_ptr - .
_call_error                              
                BX      R12 ; cerror    ; jump to it (error number is in R0)
_ok
                BX      LR              ; return to caller
; End of function _read

I.e.:

  1. System call number is in R12 (see sys/syscall.h).
  2. System call instruction is SVC 0x80 (SWI 0x80).
  3. Other parameters are according to the ABI (R0-R3, then stack).
  4. On error, carry flag is set and error number is returned in R0.
Igor Skochinsky
  • 24,629
  • 2
  • 72
  • 109