This document discusses the x86-64 instruction set architecture. It covers the general purpose integer registers, assembly language syntax, common instructions like arithmetic, logical, and shift operations. Examples are provided to demonstrate how instructions modify register values. The key points are:
1) X86-64 supports 64-bit computing with 16 integer registers and a register-memory architecture.
2) Assembly language uses mnemonics to represent machine instructions in a human-readable form.
3) Common instructions include arithmetic, logical, and shift operations that can perform calculations and bit manipulations using register operands.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
54 views67 pages
CSS224 2020 02
This document discusses the x86-64 instruction set architecture. It covers the general purpose integer registers, assembly language syntax, common instructions like arithmetic, logical, and shift operations. Examples are provided to demonstrate how instructions modify register values. The key points are:
1) X86-64 supports 64-bit computing with 16 integer registers and a register-memory architecture.
2) Assembly language uses mnemonics to represent machine instructions in a human-readable form.
3) Common instructions include arithmetic, logical, and shift operations that can perform calculations and bit manipulations using register operands.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67
2.
Instructions and Instruction Set
Architecture
CSS224 Sec.01 & 02 Semester 1/2020 1
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. Contents 2.1 x86-64 Architecture 2.2 General-purpose Integer Registers 2.3 Assembly Language 2.4 Assembly Program 2.5 Calling Convention 2.6 Control Structures 2.7 Arrays 2.8 Example of Assembly Programs
CSS224 Sec.01 & 02 Semester 1/2020 2
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.1 x86-64 Architecture • It is an extended version of the x86 architecture originally developed by AMD.
• It supports 64-bit computing.
• It is fully compatible with its predecessors (16-bit and 32-bit
architectures).
• It is a register-memory architecture.
CSS224 Sec.01 & 02 Semester 1/2020 3
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.2 General-Purpose Integer Registers • A register is a temporary storage in the processor used for storing intermediate values while conducting calculation.
• X86-64 defines 16 integer registers.
• Each register is 64 bits wide.
• The range of integer values can be stored in each register is shown in
Table 2.1 and 2.2.
CSS224 Sec.01 & 02 Semester 1/2020 4
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.2 General-Purpose Integer Registers
CSS224 Sec.01 & 02 Semester 1/2020 5
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.2 General-Purpose Integer Registers • Interpreting signed and unsigned integer values (using two’s complement method)
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.2 General-Purpose Integer Registers • We refer to each register by its name. • Table 2.3 shows the register names for x86-64.
From C.Nattee, Lecture Note of CSS224 Computer Architectures, 2017, Chapter 2.
CSS224 Sec.01 & 02 Semester 1/2020 7
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3 Assembly Language • An assembly language is a way to represent a sequence of machine instructions in a human-friendly form.
• To execute an assembly program, we need to have an assembler to
translate the instructions into the machine code.
CSS224 Sec.01 & 02 Semester 1/2020 8
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3 Assembly Language • Here are how to write assembly instructions:
where OP = an operation, A and B = operands which can be registers,
constants, and memory.
• A dollar sign ($) is used to specify an integer constant.
• For example, add $1,%rax → %rax = %rax + 1
CSS224 Sec.01 & 02 Semester 1/2020 9
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.1 Operands • Three types of operands can be used in the assembly instructions.
1. Register is referred by its name.
2. Constant is any numerical literal preceded with a $.
3. Memory location can be referred to in several ways.
• A way to refer to a memory location is called an addressing mode.
CSS224 Sec.01 & 02 Semester 1/2020 10
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.1 Operands • Note: ‘0x’ is a marker for a hexadecimal constant, and ‘0b’ is a marker for a binary constant.
• Table 2.4: Example of Operands in Assembly Instructions
From C.Nattee, Lecture Note of CSS224 Computer Architectures, 2017, Chapter 2.
CSS224 Sec.01 & 02 Semester 1/2020 11
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.1 Operands • For example, add $11,%rax → %rax = %rax + 11 add $0x11,%rax → % rax = %rax + 17 add $0b11,%rax → % rax = %rax + 3
CSS224 Sec.01 & 02 Semester 1/2020 12
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. Exercise • Let %rax = 1, %rcx = 1024, and %rdx = 64. Wrtie the output of the following instructions.
1. add %rcx,%rax
2. neg %rax
3. idiv %rcx
CSS224 Sec.01 & 02 Semester 1/2020 13
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.2 Instructions • Table 2.5 shows basic integer instructions in x86-64. • Table 2.5 Integer Arithmetic Instruction 1
From C.Nattee, Lecture Note of CSS224 Computer Architectures, 2017, Chapter 2.
CSS224 Sec.01 & 02 Semester 1/2020 14 This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.2 Instructions • Instruction Execution Examples: mov %rcx,%rax
CSS224 Sec.01 & 02 Semester 1/2020 15
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.2 Instructions • Instruction Execution Examples: add %rcx,%rax
CSS224 Sec.01 & 02 Semester 1/2020 16
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.2 Instructions • Instruction Execution Examples: sub %rcx,%rax
CSS224 Sec.01 & 02 Semester 1/2020 17
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.1 Operands • An instruction idiv S divides a 128-bit value constructed by concatenating %rdx and %rax by a register S.
• Note: dividend = quotient * divisor + remainder
o The dividend = %rdx:%rax o The divisor = S o The integer quotient = %rax o The remainder = %rdx
CSS224 Sec.01 & 02 Semester 1/2020 18
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.2 Instructions • Table 2.6 shows basic integer instructions in x86-64. • Table 2.6 Integer Arithmetic Instruction 2
From C.Nattee, Lecture Note of CSS224 Computer Architectures, 2017, Chapter 2.
CSS224 Sec.01 & 02 Semester 1/2020 19
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.2 Instructions • Instruction Execution Examples: and %rcx,%rax
CSS224 Sec.01 & 02 Semester 1/2020 20
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.2 Instructions • Instruction Execution Examples: shl $2,%rax
CSS224 Sec.01 & 02 Semester 1/2020 21
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.2 Instructions • Instruction Execution Examples: shr $2,%rax
CSS224 Sec.01 & 02 Semester 1/2020 22
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.2 Instructions • Instruction Execution Examples: sar $2,%rax
CSS224 Sec.01 & 02 Semester 1/2020 23
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.3.2 Instructions • Instruction Execution Examples: shr $2,%rax
CSS224 Sec.01 & 02 Semester 1/2020 24
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. Exercise • Let %rax = 10 and %rcx = 1024. Wrtie the output of the following instructions.
1. not %rax
2. or $-1,%rax
3. shl $4,%rcx
4. shr $4,%rcx
CSS224 Sec.01 & 02 Semester 1/2020 25
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.4 Assembly Program • An assembly program mainly is composed of a sequence of assembly instructions. • The processor do not understand the assembly instructions. • An assembler is needed to translate assembly instructions into machine code. • Each assembly instruction can be one-to-one translated into a machine instruction.
From C.Nattee, Lecture Note of CSS224 Computer Architectures, 2017, Chapter 2.
CSS224 Sec.01 & 02 Semester 1/2020 26
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.4 Assembly Program • Each assembly program also includes a number of assembler directives o To tell the assembler how to translate the program o To tell the assembler how to create an executable file.
• A directive always starts with a dot.
• Table 2.7 shows assembler directives.
From C.Nattee, Lecture Note of CSS224 Computer Architectures, 2017, Chapter 2.
CSS224 Sec.01 & 02 Semester 1/2020 27 This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.4 Assembly Program • An assembly program may include a number of labels. • Each label is a number ended with a colon. • It represents a memory address of instructions or constants.
CSS224 Sec.01 & 02 Semester 1/2020 28
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.4 Assembly Program • Example2.2 An assembly program that output “Hello world”.
• The above assembly program is equivalent to the following C program.
CSS224 Sec.01 & 02 Semester 1/2020 29
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.4 Assembly Program • Here is the output obtaining from the assembler.
Assembly
Hexa decimal
CSS224 Sec.01 & 02 Semester 1/2020 30
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.4 Assembly Program • The previous machine instructions still cannot be executed since a C standard library function (puts) • We need to pass it to a linker to create an executable program.
From C.Nattee, Lecture Note of CSS224 Computer Architectures, 2017, Chapter 2.
CSS224 Sec.01 & 02 Semester 1/2020 31
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.5 Calling Convention • We need to follow a standard scheme for calling C functions called ‘calling convention’. Here are part of them:
1.Pass parameters to function via registers in the following order:
%rdi, %rsi, %rdx, %rcx, %r8, %r9.
2.An integer value can be returned from a function via %rax.
3.The following registers may be changed when we make a function call:
4. We need to save the values of the following registers before we make
any change: %rbx, %rbp, %r12, %r13, %r14, %r15.
CSS224 Sec.01 & 02 Semester 1/2020 32
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.5 Calling Convention • We study the assembly language in order to understand how machine instructions work. • We therefore do not write a stand-alone assembly program, but only implement functions that can be called from C programs in the assembly language.
CSS224 Sec.01 & 02 Semester 1/2020 33
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.5 Calling Convention • Example 2.3 Implement a function sum(a, b, c) that accepts three integer values and returns the sum of them.
CSS224 Sec.01 & 02 Semester 1/2020 34
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.5 Calling Convention • Example 2.3 Implement a function sum(a, b, c) that accepts three integer values and returns the sum of them.
CSS224 Sec.01 & 02 Semester 1/2020 35
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.6 Control Structures • Each assembly program is just a sequence of instructions. • No other structures are provided in the syntax of the language. • We can control the instruction execution using two types of instructions, i.e., compare and jump instructions. Table 2.8 shows them. • Control Structures 2.6.1 Conditional statements 2.6.2 Repetition statements
CSS224 Sec.01 & 02 Semester 1/2020 36
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.6 Control Structures • Table 2.8 Compare and Jump instructions.
CSS224 Sec.01 & 02 Semester 1/2020 37
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.6.1 Conditional Statements • We can combine the compare and jump instructions in order to make the program work in the same manner as the condition statements (if- else). • Example:
CSS224 Sec.01 & 02 Semester 1/2020 38
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.6.1 Conditional Statements • Example 2.5 Write a function in assembly that prints “>100” when an integer parameter is greater than 100, otherwise it prints “<=100”.
CSS224 Computer Architectures 39
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.6.1 Conditional Statements • Example 2.5 (cont.)
CSS224 Computer Architectures 40
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.6.2 Repetition Statements • The compare and jump instructions can also be used to form repetition structures. • Example 2.7 Write a function sumN that calculates 1+2+…+n where n is an argument.
CSS224 Sec.01 & 02 Semester 1/2020 41
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.6.2 Repetition Statements • Example 2.7 Write a function sumN that calculates 1+2+…+n where n is an argument.
CSS224 Sec.01 & 02 Semester 1/2020 42
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.7 Arrays • An array is sequence of values of the same data type. • It is stored in the memory as a consecutive block of memory. • For example, a C statement long A[5]; defines an array which is composed of 5 elements. Each element is a 64-bit integer.
CSS224 Sec.01 & 02 Semester 1/2020 43
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.7.1 Accessing Array Elements • Since each array is a consecutive block of data, we can calculate the address of an element in the array from: 1. The starting address of the array, 2. The size in bytes of an element, and 3. The element index that we want to access.
• For example, suppose the starting address of the array A is s. The
element A[3] can be found at s+8*3 = s+24.
CSS224 Sec.01 & 02 Semester 1/2020 44
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.7.1 Accessing Array Elements • When we call a function in C and pass an array as an argument, the starting address is passed to the function. • We can use the starting address to compute the addresses of elements. • Example 2.8 Write a function sumArr(A,n) that computes the sum of the n elements in the array A.
CSS224 Sec.01 & 02 Semester 1/2020 45
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.7.1 Accessing Array Elements • When we call a function in C and pass an array as an argument, the starting address is passed to the function. • We can use the starting address to compute the addresses of elements. • Example 2.8 Write a function sumArr(A,n) that computes the sum of the n elements in the array A.
CSS224 Sec.01 & 02 Semester 1/2020 46
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. 2.8 Example of Assembly Programs
CSS224 Computer Architectures 47
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. Exercise • Exercise 2.3 Implement a function add1000(a) that returns a + 1000. The function will be used by the following C program.
CSS224 Computer Architectures 48
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. Exercise • Example 2.4 Implement a function ldgt(a) that returns the last digit of a.
CSS224 Computer Architectures 49
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. Exercise • Exercise 2.4 Implement a function exercise2(a,b,c) that returns c+2*(a+b)-(b+2*c) used by the following C program.
CSS224 Computer Architectures 50
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. Exercise • Exercise 2.5 Implement a function exercise3(a,b) that returns (a+b)%2 used by the following C program.
CSS224 Computer Architectures 51
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. Exercise • Exercise 2.6 Implement a function exercise4(a) that returns the second last digit of a.
CSS224 Computer Architectures 52
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. Exercise • Example 2.6 Write a function in assembly that checks and displays if an integer parameter is odd or even.
CSS224 Computer Architectures 53
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. Exercise • Exercise 2.7 Write a function maxoftwo(a,b) that returns the maximum value between a and b.
CSS224 Computer Architectures 54
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee. References 1. C.Nattee, Lecture Note of CSS224 Computer Architectures, 2017. 2. R.E. Bryant and D.R. O’Halloron, Computer Systems: A Programmer’s Perspective, 2nd edition, 2011, Pearson/Prentice Hall, ISBN:978-0-13- 610804-7.
CSS224 Sec.01 & 02 Semester 1/2020 55
This lecture is based on lecture note by Assoc.Prof.Dr.Cholwich Nattee.