-
Notifications
You must be signed in to change notification settings - Fork 1
/
bubble_sort.a816
113 lines (90 loc) · 1.91 KB
/
bubble_sort.a816
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
.include "SW32VM.inc"
.autoimport
.export init, sw32_print_char
.macro accu8
SEP #%00100000
.A8
.endmacro
.macro accu16
REP #%00100000
.A16
.endmacro
sw32_print_char:
.A8
.I16
; Custom Printing code here
RTL
init:
.A8
.I16
LDA #$0A
JSL sw32_print_char ; Newline
JSL print_array ; Print the unsorted Array
LDA #$80 ; Enable 24-bit Addressing
JSL sw32_init
LDX # .LOWORD(sw32_sort) ; Low Word of the Program Address
LDY # .HIWORD(sw32_sort) ; High Word of the Program Address
JSL sw32_execute ; Run the Program
JSL print_array ; Print the Sorted Array
RTL
print_array:
LDA #$0A
JSL sw32_print_char ; Newline
LDX # (element_count * 4)
@loop:
DEX
DEX
DEX
DEX
accu16
PHX
LDA f:array_ptr+2,X
PHA
LDA f:array_ptr,X
PHA
accu8
JSL sw32_print_h32
LDA #$0A
JSL sw32_print_char ; Newline
PLX
PLX
PLX
BNE @loop
LDA #$0A
JSL sw32_print_char ; Newline
RTL
.data
element_count = 10
array_ptr:
.word 9, 0
.word 3, 0
.word 1, 0
.word 8, 0
.word 6, 0
.word 4, 0
.word 0, 0
.word 3, 0
.word 7, 0
.word 8, 0
.data
sw32_sort:
LLI R15, #array_ptr ; Get the Address of the Array to be sorted
LWI R14, #element_count-1 ; Get the amount of elements in the Array (minus 1)
@outer_loop:
CLR SR, F7 ; Clear the "swapped" Flag
LWI R14, #element_count-1 ; Make a Copy of the element count
ORR R13, ZERO, ZERO ; Set the Index to 0
@inner_loop:
LL R1, (R15, R13, 0) ; Load a Value from the Array
LL R2, (R15, R13, 4) ; And Get a second Value from the Array+1
SBR R0, R2, R1 ; Compare them (R1 - R2)
BNC @no_swap ; If R1 > R2, swap them
SL (R15, R13, 4), R1 ; Store R1 to the Array+1
SL (R15, R13, 0), R2 ; Store R2 to the Array
SET SR, F7 ; And Set the "swapped" Flag
@no_swap:
ADI R13, #4 ; Increment the Index
ADI R14, #-1 ; Decrement the Element counter
BNZ @inner_loop
BF7 @outer_loop
EXIT