Step 2. Write a C function to passed two numbers to the function and calculate the sum of all the numbers between them (both inclusive). [50 marks]
a. Make a version with for loop
b. Make a version with while loop
c. Make a version with do.. while loop d. Make a version with goto loop
Tip : Try to use the same number of variables and almost the same logic in all the four loops
To do:
Using the command
$gcc -O1 -S filename.c
Will generate the assembly version of the loop. Now you need to analyze and compare them. Are these codes same or different? If different, identify the differences
//c code for a:
#include <stdio.h>
int sum_for(int a,int b)
{
int i,sum=0;
for(i=a;i<=b;i++)
{
sum += i;
}
return sum;
}
int main(void) {
int a=1,b=5;
int sum;
sum = sum_for(a,b);
printf("%d ",sum);
return 0;
}
//output for a:
//code for b:
#include <stdio.h>
int sum_while(int a,int b)
{
int i=a,sum=0;
while(i>b)
{
sum += i;
i++;
}
return sum;
}
int main(void) {
int a=1,b=5;
int sum;
sum = sum_while(a,b);
printf("%d ",sum);
return 0;
}
//Output for b:
//code for c:
#include <stdio.h>
int sum_dowhile(int a,int b)
{
int i=a,sum=0;
do
{
sum += i;
i++;
}while(i<=b);
return sum;
}
int main(void) {
int a=1,b=5;
int sum;
sum = sum_dowhile(a,b);
printf("%d ",sum);
return 0;
}
//output for c:
//After issuing the command gcc -O1 -S sum_for.c
gcc -O1 -S sum_while.c
gcc -O1 -S sum_dowhile.c
we can see three corresponding assembly language codes(sum_for.s,sum_while.s,sum_dowhile.s) created in the same directory
#NOTE: The assembly codes are automatically generated by system.If not check the answer last part for those system generated assembly codes.
Comparisons:
As we can see in line 9, line 15 for both the codes. for
loop uses termination condition jump to terminate-loop if
i is greater than given value, for the looping condition it uses
jump to loop if i is less than or equals to b.
where as while loop uses termination condition
jump to terminate-loop is i is less than given values,in looping
condition it uses jump to loop if i is less than given values.
dowhile uses looping condition in line 13, i.e go through the loop if i is less than or equal to given values.
//Assrmbly code for A: sum_for.s
.file "sum_for.c"
.text
.globl sum_for
.type sum_for, @function
sum_for:
.LFB23:
.cfi_startproc
cmpl %esi, %edi
jg .L4
movl $0, %eax
.L3:
addl %edi, %eax
addl $1, %edi
cmpl %edi, %esi
jge .L3
rep ret
.L4:
movl $0, %eax
ret
.cfi_endproc
.LFE23:
.size sum_for, .-sum_for
.section
.rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "%d "
.text
.globl main
.type main, @function
main:
.LFB24:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $15, %edx
movl $.LC0, %esi
movl $1, %edi
movl $0, %eax
call __printf_chk
movl $0, %eax
addq $8, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE24:
.size main, .-main
.ident "GCC: (Ubuntu
5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"
.section .note.GNU-stack,"",@progbits
//assembly code for B: sum_while.s
.file "sum_while.c"
.text
.globl sum_while
.type sum_while, @function
sum_while:
.LFB23:
.cfi_startproc
cmpl %esi, %edi
jle .L4
movl $0, %eax
.L3:
addl %edi, %eax
addl $1, %edi
cmpl %edi, %esi
jl .L3
rep ret
.L4:
movl $0, %eax
ret
.cfi_endproc
.LFE23:
.size sum_while, .-sum_while
.section
.rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "%d "
.text
.globl main
.type main, @function
main:
.LFB24:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $0, %edx
movl $.LC0, %esi
movl $1, %edi
movl $0, %eax
call __printf_chk
movl $0, %eax
addq $8, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE24:
.size main, .-main
.ident "GCC: (Ubuntu
5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"
.section .note.GNU-stack,"",@progbits
//assembly code for C: sum_dowhile.s
.file "sum_dowhile.c"
.text
.globl sum_dowhile
.type sum_dowhile, @function
sum_dowhile:
.LFB23:
.cfi_startproc
movl $0, %eax
.L2:
addl %edi, %eax
addl $1, %edi
cmpl %esi, %edi
jle .L2
rep ret
.cfi_endproc
.LFE23:
.size sum_dowhile, .-sum_dowhile
.section
.rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "%d "
.text
.globl main
.type main, @function
main:
.LFB24:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $15, %edx
movl $.LC0, %esi
movl $1, %edi
movl $0, %eax
call __printf_chk
movl $0, %eax
addq $8, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE24:
.size main, .-main
.ident "GCC: (Ubuntu
5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"
.section .note.GNU-stack,"",@progbits
Step 2. Write a C function to passed two numbers to the function and calculate the...
Step 1. Write a program in assembly language (using macros) to print out the following messages on the screen [20 marks]: Hello, programmers! Welcome to the world of, Linux assembly programming! Step 2. Write a C function to passed two numbers to the function and calculate the sum of all the numbers between them (both inclusive). [50 marks] a. Make a version with for loop b. Make a version with while loop c. Make a version with do.. while loop...
What to do Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways: The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void); The second version will break out of a while loop depending on whether a pair of...
Programming in C: Write a program that will help an elementary school student learn multiplication. Use the random number generator to produce two positive integers between 1 and 12 inclusive. It should then type a question such as: How much is 6 times 7? The student then types the answer. Your program checks the students’ answer. If it is correct, print a message such as Very good! If the answer is wrong, print a message such as No. Please...
C++ Program Int Main First Please Write one program that does the following: 1. 1. Ask the user for ten (10) grades, and store the data in an array. Compute the average of all the grades. Print the original ten grades and the average. a. Declare an integer array with the name of “grades” of size 10 in the main function. b. Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array. i. The function will receive...
I need help with a C++ assignment: Write a program containing the following: 1. Variable Definitions only as (DieRoll, Guess, cnt1, cnt2) followed by this statement: srand((unsigned int)time (NULL)); which will give the random number generator a random starting point. Note: srand and rand require the TIME.H (or iomanip) cnt1 and cnt2 will be used in Chapter 5 drop box as counters for loops. Do NOT create additional variables. Points will be taken off for any additional variable creation. 2....
Programming Assignment #2 EE 2372 MAKE SURE TO FOLLOW INSTRUCTIONS CAREFULLY, IF YOU HAVE ANY DOUBTS PLEASE EMAIL ME! This programming assignment will just be a small extension to programming assignment 1. In the first assignment each of the functions could only take a predefined number of inputs. This time the objective is to be able to complete these functions with any amount of inputs. The user will pass arguments through the command line (this means you will have to...
Write a C or C++ program A6p1.c[pp] that accepts two command line arguments n and m where n is an integer between 2 and 6 inclusive and m can be assumed to be a multiple of 60 (e.g. 60,120,etc). Generate a string of m random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string of m chars in place into an off-by-one lower case version (i.e. ‘A’→’b’, ‘B’→’c’, ‘C’→’d’,…, ‘Y’→’z’, ‘Z’→’a’). You should divide this conversion task among the n threads as evenly as possible. Print out the string both...
In C++, write two functions named printArray and deleteRepeats along with a main to test them according to the following specifications. Be sure to use function prototypes and place the actual function definitions after main. printArray will take two parameters: an array of characters and an integer representing the number characters stored in the array. It will print the characters from the array, labeled by their index, to the screen up to the specified size. deleteRepeats will take the same...
For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....
In python please 6 annoying_int_sequence(n) In annoying_recursion.py, write the function annoying_int_sequence(n) which takes a single integer parameter (which must be non-negative). It must return a list of intgers. The contents of the integers are defined recursively. Basically, each version of this sequence is made up of the next-smaller one, repeated n times - and with the number n in-between. For instance, the sequence for n = 3 is: ???? 3 ???? 3 ???? Just drop in the the sequence for...