-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathcodesnippets.s
124 lines (98 loc) · 2.4 KB
/
codesnippets.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// This file contains the various code
// snippets from Chapter 6. This ensures
// they compile and gives you a chance
// to single step through them.
// They are labeled, so you can set a
// breakpoint at the one you are interested in.
.global _start
.p2align 2
_start:
l1: STR X0, [SP, #-16]!
LDR X0, [SP], #16
STP X0, X1, [SP, #-16]!
LDP X0, X1, [SP], #16
l2: // … other code ...
BL myfunc
MOV X1, #4
// … more code …
B l3
myfunc: // do some work
RET
l3: // … other code ...
BL myfuncb
MOV X1, #4
// … more code …
B l4
myfuncb: STR LR, [SP, #-16]! // PUSH LR
// do some work …
BL myfuncb2
// do some more work...
LDR LR, [SP], #16 // POP LR
RET
myfuncb2: // do some work ....
RET
l4: SUB SP, SP, #16
l5: STR W0, [SP] // Store a
STR W1, [SP, #4] // Store b
STR W2, [SP, #8] // Store c
l6: ADD SP, SP, #16
l7: SUB FP, SP, #16
SUB SP, SP, #16
l8: STR W0, [FP] // Store a
STR W1, [FP, #-4] // Store b
STR W2, [FP, #-8] // Store c
ADD SP, SP, #16
l9: BL SUMFN
B l10
// Simple function that takes 2 parameters
// VAR1 and VAR2. The function adds them,
// storing the result in a variable SUM.
// The function returns the sum.
// It is assumed this function does other work,
// including other functions.
// Define our variables
.equ VAR1, 0
.equ VAR2, 4
.equ SUM, 8
SUMFN: STP LR, FP, [SP, #-16]!
SUB FP, SP, #16
SUB SP, SP, #16 // room for 3 32-bit values
STR W0, [FP, #VAR1] // save first param.
STR W1, [FP, #VAR2] // save second param.
// Do a bunch of other work, but don’t change SP.
LDR W4, [FP, #VAR1]
LDR W5, [FP, #VAR2]
ADD W6, W4, W5
STR W6, [FP, #SUM]
// Do other work
// Function Epilog
LDR W0, [FP, #SUM] // load sum to return
ADD SP, SP, #16 // Release local vars
LDP LR, FP, [SP], #16 // Restore LR, FP
RET
.macro PUSH1 register
STR \register, [SP, #-16]!
.endmacro
.macro POP1 register
LDR \register, [SP], #16
.endmacro
.macro PUSH2 register1, register2
STP \register1, \register2, [SP, #-16]!
.endmacro
.macro POP2 register1, register2
LDP \register1, \register2, [SP], #16
.endmacro
Myfunction:
PUSH1 LR
PUSH2 X20, X23
// function body …
POP2 X20, X23
POP1 LR
RET
l10:
// Setup the parameters to exit the program
// and then call Linux to do it.
MOV X0, #0 // Use 0 return code
mov X16, #1 // System call number 1 terminates this program
svc #0x80 // Call kernel to terminate the program