	.arch armv6
	.eabi_attribute 27, 3
	.eabi_attribute 28, 1
	.fpu vfp
	.eabi_attribute 20, 1
	.eabi_attribute 21, 1
	.eabi_attribute 23, 3
	.eabi_attribute 24, 1
	.eabi_attribute 25, 1
	.eabi_attribute 26, 2
	.eabi_attribute 30, 2
	.eabi_attribute 34, 1
	.eabi_attribute 18, 4
	.file	"Prog5.c"
	.global	__aeabi_uidivmod
	.section	.text.startup,"ax",%progbits
	.align	2
	.global	main
	.type	main, %function
main:
	@ args = 0, pretend = 0, frame = 40
	@ frame_needed = 0, uses_anonymous_args = 0
	mov	r3, #0
    
    @ Save registers 
	stmfd	sp!, {r4, r5, r6, r7, lr}
	mov	r5, r3
	sub	sp, sp, #44
	mov	r6, r3
	mov	r7, #1  @ Set up a 1-constant ahead of time
	str	r3, [sp]
	str	r3, [sp, #4]
	str	r3, [sp, #8]
	str	r3, [sp, #12]
	str	r3, [sp, #16]
	str	r3, [sp, #20]
	str	r3, [sp, #24]
	str	r3, [sp, #28]
	str	r3, [sp, #32]
	str	r3, [sp, #36]
.L6:
    @ Inlined checkNum
	cmp	r5, #2
	ble	.L21
	tst	r5, #1
	beq	.L3
	mov	r4, #2
	b	.L5
.L23:
	bl	__aeabi_uidivmod
	cmp	r1, #0
	beq	.L4
.L5:
    @ if divisor < toTest, plus prep for divmod
	add	r4, r4, #1
	cmp	r4, r5
	mov	r0, r5
	mov	r1, r4
	bne	.L23
.L4:
    @ if (divisor == toTest)
	cmp	r5, r4
.L21:
	bne	.L3  @ Fall through from ble .L21 will branch
    
    @ inlined setBit (uses r7 = 1, and does numPrimes++ too)
	mov	r1, r5, lsr #5
	and	r3, r5, #31
	ldr	r2, [sp, r1, asl #2]
	add	r6, r6, #1  @ Sneak in an increment of totalPrimes...
	orr	r3, r2, r7, asl r3
	str	r3, [sp, r1, asl #2]
.L3:
    @ toTest++, and test of toTest < numToTest
	add	r5, r5, #1
	cmp	r5, #320
	bne	.L6  @ Go around for another loop
    
    @ printf("Found %d primes:", totalPrimes);
	mov	r1, r6
	ldr	r0, .L25
	bl	printf
    
    @ r4 now holds toTest; toTest = 0
	mov	r4, #0
	b	.L8
.L7:
    @ toTest++ < numsToTest
	add	r4, r4, #1
	cmp	r4, #320
	beq	.L24
.L8:
    @ Loop body of print-loop
	mov	r3, r4, asr #5
	add	r2, sp, #40        @ r2 <- primeSet
	add	r3, r2, r3, asl #2 @ r2 <- primeSet + toTest/bitsPerInt
	and	r1, r4, #31
	ldr	r2, [r3, #-40]
	mov	r3, r2, lsr r1     @ r3 <- r2 >> toTest % bitsPerInt
	tst	r3, #1
	beq	.L7
    
    @  printf(" %d", toTest);
	mov	r1, r4
	ldr	r0, .L25+4
	add	r4, r4, #1
	bl	printf
	cmp	r4, #320
	bne	.L8
    
.L24:
	mov	r0, #10      @ r0 = '\n'
    
    @ Unwind stack frame, but *don't* return!
	add	sp, sp, #44
	ldmfd	sp!, {r4, r5, r6, r7, lr}
    
    @ Tricky putchar call gets existing lr as if our caller called it
	b	putchar
    
.L26:
	.align	2
.L25:
	.word	.LC0
	.word	.LC1
	.size	main, .-main
	.text
	.align	2
	.global	checkNum
	.type	checkNum, %function

checkNum:
    @ Pretest toTest for < 2, which skips loop
	cmp	r0, #2
    
    @ Save R3-R7 along with lr, so we can use them
	stmfd	sp!, {r3, r4, r5, r6, r7, lr}
    
    @ Shunt toTest and primeSet into R5/R7
	mov	r5, r0
	mov	r7, r1
    
    @ Skip loop if toTest <= 2 (yes, <= for unsigned)
	bls	.L32
    
    @ If toTest & 1.  Skip loop if toTest is even.  Loop unrolling
	tst	r0, #1
	beq	.L29
    
    @ r4 <- 2 (R4 is divisor)
	mov	r4, #2
	b	.L30
    
.L36:
    @ Compute toTest/divisor
	bl	__aeabi_uidivmod
    
    @ If result is 0, jump out of loop
	cmp	r1, #0
	beq	.L28
.L30:
    @ divisor < toTest
	add	r4, r4, #1
	cmp	r4, r5
    
    @ Precopy toTest to r0 and divisor to r1 and r6
	mov	r0, r5  @ Params for upcoming divmod call
	mov	r1, r4  
	mov	r6, r4  @ Save current divisor if we exit loop
	bne	.L36
    
.L28: @ Full postloop code, check if we found an even divisor
    @ if (toTest == divisor)
	cmp	r5, r6
	bne	.L31
    
    @ setBit body inline.  Note use of R0-R3 as scratch
    @ .. but r5 and r7 as "parameters" are not altered
	mov	r1, r5, lsr #5
	and	r3, r5, #31
	ldr	r2, [r7, r1, asl #2]
	mov	r0, #1
	orr	r3, r2, r0, asl r3
	str	r3, [r7, r1, asl #2]
    
.L31: @ Value-return code only.  Returns toTest == divisor w/o a branch!
	rsb	r0, r6, r5     @ r0 <- toTest - divisor
	clz	r0, r0         @ 32 iff toTest == divisor
	mov	r0, r0, lsr #5 @ Return 1 only if r0 was 32
    
	ldmfd	sp!, {r3, r4, r5, r6, r7, pc}

@ Special loop terminations, put after "return" but jumped to ahead of it.
@ 
.L32: @ Hardwire divisor to 2, and do post-loop test since toTest may be 2.
	mov	r6, #2
	b	.L28

@ Even case.  Hardwire divisor to 2, assume no setBit
.L29:
	mov	r6, #2
	b	.L31
	.size	checkNum, .-checkNum
	.align	2
	.global	setBit
	.type	setBit, %function
    
setBit:
	@ No need for stack frame
    @ r2 <- prime/32
	mov	r2, r0, lsr #5 
	
    mov	ip, #1
    
    @ load from primeSet + prime/32 * 4
	ldr	r3, [r1, r2, asl #2]
    
    @ toPrime <- toPrime % 32
	and	r0, r0, #31
    
    @ r0 <- primeSet[toPrime % 32] | 1 << toPrime % 32
	orr	r0, r3, ip, asl r0
    
    @ Store back to same location
	str	r0, [r1, r2, asl #2]
    
    @ Use lr directly to return
	bx	lr
	.size	setBit, .-setBit
	.section	.rodata.str1.4,"aMS",%progbits,1
	.align	2
    
.LC0:
	.ascii	"Found %d primes:\000"
	.space	3
.LC1:
	.ascii	" %d\000"
	.ident	"GCC: (Raspbian 4.9.2-10) 4.9.2"
	.section	.note.GNU-stack,"",%progbits
