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...
you will write two versions of a function that calculates the sum of all the numbers in a LinkedList. In the first version, you should use an iterator and either a while loop or a traditional for loop to compute the sum . In the second version, you should use the range - based for loop. Write one or more tests in a main() function that demonstrate that either version of your function works as intended. I would personally like...
In C++ Programming, Try using loops only. This lab demonstrates the use of the While Loop and the Do While Loop as error checking mechanisms. You will be using each of these loops to solve the same problem. Please put them both in the same program. When you test the code, you will not see a difference in the way they execute - but there will be a difference in the logic when writing the code. You will want to...
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 adjacent elements (remember...
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...
MATLAB Part 1 – randFloatValue.m This function accepts two numbers, lower and upper, and returns a random number in between. The rand() function will be useful here. Part 2 – getValue.m getValue has three parameters: - prompt: A string that is displayed to the user. This should be passed to your input function. - lower: The lowest number that the user is allowed to input - upper: The highest number that the user is allowed to input This function should...
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...