首页 > 其他分享 >Introduction to X86-64 Assembly Programming

Introduction to X86-64 Assembly Programming

时间:2024-10-04 11:49:56浏览次数:1  
标签:project Assembly X86 Introduction Programming long will 64 maxarray

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

相关文章

  • CPSC 219: Introduction to Computer Science II
    CPSC219:IntroductiontoComputerScienceIIAssignment1:ProceduralJava–SnakeGameCourse:CPSC233S24DueDate:Friday,Sept.27,2024Version:1.1.1Weight:10%Objective:WriteaJavaprogramwithastandardproceduralstructureandsavetheworkto......
  • Skills - 2022 International Collegiate Programming Contest, Jinan Site, Problem
    有3种技能,\(n(\le10^3)\)天内每天可以对一个技能进行学习,第i天学习第j个技能可以为第j个技能增加\(a_{i,j}(\le10^4)\)的熟练度。在第i天结束时,每个技能的熟练度会减去距离上次学习该技能的天数,但最多减到0。求n天后能得到的熟练度的和的最大值。首先容易有一个显然的dp状态:\(f......
  • The 2024 Guangdong Provincial Collegiate Programming Contest
    Preface这场据说题挺毒的?但实际打的时候感觉也还好,3h就出了7个题,然后被H卡飞了赛后发现是没有观察到构造的解的性质,把Dinic换成匈牙利就过了,只能说对flow的理解不够B.腊肠披萨神秘string题,被徐神开场一眼秒了,虽然中间我和祁神上去写了三个签到,但徐神还是在1h不......
  • 学习笔记:A Introduction to inertial navigation
    3.2MEMS陀螺的误差特性本节我们分析MEMS陀螺中存在的误差,以及它们对积分后的信号(也就是旋转)的影响。3.2.1常量零偏角速度陀螺的零偏是它在不忍受任何转动时的平均输出,单位度每小时。如果对一个\(\epsilon\)大小的常量零偏进行积分,会导致一个随时间线性增长的角度误差:\(\theta......
  • CMPT 477 / 777 Formal Verification Programming
    CMPT477/777FormalVerificationProgrammingAssignment1Thisassignmentisdueby11:59pmPTonWednesdayOct2,2024.PleasesubmitittoCanvas.Latepolicy:Supposeyoucangetn(outof100)pointsbasedonyourcodeandreportIfyousubmitbefor......
  • CSCI1120 Introduction to Computing Using C++
    CSCI1120IntroductiontoComputingUsingC++,Fall2024/25DepartmentofComputerScienceandEngineering,TheChineseUniversityofHongKongCopyright©2024CSE,CUHKPage1of8Assignment2:GumballMachinesDue:23:59,Thu3Oct2024Filename:gumball.......
  • CITS2402 Introduction to Data Science
    CITS2402Introduction to Data ScienceSemester2, 2024AssignmentAssessed,worth 20%. Due: 11:59pm,Friday 4th  October 20241 AimThisassignmentaimstoinvestigatethesimilaritiesanddifferencesbetweenAustraliaandNew Zealand regarding......
  • 个人翻译Introduction to Linear Algebra, 5th Edition 10.2节(仅用于交流学习,非盈利)
    本书的翻译仅为交流学习!才疏学浅,不当的地方还望指正。请勿于其它用途!PDF文件 链接一:  https://pan.baidu.com/s/1ENKxfP_QJBaZHXlQ_xdxzw?pwd=9nej提取码:9nej 链接二:https://download.csdn.net/download/sinat_21706867/89817586以下只贴出本PDF截图 ......
  • CS 417/517: Introduction to Human Computer Interaction
    CS417/517:IntroductiontoHumanComputerInteraction Project1(Fall2024)1IntroductionInthisassignment,yourtaskistoimplementaConvolutionalNeuralNetwork(CNN)andevaluatetsperformanceinclassifyinghandwrittendigits.Aftercompleti......
  • The 13th Shandong ICPC Provincial Collegiate Programming Contest
    目录写在前面I签到A签到D二分答案,贪心G排序,贪心L构造,思维B模拟,拓扑排序E数学,结论,模拟M计算几何,单调性J二进制,连通性问题,并查集K贪心orDP,结论,构造F线段树优化DP写在最后写在前面补题地址:https://codeforces.com/gym/104417。以下按个人向难度排序。妈的调休太顶......