2010-12-06 61 views
3

所以我拿起了程序集編程。這在我的Ubuntu盒子裏很簡單:使用NASMamd GNU ld,我能夠在半小時內編寫更多或更少複雜的HelloWorld風格的程序。 但是,當談到iPhone時,它非常複雜。首先,我在4.2.1固件上安裝JB'en iPhone 3G,這意味着我使用Darwin內核v10的A​​RM端口。其次。我必須使用GNU,因爲iPhone上沒有NASM:本地工具鏈(Mac OS X上的Xcode和Linux上的開源tooolchain)都使用GCC。 因此,我彙集了以下基本信息: - 如何在GNU中編寫程序集作爲語言; - 基本的ARM指令,寄存器,存儲器訪問是什麼。ARM達爾文程序集 - 尋找系統調用(教程也許)

但即使HelloWorld需要內核調用寫入標準輸出。我的問題是:使用什麼內核調用以及如何(哪些參數在哪裏);我應該使用swi#ARM指令,不是嗎?

那麼,你可以請張貼一些信息/鏈接到教程,或有人與ARM達爾文你好世界asm代碼?

截至目前,我可以這樣做:

;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 

ARM的,但是,我只能這樣做:

.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 

所以,任何人都好嗎?

+2

一件好事,你可以做的就是運行GCC與-S標誌,這將給你可以看看你的程序集輸出,看看它是如何做到的。 – 2010-12-06 23:13:37

回答

1

這裏的(libSystem中)的libc如何做它:

; 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 

即:

  1. 系統調用號爲R12(見SYS/syscall.h)。
  2. 系統調用指令是SVC 0x80(SWI 0x80)。
  3. 其他參數是根據ABI(R0-R3,然後堆棧)。
  4. 出錯時,進位標誌置位,並在R0中返回錯誤編號。