-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.s
135 lines (85 loc) · 1.76 KB
/
main.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
125
126
127
128
129
130
131
132
133
.set EXIT, 1
.set WRITE, 4
.set OPEN, 5
.set CLOSE, 6
.set BRK, 45
// Constants
.set O_WRONLY, 1
.set O_CREAT, 64
.set O_TRUNC, 512
.set CREAT_FLAGS, (O_WRONLY | O_CREAT | O_TRUNC)
.set MEM_BLOCK, 0x1000
.balign 2
.section .rodata
prime_message:
.ascii "Found a prime\n"
.set MESSAGE_LEN, .-prime_message
newline:
.ascii "\n"
filename: .asciz "primes.bin"
.set NAMESIZE, .-filename -1
.text
.global _start
_start:
// open file
movw r0, #:lower16:filename
movt r0, #:upper16:filename
movw r1, #CREAT_FLAGS
movw r2, #0644
mov r7, #OPEN
svc #0
mov r9, r0 // file handle in r9
// get some memory
bl get_initial
mov r10, r0 // location in memory to write to
mov r5, r0 // start of known primes
mov r0, #MEM_BLOCK
bl get_more
mov r11, r0 // final location
mov r0, #3
str r0, [r5] // Seed known primes
add r10, #4
// initialize parameters
mov r6, #0x00Ec0000 // upper bound
//mov r6, #0x100
mov r4, #5 // lower bound
loop:
mov r0, r4
mov r1, r5
bl is_prime
mov r8, r0
cmp r0, #1
moveq r0, r4
beq found_prime
test:
add r4, r4, #2
cmp r6, r4
bls after
b loop
after:
sub r2, r11, #MEM_BLOCK
sub r2, r10, r2
bl writing // write whatever is left
// close the file
mov r0, r9
mov r7, #CLOSE
svc #0
mov r7, #EXIT
svc #0
found_prime:
str r0, [r10], #4
cmp r10, r11
moveq r2, #MEM_BLOCK // write a full memory block of primes
bleq writing
b test
writing:
push {r1, lr}
mov r10, r11
sub r1, r11, #MEM_BLOCK
mov r0, r9
mov r7, #WRITE
svc #0
mov r0, #MEM_BLOCK
bl get_more
mov r11, r0
pop {r1, pc}