About the author: Hello everyone, I'm IM Tom Kate, you can call me Tom
Personal homepage: IM Tom Kate's CSDN blog
series column: [ARM Embedded Foundation]
Daily sentence:To anyone who has, he will be given double to make him redundant; to anyone who does not have, even what he has will be taken away - "New Testament, Gospel of Matthew"
write in front
As we all know, the direct bottom layer of C language belongs to assembly. Assembly is not only the basis of C language, but also the cornerstone of computer operation.
Since C and assembly are so close, we must learn to assemble and call functions in C language to facilitate us to achieve more functions.
Today I will show you how to call the scanf function in ARM assembly language.
We still use the example of "inputting two numbers from the keyboard, summing and outputting" to learn
The scanf function is used here. The definition of scanf is: input data into the specified variable from the keyboard in the format specified by the user.
Usage of scanf in ARM
1. In the ARM assembly, the data input by scanf is stored in the memory. If there is a variable defined in the memory, it will be stored in the variable. If it is not defined, it will be stored at an address randomly, so it is necessary to define where to store it in the memory first.
2. Like printf, you can directly use BL scanf to call, but every time you use BL to jump, you must redefine R0~R3 once, and you can only use R1~R3 to pass parameters
Knowing the usage of scanf in ARM, let's take you to write "Enter two numbers from the keyboard, sum and output" ARM assembly.
The first step is to get the framework out
- Introduced earlier, the basic framework of assembly, [ ARM assembly basic framework and the sum of two numbers ]
- Here is a new programming framework for ARM assembly, which is the same as the previous one. Push and pop are used to simply replace stack push and pop.
.data
………………
.text
.globl main
main:
push {lr}
………………
mov r0, #0
pop {lr}
mov pc, lr
.end
The second step is to declare global variables and symbolic constants
- 1. First declare the input and output formats of printf and scanf
- 2. Since you want to call scanf to input two numbers, you must first define two variables in memory
.data
fmt:.asciz "\n sum=%d\n"
fmt1:.asciz "%d %d"
a:.word 0
b:.word 0
The third step, enter two numbers and store them in memory
- When passing parameters, only R0~R3 can be used. When calling scanf, the address of the variable in the memory to store the input data must be fetched into the register first.
ldr r0, =fmt1
ldr r1,=a
ldr r2,=b
bl scanf
The fourth step, read data from memory
- As mentioned in the previous article, fetching data from memory requires two steps, first fetching the address through the LDR pseudo-instruction, and then passing the value to the register for operation through indirect addressing.
ldr r1,=a
ldr r1,[r1]
ldr r2,=b
ldr r2,[r2]
The fifth step, operation and output
add r2,r1
ldr r0,=fmt
mov r1,r2
bl printf
R0~R15 are essentially aliases for addresses. It should be noted here that the values in R0~R3 will change every time the B instruction is called, so it must be re-assigned
The results of the operation will not be shown to everyone. You can try to type. If there is any problem, you can tell me through the comment area.
Finally, use the implementation of "input three numbers and then sum input" to verify whether we have learned it!
Source code:
.data
fmt:.asciz "\n sum=%d\n"
fmt1:.asciz "%d %d %d"
a:.word 0
b:.word 0
c:.word 0
.text
.globl main
main:
stmfd sp!,{lr}
ldr r0, =fmt1
ldr r1,=a
ldr r2,=b
ldr r3,=c
bl scanf
ldr r1,=a
ldr r1,[r1]
ldr r2,=b
ldr r2,[r2]
ldr r3,=c
ldr r3,[r3]
add r2,r1
add r3,r2
ldr r0,=fmt
mov r1,r3
bl printf
mov r0,#0
ldmfd sp!,{lr}
mov pc,lr
.end
result:
This issue is over. If it is helpful to you, please like + comment to support the blogger and go.
Don't follow Tom's friends yet, click to follow and learn a little compilation every day
Next notice: [ Cycle application of ARM assembly] Forward traversal | Reverse traversal