+1 (315) 557-6473 

Find Prime Numbers Up To Value And Guess The Number In ARM Assembly For Raspberry Pi Assignment Solution.


Instructions

Objective
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 an ARM assignment 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
find prime numbers and guess the number for Raspberry PIfind prime numbers and guess the number for Raspberry PI 1
Source Code
Program 1
.data
title: .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 main
main:
    sub sp, sp, #4 @ save return address
    str lr, [sp]
    @ print program title
    ldr r0, =title @ load address of title message
    bl printf @ print the title
    @ print prompt
    ldr r0, =prompt @ load address of prompt
    bl printf @ print the prompt
    @ read number
    ldr r0, =format @ load address of number format
    ldr r1, =n @ load address of variable to save read number
    bl scanf @ read number
    ldr r0, =n @ load address of number
    ldr r4, [r0] @ load number
    @ calculate primes
    mov r6, #2 @ start in 2
loop1:
    cmp r6, r4 @ see if we reached n
    bgt endloop1 @ if so, end
    lsr r5, r6, #1 @ calculate n / 2
    mov r7, #2 @ start divisor in 2
loop2:
    cmp r7, r5 @ compare wit n/2
    bgt prime @ if we reached n/2, is a prime
    mov r0, r6 @ load current number
    mov r1, r7 @ load divisor
    bl __aeabi_uidivmod @ divide and get remainder in r1
    cmp r1, #0 @ see if remainder is zero
    beq nextnum @ if it's divisible by the current divisor, is not a prime
    add r7, r7, #1 @ increment divisor
    b loop2 @ repeat inner loop
prime:
    cmp r6, #2 @ see if it's 2
    beq printnum @ if so, don't print comma
    ldr r0, =comma @ load format to print a comma
    bl printf @ print comma
printnum:
    ldr r0, =format @ load format to print a number
    mov r1, r6 @ load number
    bl printf @ print the number
nextnum:
    add r6, r6, #1 @ advance to next number
    b loop1 @ repeat outer loop
endloop1:
    @ print a newline
    ldr r0, =newline @ load format to print a comma
    bl printf @ print comma
    ldr lr, [sp] @ restore return address
    add sp, sp, #4
    bx lr @ return to os
Program 2
.data
title: .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 main
main:
    sub sp, sp, #4 @ save return address
    str lr, [sp]
    @ print program title
    ldr r0, =title @ load address of title message
    bl printf @ print the title
loop1:
    @ print prompt for maximum
    ldr r0, =prompt1 @ load address of prompt
    bl printf @ print the prompt
    @ read number
    ldr r0, =format @ load address of number format
    ldr r1, =n @ load address of variable to save read number
    bl scanf @ read number
    bl getchar @ read ending enter
    mov r4, #1 @ minimum
    ldr r0, =n @ load address of number
    ldr r5, [r0] @ load maximum number
    cmp r5, #0 @ compare with zero
    ble endloop1 @ if <=0, end loop
    mov r7, #0 @ number of guesses = 0
    @ guess number
loop2:
    add r6, r4, r5 @ min + max
    lsr r6, r6, #1 @ calculate mid
    add r7, r7, #1 @ increment guesses
readval:
    @ print guess and ask to validate it
    ldr r0, =prompt2 @ load address of prompt
    mov r1, r6 @ copy current guess
    bl printf @ print the prompt
    @ read validation
    bl getchar
    mov r8, r0
    bl getchar @ read ending enter
    cmp r8, #'y' @ if correct
    beq found
    cmp r8, #'Y' @ if correct
    beq found
    cmp r8, #'l' @ if it's lower
    beq lower
    cmp r8, #'L' @ if it's lower
    beq lower
    cmp r8, #'h' @ if it's higher
    beq higher
    cmp r8, #'H' @ if it's higher
    beq higher
    @ else print error message
    ldr r0, =error @ load address of error message
    bl printf @ print the message
    b readval @ read again
lower:
    sub r5, r6, #1 @ use mid - 1 as max
    b loop2 @ repeat loop
higher:
    add r4, r6, #1 @ use mid + 1 as min
    b loop2 @ repeat loop
found:
    ldr r0, =result @ load format to print result
    mov r1, r7 @ load number of guesses
    bl printf @ print the result
    b loop1 @ read another max
endloop1:
    ldr lr, [sp] @ restore return address
    add sp, sp, #4
    bx lr @ return to os