A, goals,

When I was in primary school, Chinese was the first to learn population hand; Math is counting, then adding, subtracting, multiplying and dividing.

Today’s goal is addition, subtraction, multiplication and division in ARM assembly

Second, the steps

Pseudo code

int add(int a, int b) {
    return a + b;
}

int calc(int a, int b, int c, int d) {
    int e = add(a-b, c-d);
    return e * a;
}
Copy the code

Let’s calculate in our head calc(3,2,4,3) [(3-2)+(4-3)] * 3 = 6

Assembly code

.text .globl _start _start: Mov r0, $3 mov r1, $2 mov r2, $4 mov r3, // add r0,r0,$0x30 // add the result of calc to 0x30. The ASCII value used to display such as 1 is 1+0x30 = 49 LDR R1, Mov r0, $1 // fd 1 (stdout) LDR r1, $1 // fd 1 (stdout) LDR r1, =message mov r2, $message_len mov r7, $4 // syscall 4 (write) swi $0 mov r0, $0 // exit status 0 (ok) mov r7, $1 // syscall 1 (exit) swi $0 _add: add r0, r0, r1 // r0 = r0 +r1 bx lr _calc: Mov r4, r0 // r4 = a mov r5, r1 // r5 = b sub r0, r0, r1 // r0 = a sub R0, r0, r1 // r0 = a-b sub R1, r2, R0, r0 = e * a pop {r4, r5, r0 = e * a pop {r4, r5, r0 = e * a pop {r4, r5, r0 = e * a pop {r4, r5, r0 = e * a pop PC} // restore the register context and return.data message:.ascii "0000\n" message_len =. -messageCopy the code

On the blackboard

Calc program inside a total of BL STR add SUB and MUL several instructions

  • bl

The unconditional jump instruction, similar to GOto in C language, can also be used to call functions

  • str

Data storage instruction

  • add

Add instruction

  • sub

Subtraction instructions

  • mul

Multiplication instructions

compile

Compile link plus run, several commands, a knock is obviously not in line with our senior programmer’s identity

#! /bin/bash CmdPath = "/ Users/fenfei/Library/Android/SDK/the NDK / 21.3.6528147 / toolchains/arm - Linux - androideabi - 4.9 / prebuilt/Darwin - x86_64 / bin" name="calc" $cmdPath/arm-linux-androideabi-as -o $name.o $name.S $cmdPath/arm-linux-androideabi-ld -o $name $name.o  adb push $name /data/local/tmp/$name adb shell chmod +x /data/local/tmp/$name adb shell /data/local/tmp/$nameCopy the code

Run the

2. To bounce lightly over. 2. 2 MB/s (976 bytes in 0.001s) 6Copy the code

The result is the same as our mental calculation 6

Third, summary

Boss Li: What about addition, subtraction, multiplication and division? In addition to?

Fenfei: The division of this ARM is more complicated, not a command can be done……

In fact, ARM assembly is to be careful and patient, do not see the assembly on the head, a careful analysis and other languages similar, or even more rigid.

Working in the right direction can make you good. That’s certainty. Working hard but not necessarily achieving a career is called uncertainty.

TIP: The purpose of this article is only one is learning more backward techniques and train of thought, if anyone use this technology to get illegal commercial interests the legal liabilities are their operators, and the author and it doesn’t matter, this paper involves the knowledge of code project can go to my friends to fly star come undone, welcome to join star learn together to explore technology knowledge. Have a problem can add me WX: FENfei331 discussion.

Follow the wechat public account Fenfei safety, the latest technology dry goods real-time push