Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
Project 3: Introduction to X86-64 Assembly Programming
Goal
X86-64 Introduction
Also see the x86-64 assembly notes section in http://www.cs.purdue.edu/homes/cs250The X86-64 architecture uses the following register assignment:
You will find many similarities with the ARM architecture. For instance, there are also 16 registers available to the user, though not all are typically used as general purpose registers. As opposed to using r0, r1, and so on, some registers are named due to backwards-compatibility: instead of receiving/passing arguments via r0, r1, r2, and r3, arguments are received and passed via %rdi, %rsi, %rdx, and %rcx. Some registers are callee saved and can be used as local variables. In addition, %rax is used to return values in functions, similar to how you would load r0 in ARM assembly before returning.
The first argument, numerical constant $3, is assigned into the register %rsi so the target register is on the right. In ARM, this would be equivalent to:
The x86-64 architecture is backward compatible with x86-32, and as such the 4 least significant bytes of registers %rax, %rbx, %rcx, %dx are compatible with the old x86-32 bit registers %eax, %ebx, %ecx, and %edx. We will only write programs using the 64-bit registers, so most of the instructions will end with "q" which means that they will work with 8 byte words.
The addressing modes in the x86-64 are the following:
Task 1: Your first X86-64 Assembly Program
You will do all your work in data.cs.purdue.edu and in the directory ~/cs250/project-3-src.
Question 1. Write the code above into the file sqr.s, compile it and run it.
Hint: Use the instruction "idivq" to compute the average. It might be helpful to look into commonly used X86-64 opcodes, as it may have some useful functionalities that ARM does not.
Task 2: Using Arrays in X86-64
The following C function computes the maximum of an array
// Finds the max value in an array
long maxarray(long n, long *a) {
long i=0;long max = a[0];
while (i<n) {
if (max < a[i]) {
max = a[i];
}
i++;
}
return max;
}
|
The equivalent code in X86-64 assembly language is given here:
// maxarray.c:
#include <stdio.h>
long a[] = {4, 6, 3, 7, 9 };extern long maxarray(long, long*);
int main()
{
printf("maxarray(5,a)=%ld\n", maxarray(5,a));
}
|
Question 4. Type the programs maxarray.s and maxarray.c, then test them. Also answer, what do the following instructions from the above code snippet do? Explain.
Task 3: Implementing Bubble Sort in X86-64
Question 5. Implement a function bubblesort(long ascending, long n, long * a) in X86-64 in a file bubble.s that will sort an array of integers using bubblesort.
Here is the code in C that implements bubblesort. You have to implement it in X86-64 assembly language in the file bubble.s
void bubble_sort(long ascending, long n, long * a) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
long swap = 0;
if (ascending) {
if (array[j+1] < array[j]) {
swap = 1;
}
else {
if (array[j+1] > array[j]) {
swap = 1
}
}
if (swap) {
long temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}}
} |
Then call it from the file bubble.c
// bubble.c:
#include <stdio.h>
long a[] = {6, 7, 2, 3, 1, 9, 4, 5, 0, -9, 8};
long n = (sizeof(a)/sizeof(long));
extern void bubblesort(long ascending, long n, long * a);
void printArray(long n, long * a) {
for (int i = 0; i < n; i++) {
printf("%ld ", a[i]);
}
printf("\n");
}
int main(int argc, char ** argv)
{
printf("Before Ascending:\n");printArray(n,a);
bubblesort(1, n, a); // notice how we do not return anything here...
printf("After Ascending:\n");printArray(n,a);
printf("Before Descending:\n");printArray(n,a);
bubblesort(0, n, a); // notice how we do not return anything here...
printf("After Descending:\n");printArray(n,a);
}
|
Question 6. Complete the following Makefile that will make all the executables in this lab.
You may have used Makefiles in past lab courses without knowing it! Create a file named
# TODO: Modify the below to compile bubble as well
goal: sqr maxarray
sqr: sqr.s
<TAB>gcc -static -o sqr sqr.s
maxarray: maxarray.s maxarray.c
<TAB>gcc -static -o maxarray maxarray.c maxarray.s
clean:
<TAB>rm -f sqr maxarray
|
To use the Makefile, type:
make
Turnin
Make sure that your programs are built by typing "make". Make sure it builds and runs in data.cs.purdue.edu etc.
Then, you may type "turnin -c cs250 -p project-3 -v" to make sure you have submitted the correct files - remember the -v flag, or it will ask you if you wish to resubmit your lab again.
You will show your programs to the lab instructor and TAs during lab time next week.
Q5. __/30 points
Project 3 Grading Form
Question |
Max |
Current |
Question 1. Write the code above into the file sqr.s, compile it and run it. |
10 |
|
Question 2. Explain whatthe following instructions do:
pushq %rbp
# Save frame pointer
movq %rsp, %rbp
…...
leave
ret
|
10 |
|
Question 3.Write a program avg.s |
30 |
|
Question 4. Type the programs maxarray.s and maxarray.c |
10 |
|
Question 5. Implement a function bubblesort(long ascending, long n, long * a) |
30 |
|
Question 6. Complete the following Makefile |
10 |
|
|
Total: Max 100 |
|