-
Notifications
You must be signed in to change notification settings - Fork 0
/
bstinsert.S
42 lines (42 loc) · 891 Bytes
/
bstinsert.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
.globl bstinsert
bstinsert:
SUB SP,SP, #32 // Make room for 3 registers on stack
MOV X10, #0
STUR X10, [SP, #16] // right = nullptr
STUR X10, [SP, #8] // left = nullptr
STUR X1, [SP, #0] // data in stack
MOV X12, SP // new node address
mov X9, X0 // X9 = root = x
mov X10, #0 // y = null
B while
while:
cmp X9, #0 // if (x != null)
b.eq entwhile
mov X10, X9 // y = x
LDUR X13,[X10, #0] // X13 = x->key
cmp X1, X13
b.ge goright
// else going left
LDUR X9, [X9, #8] // x = x->left
B while
goright:
LDUR X9, [X9, #16] // x = x->right
B while
entwhile:
cmp X10, #0 // if (y == null)
b.eq ynull
LDUR X11,[X10, #0] // X11 = y->key
cmp X1,X11 // data < y->key
b.ge yright
// else going left
STUR X12, [X10, #8] // y->left = newnode
b done
ynull:
MOV X10, X12 // y = newnode
b done
yright:
STUR X12,[X10, #16] // y->right = newnode
b done
done:
MOV X0, X12
BR X30