Write a program to find prime numbers up to value and guess the number in ARM assembly for Raspberry Pi.
Requirements and Specifications
Do two programs. You should do either 1 or 2, and then either 3 or 4. Note that all the programs require 2 loops and an if test, so to my mind the 1 and 2 are equivalent, and 3 and 4 are equivalent.
- Write a program to find prime numbers from 3 to n in a loop by dividing the number n by all numbers from 2..n/2 in an inner loop. Using the remainder (rem) operation, determine if n is divisible by any number. If n is divisible, leave the inner loop. If the limit of n/2 is reached and the inner loop has not been exited, the number is prime and you should output the number. So if the user were to enter 25, your program would print out "2, 3, 5, 7, 11, 13, 17, 19, 23".
- Write a program to prompt the user for a number, and determine if that number is prime. Your program should print out "Number n is prime" if the number is prime, and "Number n is not prime if the number is not prime. The user should be able to enter a "-1" to end the problem. It should print an error if 0, 1, 2 or any negative number other than -1 are entered.
- Write a program to allow a user to guess a random number generated by the computer from 1 to maximum (the user should enter the maximum value to guess). In this program the user will enter the value of maximum. The user will then enter guesses and the program should print out if the guess is too high or too low until the user guesses the correct number. The program should print out the number of guesses the user took.
- Write a program to guess a number chosen by the user. In this program a user will choose a secret number from 1..maximum. The program will prompt the user for the maximum value, which the user will enter. The program will then make a guess as to the value of the secret number, and prompt the user to say if the actual number is higher, lower, or correct. The computer will then guess another number until it guesses the correct secret number. The program should use a binary search to narrow its guesses to select its next guess after each attempt.
Screenshots of output
.datatitle: .asciz "This program finds the primes between 2 and n.\n\n"prompt: .asciz "Please enter the number n: "format: .asciz "%d"comma: .asciz ", "newline: .asciz "\n"n: .space 4.text.global mainmain:sub sp, sp, #4 @ save return addressstr lr, [sp]@ print program titleldr r0, =title @ load address of title messagebl printf @ print the title@ print promptldr r0, =prompt @ load address of promptbl printf @ print the prompt@ read numberldr r0, =format @ load address of number formatldr r1, =n @ load address of variable to save read numberbl scanf @ read numberldr r0, =n @ load address of numberldr r4, [r0] @ load number@ calculate primesmov r6, #2 @ start in 2loop1:cmp r6, r4 @ see if we reached nbgt endloop1 @ if so, endlsr r5, r6, #1 @ calculate n / 2mov r7, #2 @ start divisor in 2loop2:cmp r7, r5 @ compare wit n/2bgt prime @ if we reached n/2, is a primemov r0, r6 @ load current numbermov r1, r7 @ load divisorbl __aeabi_uidivmod @ divide and get remainder in r1cmp r1, #0 @ see if remainder is zerobeq nextnum @ if it's divisible by the current divisor, is not a primeadd r7, r7, #1 @ increment divisorb loop2 @ repeat inner loopprime:cmp r6, #2 @ see if it's 2beq printnum @ if so, don't print commaldr r0, =comma @ load format to print a commabl printf @ print commaprintnum:ldr r0, =format @ load format to print a numbermov r1, r6 @ load numberbl printf @ print the numbernextnum:add r6, r6, #1 @ advance to next numberb loop1 @ repeat outer loopendloop1:@ print a newlineldr r0, =newline @ load format to print a commabl printf @ print commaldr lr, [sp] @ restore return addressadd sp, sp, #4bx lr @ return to os
.datatitle: .asciz "This program guesses a number chosen by the user from 1..maximum.\n"prompt1: .asciz "\nChoose a number and enter the maximum (-1 to exit): "prompt2: .asciz "Is your number %d (Y = correct, L = lower, H = higher)?: "error: .asciz "Please enter Y for correct, L for lower or H for higher.\n"result: .asciz "The number was found in %d guesses\n"format: .asciz "%d"n: .space 4.text.global mainmain:sub sp, sp, #4 @ save return addressstr lr, [sp]@ print program titleldr r0, =title @ load address of title messagebl printf @ print the titleloop1:@ print prompt for maximumldr r0, =prompt1 @ load address of promptbl printf @ print the prompt@ read numberldr r0, =format @ load address of number formatldr r1, =n @ load address of variable to save read numberbl scanf @ read numberbl getchar @ read ending entermov r4, #1 @ minimumldr r0, =n @ load address of numberldr r5, [r0] @ load maximum numbercmp r5, #0 @ compare with zeroble endloop1 @ if <=0, end loopmov r7, #0 @ number of guesses = 0@ guess numberloop2:add r6, r4, r5 @ min + maxlsr r6, r6, #1 @ calculate midadd r7, r7, #1 @ increment guessesreadval:@ print guess and ask to validate itldr r0, =prompt2 @ load address of promptmov r1, r6 @ copy current guessbl printf @ print the prompt@ read validationbl getcharmov r8, r0bl getchar @ read ending entercmp r8, #'y' @ if correctbeq foundcmp r8, #'Y' @ if correctbeq foundcmp r8, #'l' @ if it's lowerbeq lowercmp r8, #'L' @ if it's lowerbeq lowercmp r8, #'h' @ if it's higherbeq highercmp r8, #'H' @ if it's higherbeq higher@ else print error messageldr r0, =error @ load address of error messagebl printf @ print the messageb readval @ read againlower:sub r5, r6, #1 @ use mid - 1 as maxb loop2 @ repeat loophigher:add r4, r6, #1 @ use mid + 1 as minb loop2 @ repeat loopfound:ldr r0, =result @ load format to print resultmov r1, r7 @ load number of guessesbl printf @ print the resultb loop1 @ read another maxendloop1:ldr lr, [sp] @ restore return addressadd sp, sp, #4bx lr @ return to os