How to Use assembly language in IOS

Source: Internet
Author: User

This article systematically introduces how to use the assembly language on iOS devices using xcode.

1. Create an assembly source file:

Right-click the directory where the source file is stored in your xcode project and select new file. Then select other in the IOS column on the left. On the right side, you will see the Assembly file, select it, and name it with. s as the suffix.

2. Compile the assembly code:
 

Then try the following code:

/*
* arm7.asm
* Test
*
* Created by Zenny Chen on 4/24/10.
* Copyright 2010 GreenGames Studio. All rights reserved.
*/

.text
.align 4
.globl _my_arm_test
.globl _my_thumb_test


.arm

_my_arm_test:

vdup.32 q0, r0
qadd8 r0, r1, r2
add r0, r0, LSL #2
bx lr


.thumb
.thumb_func _my_thumb_test

_my_thumb_test:

movw r1, #1001
rev16 r0, r1
bx lr

Note that Apple's arm assembler complies with the GNU Compiler specification. We can see that the comments in the Assembly file can be annotated using the C language standard, or the C ++ standard // comments.

. Text indicates the body of the Code.

. Align has different behaviors based on different compilers. Here,. align4 may indicate 4-byte alignment or 16-byte alignment.

In the gas specification, global functions can be labeled with. Global or. globl. Only. globl is supported in Apple's referers. The name must be underlined.

. Arm indicates that all the commands in subsequent functions are arm commands. Thumb indicates that all the subsequent commands in the function are thumb or thumb-2. If a function is written with thumb, it must be modified with. thumb_func. Otherwise, the connector may have problems connecting symbols.

In the above Code, _ my_arm_test uses a neon command, an armv6 command, and two armv5te commands respectively. _ My_thumb_test uses one armv7 thumb-2 command, one armv6 command, and one armv5te command.

In addition, the conditional preprocessing in the Apple llvm assembler is almost the same as that in the C language. You can use # If, # else, # endif, # ifdef, # ifndef, # Elif, and so on. In addition, Standard Architecture identifiers are also used for architecture identifiers. For example, __i386 _ indicates the x86 processor architecture; __x86_64 _ indicates a 64-bit x86 processor; __arm _ indicates the processor of the ARM architecture. The following is an example code:

.text.align 2.globl _MyASMTest#if  defined(__i386__) || defined(__x86_64__)_MyASMTest:    xor    %eax, %eax    ret#elif defined(__arm__)_MyASMTest:    eor     r1, r0, r0    bx  lr#endif

 

3. Call assembly functions in C/C ++ or Objective C/C ++

In your C/C ++ source file, how does one call the function written in assembly? In fact, this is the same as calling a common C function.

extern int my_arm_test(int a, int b, int c);extern int my_thumb_test(int a, int b);void my_test(void){    printf("ARM value: %d\n", my_arm_test(10, 20, 30));    printf("Thumb value: %d\n", my_thumb_test(10, 20));}

 
We can see that the underline before the function name is gone. This is not required and cannot be underlined.
 

Because Apple's arm assembler does not yet fully support the thumb-2 instruction set, no. W suffix is supported. But Apple llvm3.0 and LLVM-GCC support. Therefore, we can use inline assembly in C/C ++ to write data. For details, see notes for inline pure assembly functions in the C language compiler of llvm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.