X86汇编代写

SUMMER 2020 CSE 2421 LAB 4

DUE: Sunday, June 28th, 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. You should put the

certification statement related to academic misconduct at the top of your assembly language source file:

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. 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 -o lab4 lab4.s

  • –  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 dong 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 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.

• 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 5

functions, with the C language function prototypes shown below: int main();

void multInts(int size, int *array1, int *array2);
void addShorts(short size, short *array1, short *array2);

void invertArray(int size, int *array1);
void printArray(int size, int *array1);
int findAndReturnMax (int size, int *array1);

See below for a description of what each of these functions should do.

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. 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: .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 five procedures/functions, with the labels main,

multInts, addShorts, invertArray, printArray, and findAndReturnMax (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, 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!

• Also be VERY SURE that you include the stack set up and clean up instructions at the beginning and end of each procedure/function. 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!

•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 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.

•Also remember that you will need a separate format string for printf in for each different kind of value you need to print. 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, 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 findAndReturnMax.

The output from all other procedures/functions should be printed as described below.

•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.

•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, 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 32 bit value) into a 64 bit index register, you should first CLEAR the 64 bit register by purring 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 %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!

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 findAndReturnMax function should find the maximum value in intArray2; it is passed a pointer to this array, and the size of the array. After finding the maximum value, the function should return the maximum value in the array to its caller (main in this program) int the appropriate sub-register of register rax. This

function should use a cmov instruction at least once (you can decide how many times you need to use cmov) in finding the maximum 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, 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 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.

OUTPUT:
•For the sample input given above, 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 maximum value in intArray2 is: 42

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 before submitting to Carmen. NOTE: Since you are only submitting one file, DO NOT put it in a zipped folder!!!!

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 -o lab4 lab4.s as specified earlier in this lab description.