so this is my combined .S file of functions, to be integrated with my .c file. go…
- User
so this is my combined .S file of functions, to be integrated with my .c file. go through this code and look for any potential errors that might lead to seg faults. .syntax unified .align 4 .type add %function .section .text .global add
add: ADD r0, r0, r1 BX lr
@subtract function .syntax unified .align 4 .type subtract %function .global subtract
subtract: CMP r0, r1 @if r0<r1, swap the values B subtract_swap
subtract_main: SUB r0, r0, r1 BX lr
subtract_swap: @swapping using EOR of two registers(like assignment 1) EOR r0, r0, r1 EOR r1, r1, r0 @r1=100 EOR r0, r0, r1 @r0=101 B subtract
@multiply function .syntax unified .align 4 .type multiply %function .global multiply
multiply: MUL r0, r0, r1 BX lr
@exponentiation function .syntax unified .align 4 .type exponentiation %function .global exponentiation
exponentiation: @r0 is base, r1 is power CMP r1, #0x0 @ Check if r1=0 BEQ exp_error_check
exp_start: PUSH {r4, lr} @ To clear r2 once loop is finished MOV r4, #0x1 @ Initialize result to 1 CMP r1, #0x0 @ Compare exponent to 0 BEQ exp_done @ If exponent is 0, return 1
exp_loop: MUL r4, r4, r0 @ Multiply result by base SUB r1, r1, #1 @ Decrement exponent by 1 CMP r1, #0x0 BNE exp_loop @ If exponent is not 0, continue loop
exp_done: MOV r0, r4 @ Move result to r0 for return POP {r4, lr} @ Clear all registers BX lr @ Return
exp_error_check: CMP r0, #0x0 @ Check if r0=0 BNE exp_start MOV r0, #0xFFFFFFFF @if 0^0 condition, error. returns -1 BX lr
@floor division function .syntax unified .align 4 .type floordivision %function .global floordivision
floordivision:
CMP r1, #0x0 @ Compare divisor to 0 BNE floor_div_start
MOV r0, #0xFFFFFFFF @ If divisor is 0, return -1 BX lrfloor_div_start: PUSH {r4, lr} @ To clear registers after returning MOV r4, #0x0 @ To store result
floor_div_loop: CMP r0, r1 @ Compare dividend to divisor BLT floor_div_done @ If dividend < divisor, break loop SUB r0, r0, r1 @ Subtract divisor from dividend ADD r4, r4, #0x1 @ Increment quotient by 1 B floor_div_loop @ Repeat loop
floor_div_done: MOV r0, r4 @ Move quotient to r0 for return POP {r4, lr} BX lr @ Return
@bitcounting function .syntax unified .align 4 .type bitcounting %function .global bitcounting
bitcounting: PUSH {r4, r5, r6, r7, lr} @Save registers and link register MOV r4, r0 @storing address of number in r4 LDR r5, [r4] @moving number into r5 MOV r7, #0x0 @counter
bitcount_loop: CMP r5, #0x0 BEQ bitcount_end AND r6, r5, #0x1 @extracting first bit in string, storing in r6 ADD r7, r7, r6 @if it is a 1, add it to the counter (its gonna be either 0 or 1) LSR r5, r5, #0x1 B bitcount_loop
bitcount_end:
MOV r0, r7 POP {r4, r5, r6, r7, lr} BX lr
@summation function .syntax unified .align 4 .type summation %function .global summation
summation: CMP r0, r1 BGT sum_swap @if r0>r1, swap BEQ sum_equal @if r0==r1, return r0 PUSH {r4, lr} @pushing register to clear them once result is returned B sum_loop
sum_equal: BX lr
sum_swap: @swapping using EOR of two registers(like assignment 1) EOR r0, r0, r1 EOR r1, r1, r0 EOR r0, r0, r1 B summation
sum_loop:
ADD r4, r4, r0 @r4=r4+r0 ADD r0, #0x1 @r0++ CMP r0, r1 @if r0!=r1, loop BLT sum_loop
ADD r4, r4, r1 @to add last number to result MOV r0, r4 POP {r4, lr} BX lr
@factorial function .syntax unified .align 4 .type factorial %function .global factorial
factorial: CMP r0, #0x0 BEQ baseCase0
factorialHelper: PUSH {r4, lr} MOV r4, r0 CMP r0, #0x1 BEQ baseCase1 SUB r0, r0, #0x1 BL factorialHelper
baseCase1: MUL r0, r0, r4 POP {r4, lr} BX LR
baseCase0: MOV r0, #0x1 BX LR
@modulus function .syntax unified .align 4 .type modulus %function .section .text .global modulus
modulus: CMP r1, #0x0 @check if dividing by zero. return -1 if yes BEQ modulus_error B modulus_loop
modulus_error: MOV r0, #0xFFFFFFFF POP {lr} BX lr
modulus_loop: CMP r0, r1 @if r0<r1 BLT modulus_end SUB r0, r0, r1 @r0=r0-r1 B modulus_loop
modulus_end: POP {lr} BX lr
this is my .c file. go through this file too, compare it with the .S file functions to look for any errors. I got a segfault when trying to run the test command. pay attention to the arguments in the test command, see if the inputs might be making any errors, also pay extra attention to bitcounting function:
#include <stdio.h> int beginProgram(); int add(int n1, int n2); int subtract(int n1, int n2); int multiply(int n1, int n2); int exponentiation(int n1, int n2); int floordivision(int n1, int n2); int bitcounting(int n); int summation(int n1, int n2); int factorial(int n); int modulus(int n1, int n2);
int main () { while (1) {
int input; printf (“Welcome to DanBurr Calcutron\n”); printf (“----------------------------\n”); printf (“Press 1 to begin and list all available commands\n”); printf (“Press 9 to exit program\n”); scanf (“%d”, &input); if (input == 1) { beginProgram (); } else if (input == 9) { printf (“Exit command executed\n\n”); break; } else continue;
}
return 0; }
int beginProgram() { while (1) { int input; printf(“Press 0 to add two numbers\n”); printf(“Press 1 to subtract two numbers\n”); printf(“Press 2 to multiply two numbers\n”); printf(“Press 3 to get exponentiation of a number\n”); printf(“Press 4 to perform floor division of two numbers\n”); printf(“Press 5 to perform bitcounting of a number\n”); printf(“Press 6 to find integer summation of two numbers\n”); printf(“Press 7 to find factorial of a number\n”); printf(“Press 8 to perform modulo division of two numbers\n”); printf(“Press 9 to go back to main screen\n”); printf(“Enter 10 for test command\n\n”); scanf(“%d”, &input);
if (input == 9) { printf(“Exit called code 9\n\n”); break; }
else if (input == 0) { int n1, n2; printf(“Enter first number: \n”); scanf(“%d”, &n1); printf(“Enter second number: \n”); scanf(“%d”, &n2); int result = add(n1, n2); printf(“The result is:%d\n\n”, result); }
else if (input == 1) { int n1, n2; printf(“Enter first (larger) number: \n”); scanf(“%d”, &n1); printf(“Enter second (smaller) number: \n”); scanf(“%d”, &n2); int result = subtract(n1, n2); printf(“The result is:%d\n\n”, result); }
else if (input == 2) { int n1, n2; printf(“Enter first number: \n”); scanf(“%d”, &n1); printf(“Enter second number: \n”); scanf(“%d”, &n2); int result = multiply(n1, n2); printf(“The result is:%d\n\n”, result); }
else if (input == 3) { int n1, n2; printf(“Enter base number: \n”); scanf(“%d”, &n1); printf(“Enter power raising the number to: \n”); scanf(“%d”, &n2); int result = exponentiation(n1, n2); if(result<0){ printf(“Illegal arguments. Try again\n\n”); continue; } else printf(“The result is: %d\n\n”, result); }
else if (input == 4) { int n1, n2; printf(“Enter larger number: \n”); scanf(“%d”, &n1); printf(“Enter number dividing the larger number by: \n”); scanf(“%d”, &n2); int result = floordivision(n1, n2); if(result<0){ printf(“Illegal arguments. Try again\n\n”); continue; } else printf(“The result is: %d\n\n”, result); }
else if (input == 5) { int n; printf(“Enter number to count bits. Number cannot exceed 32 bits: \n”); scanf(“%d”, &n); int result = bitcounting(n); printf(“The result is:%d\n\n”, result); }
else if (input == 6) { int n1, n2; printf(“Enter starting(smaller) number: \n”); scanf(“%d”, &n1); printf(“Enter ending(larger) number: \n”); scanf(“%d”, &n2); int result = summation(n1, n2); printf(“The result is:%d\n\n”, result); }
else if (input == 7) { int n; printf(“Enter positive number to find factorial. Number cannot exceed 12: \n”); scanf(“%d”, &n); int result = factorial(n); printf(“The result is:%d\n\n”, result); }
else if (input == 8) { int n1, n2; printf(“Enter larger number: \n”); scanf(“%d”, &n1); printf(“Enter number dividing the larger number by: \n”); scanf(“%d”, &n2); int result = modulus(n1, n2); if(result<0){ printf(“Illegal arguments. Try again\n\n”); continue; } else printf(“The result is: %d\n\n”, result); }
else if (input == 10) { int n1 = add(100, 199); int n2 = subtract(211999, 9876); int n3 = exponentiation(5, 5); int n4 = floordivision(2004, 5); int n5 = bitcounting(0b100101010001011110011); int n6 = summation(10, 100); int n7 = factorial(6);
printf(“100 + 199 = %d\n”, n1); printf(“211999 - 9876 = %d\n”, n2); printf(“5^5 = %d\n”, n3); printf(“floor 2004/5 = %d\n”, n4); printf(“1s in 100101010001011110011 = %d\n”, n5); printf(“sum [10,100] = %d\n”, n6); printf(“6! = %d\n”, n7); } else { printf(“Wrong input. Please try again\n\n”); continue; }
} return 0; }
- Assistant
Hi! I have spotted some potential sources of errors in your code. I'll list them down here:
- In the
bitcountingfunction in your .S file, you are not using the correct register to pass the 32-bit unsigned integer argumentn. Instead, you should be usingr0for input and output. Please change the following lines:
From:
MOV r4, r0 @storing address of number in r4 LDR r5, [r4] @moving number into r5To:
MOV r5, r0 @moving number into r5- In addition to the above, you should modify the following line in the
bitcountingfunction:
From:
AND r6, r5, #0x1 @extracting first bit in string, storing in r6To:
ANDS r6, r5, #0x1 @extracting first bit in string, storing in r6-
For the .c file, there is an issue with the string syntax. Replace all instances of
“and”with". -
Concerning tests, the
bitcountingtest has more than 32 bits which might cause issues. Please limit the test input to 32 bits or less. For example, you could change it to something like:
int n5 = bitcounting(0b11010100001011110011);After making these changes, you should recompile and test your code again. The segfaults should be resolved. Let me know if you need any more help!
- In the
- User
the input given to me to test bitcounting is this number, so how do I pass this number 11010100001011110011 as a binary number to the function ?
- Assistant
To pass the binary number 11010100001011110011, you need to make sure it is in the right format for C language. You should use the
0bor0Bprefix to denote a binary number. For example:int n5 = bitcounting(0b11010100001011110011);However, remember that the
0bor0Bbinary prefix is a non-standard feature supported as an extension by some compilers like GCC. If your compiler doesn't support it, you can convert the binary number to its equivalent hexadecimal representation and use the0xprefix instead:int n5 = bitcounting(0xD2173);Both lines above represent the same number and should work the same when passed to the
bitcountingfunction.Keep in mind that my previous comment mentioned an incorrect number of bits for this test value. This binary number has 20 bits, so it should work fine as an input for your
bitcountingfunction without causing any issues. - User
something's wrong with subtract and modulo functions. whn trying to subtract 4 from 5 to check if it swaps and the subtracts, it just hangs up. And when trying to divide 17 by 3 in modulus function, it throws segfault. go through the following .S code and .c code to resolve these errors: .syntax unified .align 4 .type add %function .section .text .global add
add: ADD r0, r0, r1 BX lr
@subtract function .syntax unified .align 4 .type subtract %function .global subtract
subtract: CMP r0, r1 @if r0<r1, swap the values B subtract_swap
subtract_main: SUB r0, r0, r1 BX lr
subtract_swap: @swapping using EOR of two registers(like assignment 1) EOR r0, r0, r1 EOR r1, r1, r0 @r1=100 EOR r0, r0, r1 @r0=101 B subtract
@multiply function .syntax unified .align 4 .type multiply %function .global multiply
multiply: MUL r0, r0, r1 BX lr
@exponentiation function .syntax unified .align 4 .type exponentiation %function .global exponentiation
exponentiation: @r0 is base, r1 is power CMP r1, #0x0 @ Check if r1=0 BEQ exp_error_check
exp_start: PUSH {r4, lr} @ To clear r2 once loop is finished MOV r4, #0x1 @ Initialize result to 1 CMP r1, #0x0 @ Compare exponent to 0 BEQ exp_done @ If exponent is 0, return 1
exp_loop: MUL r4, r4, r0 @ Multiply result by base SUB r1, r1, #1 @ Decrement exponent by 1 CMP r1, #0x0 BNE exp_loop @ If exponent is not 0, continue loop
exp_done: MOV r0, r4 @ Move result to r0 for return POP {r4, lr} @ Clear all registers BX lr @ Return
exp_error_check: CMP r0, #0x0 @ Check if r0=0 BNE exp_start MOV r0, #0xFFFFFFFF @if 0^0 condition, error. returns -1 BX lr
@floor division function .syntax unified .align 4 .type floordivision %function .global floordivision
floordivision:
CMP r1, #0x0 @ Compare divisor to 0 BNE floor_div_start
MOV r0, #0xFFFFFFFF @ If divisor is 0, return -1 BX lrfloor_div_start: PUSH {r4, lr} @ To clear registers after returning MOV r4, #0x0 @ To store result
floor_div_loop: CMP r0, r1 @ Compare dividend to divisor BLT floor_div_done @ If dividend < divisor, break loop SUB r0, r0, r1 @ Subtract divisor from dividend ADD r4, r4, #0x1 @ Increment quotient by 1 B floor_div_loop @ Repeat loop
floor_div_done: MOV r0, r4 @ Move quotient to r0 for return POP {r4, lr} BX lr @ Return
@bitcounting function .syntax unified .align 4 .type bitcounting %function .global bitcounting
bitcounting: PUSH {r4, r5, r6, r7, lr} @Save registers and link register MOV r4, r0 @storing address of number in r4 LDR r5, [r4] @moving number into r5 MOV r7, #0x0 @counter
bitcount_loop: CMP r5, #0x0 BEQ bitcount_end AND r6, r5, #0x1 @extracting first bit in string, storing in r6 ADD r7, r7, r6 @if it is a 1, add it to the counter (its gonna be either 0 or 1) LSR r5, r5, #0x1 B bitcount_loop
bitcount_end:
MOV r0, r7 POP {r4, r5, r6, r7, lr} BX lr
@summation function .syntax unified .align 4 .type summation %function .global summation
summation: CMP r0, r1 BGT sum_swap @if r0>r1, swap BEQ sum_equal @if r0==r1, return r0 PUSH {r4, lr} @pushing register to clear them once result is returned B sum_loop
sum_equal: BX lr
sum_swap: @swapping using EOR of two registers(like assignment 1) EOR r0, r0, r1 EOR r1, r1, r0 EOR r0, r0, r1 B summation
sum_loop:
ADD r4, r4, r0 @r4=r4+r0 ADD r0, #0x1 @r0++ CMP r0, r1 @if r0!=r1, loop BLT sum_loop
ADD r4, r4, r1 @to add last number to result MOV r0, r4 POP {r4, lr} BX lr
@factorial function .syntax unified .align 4 .type factorial %function .global factorial
factorial: CMP r0, #0x0 BEQ baseCase0
factorialHelper: PUSH {r4, lr} MOV r4, r0 CMP r0, #0x1 BEQ baseCase1 SUB r0, r0, #0x1 BL factorialHelper
baseCase1: MUL r0, r0, r4 POP {r4, lr} BX LR
baseCase0: MOV r0, #0x1 BX LR
@modulus function .syntax unified .align 4 .type modulus %function .section .text .global modulus
modulus: CMP r1, #0x0 @check if dividing by zero. return -1 if yes BEQ modulus_error B modulus_loop
modulus_error: MOV r0, #0xFFFFFFFF POP {lr} BX lr
modulus_loop: CMP r0, r1 @if r0<r1 BLT modulus_end SUB r0, r0, r1 @r0=r0-r1 B modulus_loop
modulus_end: POP {lr} BX lr
this is my .c file: #include <stdio.h> int beginProgram(); int add(int n1, int n2); int subtract(int n1, int n2); int multiply(int n1, int n2); int exponentiation(int n1, int n2); int floordivision(int n1, int n2); int bitcounting(int n); int summation(int n1, int n2); int factorial(int n); int modulus(int n1, int n2);
int main () { while (1) {
int input; printf (“Welcome to DanBurr Calcutron\n”); printf (“----------------------------\n”); printf (“Press 1 to begin and list all available commands\n”); printf (“Press 9 to exit program\n”); scanf (“%d”, &input); if (input == 1) { beginProgram (); } else if (input == 9) { printf (“Exit command executed\n\n”); break; } else continue;
}
return 0; }
int beginProgram() { while (1) { int input; printf(“Press 0 to add two numbers\n”); printf(“Press 1 to subtract two numbers\n”); printf(“Press 2 to multiply two numbers\n”); printf(“Press 3 to get exponentiation of a number\n”); printf(“Press 4 to perform floor division of two numbers\n”); printf(“Press 5 to perform bitcounting of a number\n”); printf(“Press 6 to find integer summation of two numbers\n”); printf(“Press 7 to find factorial of a number\n”); printf(“Press 8 to perform modulo division of two numbers\n”); printf(“Press 9 to go back to main screen\n”); printf(“Enter 10 for test command\n\n”); scanf(“%d”, &input);
if (input == 9) { printf(“Exit called code 9\n\n”); break; }
else if (input == 0) { int n1, n2; printf(“Enter first number: \n”); scanf(“%d”, &n1); printf(“Enter second number: \n”); scanf(“%d”, &n2); int result = add(n1, n2); printf(“The result is:%d\n\n”, result); }
else if (input == 1) { int n1, n2; printf(“Enter first (larger) number: \n”); scanf(“%d”, &n1); printf(“Enter second (smaller) number: \n”); scanf(“%d”, &n2); int result = subtract(n1, n2); printf(“The result is:%d\n\n”, result); }
else if (input == 2) { int n1, n2; printf(“Enter first number: \n”); scanf(“%d”, &n1); printf(“Enter second number: \n”); scanf(“%d”, &n2); int result = multiply(n1, n2); printf(“The result is:%d\n\n”, result); }
else if (input == 3) { int n1, n2; printf(“Enter base number: \n”); scanf(“%d”, &n1); printf(“Enter power raising the number to: \n”); scanf(“%d”, &n2); int result = exponentiation(n1, n2); if(result<0){ printf(“Illegal arguments. Try again\n\n”); continue; } else printf(“The result is: %d\n\n”, result); }
else if (input == 4) { int n1, n2; printf(“Enter larger number: \n”); scanf(“%d”, &n1); printf(“Enter number dividing the larger number by: \n”); scanf(“%d”, &n2); int result = floordivision(n1, n2); if(result<0){ printf(“Illegal arguments. Try again\n\n”); continue; } else printf(“The result is: %d\n\n”, result); }
else if (input == 5) { int n; printf(“Enter number to count bits. Number cannot exceed 32 bits: \n”); scanf(“%d”, &n); int result = bitcounting(n); printf(“The result is:%d\n\n”, result); }
else if (input == 6) { int n1, n2; printf(“Enter starting(smaller) number: \n”); scanf(“%d”, &n1); printf(“Enter ending(larger) number: \n”); scanf(“%d”, &n2); int result = summation(n1, n2); printf(“The result is:%d\n\n”, result); }
else if (input == 7) { int n; printf(“Enter positive number to find factorial. Number cannot exceed 12: \n”); scanf(“%d”, &n); int result = factorial(n); printf(“The result is:%d\n\n”, result); }
else if (input == 8) { int n1, n2; printf(“Enter larger number: \n”); scanf(“%d”, &n1); printf(“Enter number dividing the larger number by: \n”); scanf(“%d”, &n2); int result = modulus(n1, n2); if(result<0){ printf(“Illegal arguments. Try again\n\n”); continue; } else printf(“The result is: %d\n\n”, result); }
else if (input == 10) { int n1 = add(100, 199); int n2 = subtract(211999, 9876); int n3 = exponentiation(5, 5); int n4 = floordivision(2004, 5); int n5 = bitcounting(0b100101010001011110011); int n6 = summation(10, 100); int n7 = factorial(6);
printf(“100 + 199 = %d\n”, n1); printf(“211999 - 9876 = %d\n”, n2); printf(“5^5 = %d\n”, n3); printf(“floor 2004/5 = %d\n”, n4); printf(“1s in 100101010001011110011 = %d\n”, n5); printf(“sum [10,100] = %d\n”, n6); printf(“6! = %d\n”, n7); } else { printf(“Wrong input. Please try again\n\n”); continue; }
} return 0; }
- Assistant
I found a couple of issues with your .S file in the
subtractandmodulusfunctions. Here are the improvements you can make to fix those issues:subtractfunction: There seems to be an issue with branching back to thesubtractlabel after swapping the numbers. Instead, branch to thesubtract_mainlabel after swapping. Update the following line:
From:
B subtractTo:
B subtract_mainmodulusfunction: The issue with themodulusfunction is that you are popping the link registerlrat the wrong place. Make sure to pop it just before returning from the function. Update the following lines:
From:
POP {lr} BX lrTo:
BX lrThen, add the missing
POP {lr}just beforeBX lrin themodulus_endlabel:modulus_end: POP {lr} BX lrWith these changes, your
subtractandmodulusfunctions should work correctly.It should be noted that you might encounter issues if you provide negative input to functions that do not check for negative numbers. Make sure to add necessary checks for those functions if required.
- User
subtract works, but the modulus function is still throwing segfault error. here is the modified code for modulus:
- Assistant
I apologize for the confusion. I realize now that I missed another issue in the
modulusfunction. Here is an updated version with the changes:@modulus function .syntax unified .align 4 .type modulus %function .section .text .global modulus modulus: PUSH {lr} @ Save link register CMP r1, #0x0 @ check if dividing by zero. return -1 if yes BEQ modulus_error B modulus_loop modulus_error: MOV r0, #0xFFFFFFFF POP {lr} BX lr modulus_loop: CMP r0, r1 @ if r0 < r1 BLT modulus_end SUB r0, r0, r1 @ r0 = r0 - r1 B modulus_loop modulus_end: POP {lr} @ Restore link register BX lrThis version should fix the segfault issue for the
modulusfunction. - User
in the test command, the subtraction of 211999 - 9876 is giving the wrong result -202123
- Assistant
Thank you for pointing this out. I investigated the issue and found out that the reason for the wrong result is due to an oversight in the
subtractfunction during the swapping process. The subtraction is being performed correctly, but after swapping, the carry (C) flag is not updated, which leads to incorrect results. To fix this, add a new CMP instruction after swapping to refresh the flags.Here's the updated
subtractfunction:@subtract function .syntax unified .align 4 .type subtract %function .global subtract subtract: CMP r0, r1 @if r0<r1, swap the values B subtract_swap subtract_main: SUB r0, r0, r1 BX lr subtract_swap: @swapping using EOR of two registers(like assignment 1) EOR r0, r0, r1 EOR r1, r1, r0 @r1=100 EOR r0, r0, r1 @r0=101 CMP r0, r1 @refresh the flags after swapping B subtract_mainAfter updating the
subtractfunction with the new CMP instruction, the test command should produce the correct result for the subtraction. - User
but it shouldn't be swapping if I am providing the arguments in order 211999 and 9876, and it certainly shouldn't be throwing out a negative answer
- Assistant
You're right, I apologize for the confusion. The issue is with how the
B subtract_swapinstruction is used. It should only branch to the swap section when ther0value is less than ther1value. To achieve this, use theBLT(Branch Less Than) instruction instead of theBinstruction.Here's the updated
subtractfunction with the correction:@subtract function .syntax unified .align 4 .type subtract %function .global subtract subtract: CMP r0, r1 @if r0<r1, swap the values BLT subtract_swap subtract_main: SUB r0, r0, r1 BX lr subtract_swap: @swapping using EOR of two registers(like assignment 1) EOR r0, r0, r1 EOR r1, r1, r0 @r1=100 EOR r0, r0, r1 @r0=101 CMP r0, r1 @refresh the flags after swapping B subtract_mainNow, the test command should produce the correct result for the subtraction, and no swapping will occur for the inputs 211999 and 9876.