Project 3: Introduction to X86-64 Assembly Programming
Grading Form
Goal
In this project you will write programs in x86-64 assembly language. It is important that you learnthe x86-64 assembly language since it is the one you use every day in your PC, Mac, or indata.cs.purdue.edu. Also, this is a 64 bit architecture that uses 8 byte addresses and
ariable-length assembly instructions.
X86-64 Introduction The X86-64 assembly language was created by AMD and then adopted by Intel. The X86-64
assembly language extends the x86 32 bit architecture to 64 bits. X86-64 is a superset ofx86-32. It provides an incremental evolution to migrate from x86-32 bits to x86-64 bits and it isbackward compatible.
Here is a good reference that can help you in programming with the X86-64. Section 3.2 onpage 5 gives an example of a C program translated to X86-64, and Figure 2 on page 7 gives anexplanation of each register (which is also shown below).
x86-64 tutorial
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. Asopposed 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 andpassed via %rdi, %rsi, %rdx, and %rcx. Some registers are callee saved and canbe used aslocal variables. In addition, 代 写 Introduction to X86-64 Assembly Programming %rax is used to return values in functions, similar to how you wouldload r0 in ARM assembly before returning.
One of the main differences is that in the x86-64 architecture is that the order of the arguments is different than in the ARM. For example, in the instruction:movq $3, %rsi
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:mov r2 #3The x86-64 architecture is backward compatible with x86-32, and as such the 4 least significantbytes 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 theinstructions will end with "q" which means that they will work with 8 byte words.The addressing modes in the x86-64 are the following:
Immediate Value movq $0x501208,%rdi
#Put in register %rdi the constant 0x501208
Direct Register Reference
movq %rax,%rdi
#Move the contents of register %rax to %rdi
Indirect through a register movq %rsi,(%rdi )
#Store the value in %rsi in the address contained in %rdi
Direct Memory Reference movq 0x501208,%rdi
#Fetch the contents in memory at address 0x501308 and store it in %rdi
Task 1: Your first X86-64 Assembly ProgramLogin to data.cs.purdue.edu and create a directory project-3-src where you will put all your
code:
Type:cd
mkdir -p cs250/project-3-src
cd cs250/project-3-src You will do all your work in data.cs.purdue.edu and in the directory ~/cs250/project-3-src.Type the following program sqr.s that squares a number read from the terminal, then prints the
result. Important note: it is easy to miss that your “main'' function will be declared with .globl inx86 as opposed to .global in ARM.# Define global variable a in data section
. See what the program does line by line. You will find it very similar to ARM, such as how theargument registers are used (i.e. %rdi instead of r0, %rsi instead of r1 - library functions such asprintf still need a format string and an address to put the value it scans in).
Then assemble it using the following command.
gcc -static -o sqr sqr.s./sqr
The -static flag passed to gcc is used to make the .text section has a predefined loadingaddress instead of being able to load it at random text addresses. The -static flags tells thecompiler not to generate position independent code or PIC that is the default. One of the newsecurity features of Linux is to load the program at random memory addresses every time yourun it to make programs more difficult to hack. The flag -static will disable this feature in theassembly programs you write to make assembly programming easier.
Question 1. Write the code above into the file sqr.s, compile it and run it.
Question 2. Explain what the following instructions do:
pushq %rbp
# Save frame pointer
movq %rsp, %rbp
…...
leave
ret
Question 3.Write a program avg.s in assembly language that reads n numbers, then
computes the sum and the average. The numbers are long integer of 8 bytes and the
average will be truncated. Assume that the first number you read in will be the value of n, i.e. there will be n more inputs that follow.
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.
gcc -static -o maxarray maxarray.c maxarray.s
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.
movq
%rdx,%rcx
imulq
$8,%rcx
addq
%rsi,%rcx
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);
} To compile the program type
gcc -static -o bubble bubble.c bubble.s
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
“Makefile” in your lab directory, and put the following contents in it - Bash knows the keyword“make” and will search for a Makefile, attempting to compile the “goal:” line it finds. This is easierthan compiling each individual file when working on large projects, and will not compile a file if ithas not been edited since the last time it was compiled (you will learn more about this inCS252).# 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 clean
make Turnin
Follow these instructions to turnin project-3:
Make sure that your programs are built by typing "make". Make sure it builds and runs indata.cs.purdue.edu etc.If you have not created a project-3-src create it. Type:cdcd cs250
mkdir project-3-srcCopy your files into project-3-src/ and cd to the parent directory ofproject-3-src. Then type:
turnin -c cs250 -p project-3 project-3-srcThen, you may type "turnin -c cs250 -p project-3 -v" to make sure you have submitted the
orrect 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.
标签:project,Assembly,X86,Introduction,Programming,long,will,64,maxarray From: https://www.cnblogs.com/comp9021/p/18446274