I am writing a main.s program in ARM assembly and running it together with a checkPrimeNumber.c program . The program prompts the user to enter two positive numbers and output the prime numbers between them. For example if you enter 2 and 6 the prime numbers that would output would be 3 and 5. The checkPrimeNumber function is called in main to check whether a given integer n is a prime number or not. I am running into an issue were if I enter two numbers, I get the value of 1 each time I try and run the program.
I ran the program in the debugger and fixed some of the registers so that they were not taking up space in the program, but that did not change the result. I am unsure if this is a memory allocation issue or if I did not do the comparison correctly in my program. For reference, I have provided my main.s code and the checkPrimeNumber.c code
main.s
.cpu cortex-a53
.fpu neon-fp-armv8
@ Data section
.data
inp1: .asciz "Enter two positive numbers"
inp2: .asciz "%d %d"
outp: .asciz "Prime numbers between %d and %d are: %d\n "
.balign @ Align the data section to 4 bytes
n1: .word 0
n2: .word 0
i: .word 0
flag: .word 0
.text
.align 2 @ Align the code section to 2 bytes
.global main @ Declare 'main' as a global symbol
.type main, %function @ Specify the type of the 'main' symbol as a function
main:
push {fp, lr} @ Save the frame pointer (fp) and link register (lr) to the stack
add fp, sp, #4 @ Set the frame pointer (fp) to the current stack pointer (sp)
@ Print the prompt "Enter two positive numbers"
ldr r0, =inp1
bl printf
@ Scanf ("%d %d", &n1, &n2)
ldr r0, =inp2
ldr r1, =n1 @ Load the memory address of n1 into r1
ldr r2, =n2 @ Load the memory address of n2 into r2
bl scanf
@ Initialize flag = 1 (to assume that numbers are prime)
ldr r0, =flag
mov r5, #1
str r5, [r0]
@ Load the input values from memory into r1 and r2
ldr r1, =n1 @ Load the value of n1 into r1
ldr r8, [r1] @ Load the content of the memory address pointed by r1 into r1
ldr r2, =n2 @ Load the value of n2 into r2
ldr r6, [r2] @ Load the content of the memory address pointed by r2 into r2
@ Initialize i = n1 + 1
add r3, r8, #1
forloop: @ Loop label (start of the loop)
@Check if i < n2
cmp r3, r2 @ Compare i with n2
bgt done @ If i is greater than or equal to n2, exit the loop
@ Call the function checkPrimeNumber(i) and store the result in 'flag'
mov r0, r3 @ Move the current value of i to r0 (the function argument)
bl checkPrimeNumber
@ Check if flag == 1 (if the number is prime)
cmp r0, #1
bne increment_num @ If not equal to 1, go to increment_num label and proceed to the next iteration
@ Print the output statement "Prime numbers between %d and %d are: "
ldr r0, =outp @ print out end prompt
mov r1, r8 @ Move the first range value (n1) to r1 (for printf)
mov r2, r6 @ Move the second range value (n2) to r2 (for printf)
mov r4, r3 @move the resulting prime numbers (i) to r4 (for printf)
bl printf
increment_num: @ Increment the loop variable (i) and continue to the next iteration
add r3, r3, #1
b forloop @ Branch back to the forloop label
done:
sub sp, fp, #4 @ Restore the stack pointer (sp) to its original value
pop {fp, pc} @ Restore the frame pointer (fp) and program counter (pc) to return from the function
checkPrimeNumber.c
int checkPrimeNumber(int n);
int main(void) {
int n1, n2, i, flag;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("Prime numbers between %d and %d are: ", n1, n2);
for (i = n1 + 1; i < n2; ++i) {
// i is a prime number, flag will be equal to 1
flag = checkPrimeNumber(i);
if (flag == 1)
printf("%d ", i);
}
return 0;
}