CS计算机代考程序代写 assembly assembler algorithm CSE 2421 LAB 4 SPRING 21

CSE 2421 LAB 4 SPRING 21
DUE: Sunday, April 11th, by 11:30 p.m.
Objectives:
•Assembly language programs in X86-64
•Using assembler directives in X86-64
•Writing code for functions in X86-64; passing parameters in X86-64 •Correct stack management in X86-64 procedures/functions •Correct use of caller save and callee save registers in X86-64 •Mov, ALU, jump, call, and return instructions in X86-64
•Correct return of values to caller in sub-register of rax •Instructions for four byte (“long”) operands in X86-64
•Instructions for two byte (“word”) operands in X86-64
•Calling C library function printf to print output in X86-64
REMINDERS:
–For this lab, you do not need to submit a README file, and should not submit one.
You should put the certification statement related to academic misconduct at the top of your assembly language source file (BE SURE YOU ADD THIS WHEN YOU FIRST CREATE YOUR SOURCE CODE FILE: IF YOU NEED TO RESUBMIT LATE BECAUSE OF FAILURE TO DO THIS, UNFORTUNATELY, THE LATE SUBMISSION PENALTY WILL APPLY):
BY SUBMITTING THIS FILE TO CARMEN, I CERTIFY THAT I HAVE STRICTLY ADHERED TO THE TENURES OF THE OHIO STATE UNIVERSITY’S ACADEMIC INTEGRITY POLICY WITH RESPECT TO THIS ASSIGNMENT.
–You should always aim to hand an assignment in on time. If you are late (even by a minute – or heaven forbid, less than a minute late), you will receive 75% of your earned points for the designated grade as long as the assignment is submitted by 11:30 pm the following day, based on the due date shown at the top of this document. If you are more than 24 hours late, you will receive a zero for the assignment and your assignment will not be graded at all.
–Any lab submitted that does not assemble (using the command indicated below) and run without crashing WILL RECEIVE AN AUTOMATIC GRADE OF ZERO for the lab (This is a department rule, not a rule of the instructor). No exceptions will be made for this rule – to achieve even a single point on a lab your code must minimally assemble and execute without crashing. To assemble your code, you must use the following command (this is the command the graders will use also):
At the Linux command line prompt:

$gcc –m64 lab4.s -o lab4
– Do not attempt to do more than what is required by the assignment; you will find that it is challenging enough to do what the assignment requires. It is better to focus on doing what you are asked to do and understanding it than to try to do additional things.
– You are responsible for making sure that your lab submits correctly to Carmen Canvas to the Lab4 assignment. Please zip your lab4.s assembly language source file into a file called lab4.zip before submitting it.
– You are required to comment your assembly language code, and the quality and clarity of the comments will be given weight in the grade for the lab.
GRADING CRITERIA (approximate percentages listed)
–(10%) The code and algorithm are well commented.
• The Academic Integrity Statement should also be included as a comment
at the top of your source file.
• A comment should be included in the main program including the
programmer name as well as explaining the nature of the problem and an overall method of the solution (what you are doing, not how).
• A short comment should be included for each logical or syntactic block of statements, including each function.
• A comment should be included for values in registers, and stack set up and clean up instructions in each function; these comments should help the reader of your program to understand the algorithm being used, and where various values in the program are being stored.
–(10%) The program should be appropriate to the assignment, well-structured and easy to understand without complicated and confusing flow of control.
–(80%) The results are correct, verifiable, and well-formatted. The program correctly performs as assigned.
LAB DESCRIPTION
Assembly language program in X86-64 assembly language. Mandatory file name:
lab4.s
For this lab, you will write an X86-64 assembly language program which has 6
functions, with the C language function prototypes shown below: int main();

void multInts(int *array1, int *array2, int size);
void addShorts(short *array1, short *array2, short size); void invertArray(int *array1, int size);
void printArray(int *array1, int size);
int findAndReturnMin (int *array1, int size);
See below for a description of what each of these functions should do.
NOTE: Pay VERY CLOSE ATTENTION to the ORDER of the parameters as they are listed above, and make sure they are passed in the correct registers following the convention discussed in the class slides for System V ABI. EVEN IF your code produces correct output, if the parameters are in the wrong registers, there will be a deduction.
PROBLEM:
Write an assembly language program with five procedures or functions using X86-64. The program should have 4 static arrays, defined in the data section of the program (that is, stored on the heap), including two int arrays, intArray1 and intArray2, both of the same size, and two short arrays, shortArray1 and shortArray2, both of the same size (See the data section below). There should also be two other variables defined in the data section, one of which is an int, sizeIntArrays, and will hold the size of the int arrays, and the other of which is a short, sizeShortArrays, and will hold the size of the short arrays. Do not change any of these labels in the data section (case matters!).
Required data section (You are responsible for putting this in your source file): .data
.align 8
sizeIntArrays:
.long 5 sizeShortArrays:
.word 4 intArray1:
.long 10 .long 25
.long 33 .long 48

.long 52 intArray2:
.long 20 .long -37 .long 42 .long -61 .long -10
shortArray1: .word 69
.word 95 .word 107 .word 332
shortArray2: .word -87
.word 331 .word -49 .word -88
CONSTRAINTS:
•Your program must have six procedures/functions, with the labels main, multInts,
addShorts, invertArray, printArray, and findAndReturnMin (Do not change
these: case matters!).
•Make very sure that you include the required assembler directives for each of
your procedures/functions. These are covered in the class slides on X86-64, and there are functions with examples so that you can see the correct way to do it (follow the examples, but be careful); leaving these directives out of your program will cause major problems, so do not overlook this!
NOTE: BEFORE you write code for ANY function, make a source file, lab4.s, with the Academic Integrity Statement, the required .data section shown above, and 6 stub functions with the correct function labels (given above) and all necessary assembler directives for each function, based on the examples given in the class slides.
•Also be VERY SURE that you include the stack set up and clean up instructions at the beginning and end of each procedure/function. These should ALSO be included in your stub functions BEFORE you write ANY OTHER CODE!!!! This is also a detail that beginning assembly language programmers sometimes overlook, but it will cause your program to work incorrectly, if it works at all, and will be a MAJOR HEADACHE TO DEBUG, so do not overlook this!
•Also, after you have done all of the above, you should write the code for main to pass parameters to the five other functions/procedures.
•Now, write the code for the five other functions, and test and debug them, ONE AT A TIME!!! I cannot emphasize how HUGE an ERROR it is to write code for all the functions before testing and debugging the first one. You will learn a lot by

testing and debugging the first function, so do yourself a huge favor and wait to
write the others till after you write, test and debug the first.
•Also be very sure to pay attention to the difference between caller save and
callee save registers (You should do this throughout your program, but especially before you call printf!). Overlooking this difference will also cause incorrect output, segmentation faults, and other major problems!
•NOTE CAREFULLY: All labels in an assembly language source code file must be UNIQUE! If you use a label loop in four functions, and if you execute jmp loop, you do not know which of those four labels your program will jump to! Also, this kind of problem can be EXTREMELY difficult to debug, and it often drives people crazy (We’ve had poor CSE 2421 students who went off to write their first X86-64 assembly language program and have never returned ☹)!!!! THEREFORE, you should use labels such as loop1, exit1, loop2, exit2, etc., so that the labels are all unique; do yourself a favor and use the same number for all labels in a given function (loop1, exit1, etc. for one function, loop2, exit2, etc. for another function, etc.).
•Also remember that you will need a separate format string for printf in for each different kind of value you need to print (see the output given near the end of this description to see what format string literals you will need in your rodata section for calls to printf. These strings should be put in the rodata section of the program. See the examples in the class slides.
Description of Procedures
•You need to write the main procedure to call the other five procedures at
appropriate points (the other five procedures should be called in the order they are listed below), and with the appropriate parameters in the correct registers to pass to the procedures, so that they do what is described below. You are allowed to do other work in main to get parameter values to pass to the procedures before you call them, but THE ONLY OUTPUT WHICH SHOULD BE PRINTED FROM main IS THE VALUE RETURNED BY FUNCTION findAndReturnMin. The output from all other procedures/functions should be printed as described below. Main should print the return value from findAndReturnMin after the string:
“The minimum value in intArray2 is: “, followed by the value returned from findAndReturnMin. Finally, main should return 0 to its caller.
•The multInts procedure should first call printf to print “Products” on one line, and then multiply the first value in the first int array, intArray1, times the first value in the second int array, intArray2, and call printf to write the result to output on a separate line, then multiply the second value in the first int array times the second value in the second int array, and call printf to write the result to output on a separate line, and so on, for each pair of values consisting of one value from the first int array, and the second value in the same position in the second int array. A blank line should be printed at the end of all of the products. For this

function, you should only need one index register. Remember that only a 64 bit register can be used as part of an address expression, so you will have to use a 64 bit register as the index register. Also note carefully: If you want to put a non- zero value which is less than 64 bits (the size of the array here is a 32 bit value) into a 64 bit index register, you should first CLEAR the 64 bit register by putting 0 into it; then you can move the value less than 64 bits in size into the appropriate sub-register of the 64 bit register. [For example, if I want to use register rcx as an index register, but want to initialize it with a 32 bit value in register %edi, I first do:
movq $0, %rcx And then do:
movl %edi, %ecx #Now, the value in %rcx is equal to the value in %edi
If we do not CLEAR rcx first, the most significant 32 bits of rcx may not be 0, so the 64 bit value is rcx after moving edi to ecx may not be equal to the 64 bit value in rcx, and our 64 bit index value will be INCORRECT!
•The addShorts procedure should first call printf to print “Sums”, and then add the first value in the first short array, shortArray1, to the last value in the second short array, shortArray2, and call printf to write the sum to output (on a separate line from the “Sums” string printed before), then add the second value in the first short array to the second from the last value in the second short array, and call printf to write the sum to output, and so on, for each of the n pairs of values in the two arrays consisting of one value at index position i in the first short array, and the second value at index position n – 1 – i in the second short array. A blank line should be printed at the end of all of the sums. For this function, you will want to use two index registers, one starting at the beginning of one array, and one starting at the end of the other array. Remember that only 64 bit registers can be used as part of an address expression, so you will have to use 64 bit registers as index registers. Also note carefully: If you want to put a non-zero value which is less than 64 bits (the size of the array here is a 16 bit value) into a 64 bit index register, you should first CLEAR the 64 bit register by putting 0 into it; then you can move the value less than 64 bits in size into the appropriate sub-register of the 64 bit register. [For example, if I want to use register rcx as an index register, but want to initialize it with a 16 bit value in register %di, I first do:
movq $0, %rcx

And then do:
movw %di, %cx #Now, the value in %rcx is equal to the value in %di
If we do not CLEAR rcx first, the most significant 48 bits of rcx may not be 0, so the 64 bit value is rcx after moving di to cx may not be equal to the 64 bit value in rcx, and our 64 bit index value will be INCORRECT!
ALSO NOTE CAREFULLY: The format specifier to print a short (two-byte) value with printf is %hi or %hd. The format string in your rodata section to print the short array values must have one of these format specifiers.
•The printArray procedure will be called twice, the first time before the call to invertArray, and the second time after. It should first call printf to print “Elements in intArray1”, and then, on the following lines, print the values in the array it is passed in order, from the first value to the last value, one value per line. printArray should be called with a pointer to intArray1, as well as the size of the array it is to print. It should also print a blank line following all of the values in the array. For this function, you should only need one index register. Remember that only a 64 bit register can be used as part of an address expression, so you will have to use a 64 bit register as the index register.
•The invertArray procedure should invert, or reverse, the elements in intArray1, but prints no output. The elements in intArray1, after they are placed in inverse order by invertArray, will be printed out by procedure printArray, below, which will be called from main after the invertArray procedure returns to main. For this function, you will want to use two index registers, one starting at the beginning of the array, and one starting at the end of the array. Remember that only 64 bit registers can be used as part of an address expression, so you will have to use 64 bit registers as index registers. You will also need to think about how to
determine when to terminate the loop to swap the elements from the beginning and end of the array. Even though the array here (intArray1) has an odd number of elements, you should write your code so that it would work correctly even for an array with an even number of elements (you can modify the intArray1 data and size to test to see if your solution works correctly for an even number of elements, but BE SURE TO RESTORE THE DATA VALUES GIVEN ABOVE BEFORE SUBMITTING YOUR LAB!).
•The printArray procedure should be called the second time after the call to, and return from, invertArray. It first calls printf to print “Elements in intArray1”, and then, on the following lines, print the values in the array it is passed in order, from the first to the last, one value per line. printArray should be called the second time with a pointer to intArray1 after the elements are inverted, or reversed, by invertArray, to print out the elements in the opposite order which

they were originally in. It will also be passed the size of the array it is to print. It should also print a blank line following all of the values in the array.
•The findAndReturnMin function should find the minimum value in intArray2; it is passed a pointer to this array, and the size of the array. After finding the minimum value, the function should return the minimum value in the array to its caller (main in this program) in the appropriate sub-register of register rax. This function should use a cmov instruction (conditional move) at least once (you can decide how many times you need to use cmov) in finding the minimum value in the array. For this function, you should only need one index register. Remember that only a 64 bit register can be used as part of an address expression, so you will have to use a 64 bit register as the index register.
Note Carefully
•To pass values to procedures, you must use the appropriate registers (the order
of the parameters specified in the function declarations above is important), as
discussed in class and shown in the class slides.
•You should also follow the X86-64 conventions discussed in class, and covered in
the class slides, related to caller and callee save registers, and returning values from procedures in register rax or an appropriate sub-register (eax, ax, or al, depending on the size of the return value).
•Remember that you need to put format strings in read only memory (in your rodata section) in order to call printf to print output, as well as passing the values of any variables to be printed in the appropriate register(s). See the first X86-64 program in the class slides for examples of how to do this.
FINALLY: Remember that, before calling any function which has a variable number of parameters (such as printf), you must set register rax equal to 0!
OUTPUT:
•For the input given above in the data section (which you are required to use), the output should be as follows:
Products 200
-925 1386 -2928 -520
Sums -19 46

438 245
Elements in intArray1 10
25
33
48 52
Elements in intArray1 52
48
33
25 10
The minimum value in intArray2 is: -61
LAB SUBMISSION
You should submit all your lab assignments electronically to Carmen Canvas. See the instructions for earlier labs for guidance on how to start a firefox web browser on stdlinux and log in to Carmen to submit your lab. Be sure to zip your lab4.s assembly language source code file in a file lab4.zip before submitting to Carmen. NOTE: Since you are only submitting one file, DO NOT put it in a zipped folder (that is, the zip file should only contain one file, lab4.s)!!!!
NOTE:
• Your programs MUST be submitted in source code form. Make sure that you
submit the program code as a .s file only. Do NOT submit the object files and/or the executable. If you only submit the executable file for the program, you will receive NO CREDIT, and no exceptions will be made for this rule!
• It is YOUR responsibility to make sure your code can assemble and run without causing runtime errors on the CSE department server stdlinux.cse.ohio-state.edu using the command $gcc –m64 lab4.s -o lab4 as specified earlier in this lab description.