Objective-c is based on C. On a PC, if you operate on a null pointer in C, the program will crash due to protection errors due to out-of-bounds access.

The reasons for this need to be found in the source code, the following is the arm version of objc_msgSend assembly code fragment: in the arm function call process, usually r0-R4 pass parameters, r0 pass return value. For 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 is the message receiver,

 *                  a2 is the selector

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

 

ENTRY objc_msgSend

# check whether receiver is nil

teq     a1, #0 // statement 1: Check if self is empty

moveq   a2, #0 // If self is null, then SEL should also be null, otherwise it will not be executedBxeq lr // If the statement determines that self is empty, it returns to the place where objc_msgSend was called and continues executionCopy the code

Teq instruction description:

TEQ Rn, Operand2

The TEQ instruction performs a bitwise Exclusive OR operation on The value in Rn and The value of Operand2. Bit by bit and 0 xor to determine if 0 tests if self is null.

Moveq instruction description:

If self is null, set selector to be null as well.

Bx instruction description:

In ARM, bx LR is used to return to the place where the subroutine was called (i.e., to return to the caller), which is: if self is empty, return to the place where objc_msgSend was called to continue execution.

In a word:

If the self argument passed to objc_msgSend is nil, the function does nothing meaningful and returns directly.

ARM assembly instruction 1. Conditional execution suffixes: Conditional execution suffixes are appended to ARM assembly instructions to determine whether the statement will be executed

Mov r0, R1: equivalent to R0 = R1 in C language;

Movq r0, r1: if eq suffix is true, execute mov r0, r1; If eq is not true, this code is null. Similar to C language if (eq) {r0 = R1; }

Conditional suffix execution notes:

1) Whether the conditional suffix is valid depends not on the code of this sentence, but on the result of the code before this code.

2) Conditional suffixes determine whether this sentence will be executed, and do not affect whether the preceding and next sentences will be executed.

2. Compare the CMP command

• CMP instruction: Compare two operands and store the result in CPSR for the next statement

CMP R0,R1; Compare R0, R1; Sub R2, r0, r1 (r2 = r0 – R1)

CMN R0,R1; That’s the same thing as add r0, r1

tst r0, #0xf; Test whether bit0 to bit3 of R0 are all 0

Teq: TEQ is EOR (xOR) for 2 numbers.

Note: The compare instruction is used to compare numbers in two registers. The compare instruction can affect the flag bits in the CPSR without adding the s suffix.

3. Jump statement B/BL/BX

There are two ways to jump program flow in ARM programs:

• Use the specialized jump instruction B

• Write the jump address value directly to the program counter PC, which is almost necessary for any CPU. PC indicates the position of the CPU before executing the statement. Changing the value of PC is equivalent to realizing the program jump, similar to the Return statement in C language. MOV PC,LR, here you can jump in any 4G space.

• Unlike MOV PC,XXX can jump in 4G space, B statement can only jump in 32M space (because the offset is a signed 26bit value =32M)

The B instruction (Branch) indicates an unconditional jump. B. Jump to the code labeled main

The BL instruction (Branch with Link) represents a jump with a return value. Before the jump, the return address is put into the LR for return. Example: BL delay; Execute subfunction or code block delay. Delay can be a C function.

BL takes one step more than B. Before the jump, BL will save the current position in R14(i.e. LR register). When the jump code ends,LR instruction will jump back with MOV PC, which is actually the usage of C language execution function. Assembly of the tune program with BL after the execution of sub-functions, with MOV PC,LR jump back.