Does sending a message to nil in the iOS development-oc crash? Why?

Source: Internet
Author: User
Tags bitwise

Objective-c is based on the C language,
On your PC, you operate the null pointer in C language,
The program will crash because of a protection error due to Cross-border access.

Reasons need to be found from the source code,
Here is the objc_msgsend arm version of the assembly code fragment:
In the process of ARM's function call,
Generally use R0-R4 to pass parameters,
Pass the return value with R0.
Corresponds to Objc_msgsend, the first argument is self, and the return value is self, both in R0 (A1).

/********************************************************************

 * idobjc_msgsend (ID self, SEL op, ...)

 * On ENTRY:A1 are the receiver,

 * A2 is the                  selector

 *********************************************** /



ENTRY objc_msgsend

# Check whether receiver is nil

Teq     -A1, #0  // Statement one: Determine if self is not an empty

moveq   A2, #0//If the statement first determines that self is empty, then also leave the SEL empty, otherwise do not execute this

bxeq    LR  // If the statement first determines that self is empty, it returns to the place where the call Objc_msgsend continues to execute

TEQ instruction Description:

TEQ Rn, Operand2

The TEQ instruction performs a bitwise (bitwise, bitwise) Exclusive OR operation on the value in Rn and the value of Operand2.
Bitwise AND 0 XOR or, judge is not 0
Tests whether self is empty.

Moveq instruction Description:

If self is empty, the selector is also set to null.

BX directive Description:

In arm, BX LR is used to return to the calling subroutine (that is, to return to the caller), where if self is empty, return to the place where the call Objc_msgsend continues.

anyway:

If the self argument passed to Objc_msgsend is nil, the function does not perform a meaningful operation and returns directly.

Arm Assembly Instruction
1. Conditional execution suffix:
A conditional execution suffix is appended to the ARM assembly instruction to determine whether the statement will be executed

mov r0, R1: equivalent to the C language of the r0 = R1;

Moveq R0, R1:
If the EQ suffix is established, then directly execute MOV r0, r1;
If the EQ does not set up then this sentence code directly void, equivalent to No.
Similar to the C language if (eq) {r0 = r1;}

Conditional suffix execution Note points:

1, conditional suffix is set up, not depending on this sentence code, but depends on the code before the code run the results.

2), the conditional suffix determines whether this code is executed without affecting whether the previous sentence or the next code is executed.

2. Comparison instruction CMP

CMP instruction: Compares two operands and stores the results in CPSR for the next statement to use

CMP R0,r1; comparison r0,r1; equivalent to sub R2, R0, r1 (r2 = r0-r1)

CMN R0,r1; Equivalent to add r0, R1

TST R0, #0xf; Test whether the R0 Bit0~bit3 is all 0

Teq:teq is for 2 number, carry Eor (XOR)

Note: The comparison instruction is used to compare the number of 2 registers, and the comparison instruction does not need to add the s suffix to affect the flag bit in the CPSR.

3. Jump Statement B/BL/BX

In the ARM program there are two ways to achieve the program flow of the jump:

• Use special jump instruction B

• Directly to the program counter PC write jump address value, which is almost any kind of CPU necessary, the PC said the CPU is currently executing the statement location, change the value of the PC, equivalent to implement program jump, similar to the C language return statement. With MOV PC,LR, this can be in any 4G of space to jump.

• Unlike mov pc,xxx can jump in 4G space, the B statement can only be 32M space jump (because the offset is a signed 26bit numerical =32m)

The b instruction (Branch) represents an unconditional jump. Example: B main; Jump to code where the label is main

The BL directive (Branch with Link) represents a jump with a return value, and the return address is placed in LR before the jump to return. Example: BL delay; Executing a child function or code snippet Delay,delay can be a C function.

BL more than B to do one step, before the jump, BL will be the current position in the R14 (ie LR register), when the jump code end, with MOV PC,LR instructions to jump back, this is actually the use of C language execution functions. The Assembly of the tone program with BL execution after the child function, with MOV pc,lr jump back.

Related Article

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.