Project #3 This project will combine C++ and assembly programming. As we’ve done before, we’ll use C-strings (arrays of chars that mark the end of the “string” with a 0) rather than C++ string...

1 answer below »
This program will combine C++ and assembly language.


Project #3 This project will combine C++ and assembly programming. As we’ve done before, we’ll use C-strings (arrays of chars that mark the end of the “string” with a 0) rather than C++ string objects. We’ll incorporate those into a structure data type. We’ll be sending an array of structures to all the functions, so keep in mind that when you send an array as a parameter, the function is actually receiving an address. Open up the Word document Project3 starter code.docx that I included with the project. Please copy that into your project’s source code, and don’t modify it. Let’s look at the individual functions: getData void getData(Student []); This function receives an array of Student structures. The data to fill these structures is in a file named “student_data.txt” which is attached to this assignment. I’m supplying the code for this function, because it uses an ifstream function that you might not have seen before. It’s handy, because it reads in a string as a char array (just what we need) and even puts the terminating zero where it goes! As long as the “student_data.txt” file is in the same folder as your source code, this function should work as written. print void print(Student[], Student); Please look at the file named “output.txt” that is attached to this assignment. It shows the exact output that your program should create. Its parameters are the array of structures and a single structure that holds data for the student with the highest average. findAverages void findAverages(Student[]); This function receives an array of Student structures. It’s task is to get the average of the four tests for each student, and store that average into the average field in that student’s structure. Suggestions for this function: a. Don’t use the lea instruction; instead, mov the parameter into a general-purpose register b. You can declare no more than three local variables: two ints and a float c. Base-index addressing works well here, like [esi + edx].test1 for example; if you’ve got esi and edx set up correctly, you can address fields as shown here. You could use other general-purpose registers for the job, also. The C++ compiler creates some problems for addressing as we would in an all-assembly program. There’s no need to use the PTR operator. d. You’re going to be using the FPU to divide. When you store the result, use the fstp instruction rather than just fst. The fstp instruction means “store result and pop”, and it cleans up the FPU stack after it stores the result where you tell it to. It will prevent the FPU stack from getting used up (it only has 8 registers, don’t forget) Project #3 determinePassing void determinePassing(Student[]); This is a void function that receives the array of Student structures. Its job is to determine whether or not each student is passing, based on his/her average. For this program, if the average is 60 or higher, the student is passing (in which case, make the passing field true, that is, 1). For an average of less than 60, the passing field should be false (0). Suggestions for this function: a. Pretty much the same as the previous function in terms of addressing, etc. b. You’re going to be using the FPU to compare the float averages to 60. After doing so, execute this instruction two times: fstp ST(0) This will clear data off of the FPU stack; you might get some near-impossible-to-track errors if you don’t. getHighestStudent void getHighestStudent(Student[], Student &); This function will probably take you the longest, so you might want to save it for last (after you’ve gotten the addressing thing down). If you look in main(), you’ll see that I declared an individual Student structure that will contain the info for the student with the highest average. I initialized that structure with a name (“Nobody”) and an average of 0. The function receives the address of that structure (notice that the parameter is a reference parameter) in addition to the array of Student structures. The idea is to loop through the array, and do the “King of the Mountain” algorithm with each student’s average. The trick is, as you find each new “King”, you’ll have to copy its data into the individual structure. That way, the individual structure will always contain the data for the highest-so-far student. Suggestions for this function: a. Pretty much the same as the previous functions in terms of addressing, etc. b. Once more, you’re going to be using the FPU to compare the float averages. After doing so, execute this instruction two times: fstp ST(0) c. Warning: it will be really easy to trash out registers in this function, especially the ecx and the esi (if you’re using it in base-index addressing). Why? You’ll be using both of them to copy the studentName field from one structure to another. Additional specs 1. I STRONGLY suggest writing one function at a time. The order of the prototypes is the suggested order. So, write a function and comment out everything in main() except where the function is being tested. Get each one working before going on to the next. Writing the whole program before testing any of it is not the best approach. 2. Please put a program heading at the top of your program; here is the form: /* ------------------------------------------------------------ Programmer : Bill Gates Date : April 30, 2020 Description : This program performs several functions with an array of Student records. This program should work with any size array ------------------------------------------------------------ */ Project #3 3. Please put function headings above all the functions (not the prototypes); here is the form: /* ------------------------------------------------------------ Function name Description of what it does Receives: what parameters does it get, and what do they represent? Returns: what does the return value represent? Requires: Any preconditions? Anything that must be true for this function to work? ------------------------------------------------------------ */ void getNums(unsigned int nums[]) { //code here 4. Feel free to use these registers: eax, ebx, ecx, edx, esi, and edi. 5. Figure on the entire program taking 250 or so lines of code counting headings, spacing for readability, etc. 6. There will be a forum in Discussions for questions about this project. Any posts should clear up questions about what the project is supposed to do, things I might have overlooked, etc. It’s not a place to share solutions. 7. Please copy/paste your source code into a Word document named in this form: Lastname, Firstname – Project 3.docx #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using namespace std; const int ARRAY_SIZE = 12; //there are 12 records in the file const int NAME_SIZE = 25; //all names are < 25="" chars="" struct="" student="" {="" char="" studentname[name_size];="" unsigned="" int="" test1,="" test2,="" test3,="" test4;="" float="" average;="" bool="" passing;="" };="" void="" getdata(student[]);="" loads="" the="" array="" with="" data="" from="" file="" void="" print(student[],="" student);="" show="" results="" void="" findaverages(student[]);="" gets="" each="" student's="" avg="" based="" on="" 4="" tests="" void="" determinepassing(student[]);="" passing="" is="" an="" average="">= 60 void getHighestStudent(Student[], Student &); //finds student with highest average int main() { Student theClass[ARRAY_SIZE]; Student highest = { {"Nobody"}, 0,0,0,0,0,0 }; //this strucure will hold top student's info getData(theClass); findAverages(theClass); determinePassing(theClass); getHighestStudent(theClass, highest); //NOTE: highest is a REFERENCE parameter! print(theClass, highest); } void getData(Student theClass []) { ifstream infile; char temp[NAME_SIZE]; //temporary array to read in names infile.open("student_data.txt"); if (infile) { for (int x = 0; x < array_size;="" x++)="" {="" infile.getline(temp,="" name_size);="" workaround="" to="" read="" in="" array="" of="" chars="" strcpy(theclass[x].studentname,="" temp);="" instead="" of="" a="" string="" infile="">> theClass[x].test1; infile >> theClass[x].test2; infile >> theClass[x].test3; infile >> theClass[x].test4; infile.ignore(); } infile.close(); } else
Answered Same DayApr 30, 2021

Answer To: Project #3 This project will combine C++ and assembly programming. As we’ve done before, we’ll use...

Aditya answered on May 01 2021
144 Votes
/* ------------------------------------------------------------
Programmer : Bill Gates
Date : April 30, 2020
Description : This program performs several functions with
an array of Student records. This program
should work with any size array
------------------------------------------------------------ */
//#define_CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
using namespace std;
const int ARRAY_SIZE = 12; //there
are 12 records in the file
const int NAME_SIZE = 25; //all names are < 25 chars
struct Student
{
char studentName[NAME_SIZE];
unsigned int test1, test2, test3, test4;
float average;
bool passing;
};
void getData(Student[]); //loads the array with data from file
void print(Student[], Student); //show results
void findAverages(Student[]); //gets each student's avg based on 4 tests
void determinePassing(Student[]); //passing is an average >= 60
void getHighestStudent(Student[], Student&); //finds student with highest average
int main()
{
Student theClass[ARRAY_SIZE];
Student highest = { {"Nobody"}, 0,0,0,0,0,0 }; //this strucure will hold top student's info
getData(theClass);
findAverages(theClass);
determinePassing(theClass);
getHighestStudent(theClass, highest); //NOTE: highest is a REFERENCE parameter!
print(theClass, highest);
}
/* ------------------------------------------------------------
Function name: getData(Student theClass[])
Description: This function is used to read the data from the student_data.txt and store it into theClass [] array of structure
Receives: Array of structure of Student
Returns: It stores the data into Student structure array
Requires: The input file,Student structure array
------------------------------------------------------------ */
void getData(Student theClass[])
{
ifstream infile;
char temp[NAME_SIZE];//temporary array to read in names
infile.open("student_data.txt");
if (infile)
{
for (int x = 0; x < ARRAY_SIZE; x++)
{
infile.getline(temp, NAME_SIZE);//workaround to read in array of chars
strcpy(theClass[x].studentName, temp);// instead of a string
infile>>theClass[x].test1;
infile>>theClass[x].test2;
infile>>theClass[x].test3;
infile>>theClass[x].test4;
infile.ignore();
}
infile.close();
}
else
cout<<"Error opening file"<}
/* ------------------------------------------------------------
Function name : findAverages(Student theClass[])
Description : it is used to find average of the test marks of student
Receives: Array of structure of Student
Returns: It stores the data into Student Array
Requires: Student structure array should contain data
------------------------------------------------------------ */
void findAverages(Student theClass[])
{
float average;
for(int i =0;i {
average = theClass[i].test1+theClass[i].test2+theClass[i].test3+theClass[i].test4;
average = average/4;
theClass[i].average = average;
}
}
/*Assembly Code
__Z12findAveragesP7Student:
LFB2253:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $32, %esp
    movl    $0, -4(%ebp)
L5:
    cmpl    $11, -4(%ebp)
    jg    L6
    movl    -4(%ebp), %eax
    imull    $52, %eax, %edx
    movl    8(%ebp), %eax
    addl    %edx, %eax
    movl    28(%eax), %edx
    movl    -4(%ebp), %eax
    imull    $52, %eax, %ecx
    movl    8(%ebp), %eax
    addl    %ecx, %eax
    movl    32(%eax), %eax
    leal    (%edx,%eax), %ecx
    movl    -4(%ebp), %eax
    imull    $52, %eax, %edx
    movl    8(%ebp), %eax
    addl    %edx, %eax
    movl    36(%eax), %eax
    addl    %eax, %ecx
    movl    -4(%ebp), %eax
    imull    $52, %eax, %edx
    movl    8(%ebp), %eax
    addl    %edx, %eax
    movl    40(%eax), %eax
    addl    %ecx, %eax
    movl    %eax, -32(%ebp)
    movl    $0, -28(%ebp)
    fildq    -32(%ebp)
    fstps    -8(%ebp)
    flds    -8(%ebp)
    flds    LC2
    fdivrp    %st, %st(1)
    fstps    -8(%ebp)
    movl    -4(%ebp), %eax
    imull    $52, %eax, %edx
    movl    8(%ebp), %eax
    addl    %edx, %eax
    flds    -8(%ebp)
    fstps    44(%eax)
    addl    $1, -4(%ebp)
    jmp    L5
L6:
    nop
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE2253:
    .def    ___tcf_0;    .scl    3;    .type    32;    .endef
___tcf_0:
LFB2753:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $8, %esp
    movl    $__ZStL8__ioinit, %ecx
    call    __ZNSt8ios_base4InitD1Ev
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE2753:
    .def    __Z41__static_initialization_and_destruction_0ii;    .scl    3;    .type    32;    .endef
__Z41__static_initialization_and_destruction_0ii:
LFB2752:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $24, %esp
    cmpl    $1, 8(%ebp)
    jne    L10
    cmpl    $65535, 12(%ebp)
    jne    L10
    movl    $__ZStL8__ioinit, %ecx
    call    __ZNSt8ios_base4InitC1Ev
    movl    $___tcf_0, (%esp)
    call    _atexit
L10:
    nop
    leave
    .cfi_restore 5
    .cfi_def_cfa 4,...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here