Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.

Slides:



Advertisements
Similar presentations
Machine Instructions Operations
Advertisements

Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1.ppt Modification date: March 18, 2015.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.c: Logical Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
Computer Organization & Assembly Language
80x86 Instruction Set Dr. Qiang Lin.
1 Homework / Exam Reading –PAL, pp Homework –mp2 due before class number 12 Exam #1 –Class 13 (three sessions from today) –Open book / Open notes.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
Introduction to Assembly Here we have a brief introduction to IBM PC Assembly Language –CISC instruction set –Special purpose register set –8 and 16 bit.
Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Shift and Rotate Instructions
Introduction to Computer Engineering by Richard E. Haskell Shift and Rotate Instructions Module M16.2 Section 10.3.
Ch. 7 Logic, Shift and Rotate instr.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
Department of Computer Science and Software Engineering
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
Arithmetic Flags and Instructions
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1.
Logical and Bit Operations Chapter 9 S. Dandamudi.
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
Lecture 12 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Computer Architecture and Assembly Language
Assembly Language Programming IV: shift, struct, recursion
Data Transfers, Addressing, and Arithmetic
Microprocessor Systems Design I
ICS312 SET 7 Flags.
Chapter 3 Bit Operations
Microprocessor Systems Design I
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Assembly Language Programming Part 2
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Chapter 4: Instructions
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah –
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Shift & Rotate Instructions)
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah –
Multiplication and Division Instructions
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Shift & Rotate Instructions)
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Shift, Multiply, and Divide
Computer Architecture and System Programming Laboratory
X86 Assembly Review.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Shift and Rotate Instructions.
Computer Architecture and System Programming Laboratory
CS-401 Computer Architecture & Assembly Language Programming
Presentation transcript:

Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail

All materials is licensed under a Creative Commons “Share Alike” license. http://creativecommons.org/licenses/by-sa/3.0/ Attribution condition: You must indicate that derivative work "Is derived from Xeno Kovah's ‘Intro x86-64’ class, available at http://OpenSecurityTraining.info/IntroX86-64.html" Attribution condition: You must indicate that derivative work "Is derived from Xeno Kovah's 'Intro x86-64’ class, available at http://OpenSecurityTraining.info/IntroX86-64.html”

ShiftExample1.c Whither the multiply and divide instructions?! #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { unsigned int a, b, c; a = atoi(argv[0]); b = a * 8; c = b / 16; return c; } Note: Compiled with “Maximize Speed”, to clear away a bit of cruft main: 0000000140001010 sub rsp,28h 0000000140001014 mov rcx,qword ptr [rdx] 0000000140001017 call qword ptr [40008368h] 000000014000101D shl eax,3 0000000140001020 shr eax,4 0000000140001023 add rsp,28h 0000000140001027 ret Whither the multiply and divide instructions?!

SHL - Shift Logical Left Book p. 224 21 SHL - Shift Logical Left Can be explicitly used with the C “<<” operator First operand (source and destination) operand is an r/mX Second operand is either cl (lowest byte of ecx), or a 1 byte immediate. The 2nd operand is the number of places to shift. It multiplies the register by 2 for each place the value is shifted. More efficient than a multiply instruction. Bits shifted off the left hand side are “shifted into” (set) the carry flag (CF) For purposes of determining if the CF is set at the end, think of it as n independent 1 bit shifts. shl bl, 2 shl bl, 3 00110011b (bl - 0x33) result 11001100b (bl - 0xCC) CF = 0 00110011b (bl - 0x33) result 10011000b (bl - 0x98) CF = 1

SHR - Shift Logical Right Book p. 225 22 SHR - Shift Logical Right Can be explicitly used with the C “>>” operator First operand (source and destination) operand is an r/mX Second operand is either cl (lowest byte of ecx), or a 1 byte immediate. The 2nd operand is the number of places to shift. It divides the register by 2 for each place the value is shifted. More efficient than a multiply instruction. Bits shifted off the right hand side are “shifted into” (set) the carry flag (CF) For purposes of determining if the CF is set at the end, think of it as n independent 1 bit shifts. shr bl, 2 shr bl, 3 00110011b (bl - 0x33) result 00001100b (bl - 0x0C) CF = 1 00110011b (bl - 0x33) result 00000110b (bl - 0x06) CF = 0

ShiftExample1.c takeaways When a multiply or divide is by a power of 2, compilers prefer shift instructions as a more efficient way to perform the computation #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { unsigned int a, b, c; a = atoi(argv[0]); b = a * 8; c = b / 16; return c; } main: 0000000140001010 sub rsp,28h 0000000140001014 mov rcx,qword ptr [rdx] 0000000140001017 call qword ptr [40008368h] 000000014000101D shl eax,3 0000000140001020 shr eax,4 0000000140001023 add rsp,28h 0000000140001027 ret

That’s the power of love sign! #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { unsigned int a, b, c; a = atoi(argv[0]); b = a * 8; c = b / 16; return c; } #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { int a, b, c; a = atoi(argv[0]); b = a * 8; c = b / 16; return c; } http://4.bp.blogspot.com/-FAaWCtna3mw/Tc69R-mPbFI/AAAAAAAABxA/Nriylz_dc20/s1600/hlhlfr.jpeg

ShiftExample2.c #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { int a, b, c; a = atoi(argv[0]); b = a * 8; c = b / 16; return c; } main: 0000000140001010 sub rsp,28h 0000000140001014 mov rcx,qword ptr [rdx] 0000000140001017 call qword ptr [40008368h] 000000014000101D shl eax,3 0000000140001020 cdq 0000000140001021 and edx,0Fh 0000000140001024 add eax,edx 0000000140001026 sar eax,4 0000000140001029 add rsp,28h 000000014000102D ret

main: 0000000140001010 sub rsp,28h 0000000140001014 mov rcx,qword ptr [rdx] 0000000140001017 call qword ptr [40008368h] 000000014000101D shl eax,3 0000000140001020 shr eax,4 0000000140001023 add rsp,28h 0000000140001027 ret Vs main: 0000000140001010 sub rsp,28h 0000000140001014 mov rcx,qword ptr [rdx] 0000000140001017 call qword ptr [40008368h] 000000014000101D shl eax,3 0000000140001020 cdq 0000000140001021 and edx,0Fh 0000000140001024 add eax,edx 0000000140001026 sar eax,4 0000000140001029 add rsp,28h 000000014000102D ret Changed CD* is added as an VS-ism. It’s necessary for the math to work out, but I feel like I’ve only run into it once ever in the wild. So I don’t consider it that important for beginners to know and I’m skipping it. But you can feel free to come back and read this code once we’ve gone through the RTFM section.

SAR - Shift Arithmetic Right Book p. 224 23 SAR - Shift Arithmetic Right Can be explicitly used with the C “>>” operator, if operands are signed First operand (source and destination) operand is an r/mX Second operand is either cl (lowest byte of ecx), or a 1 byte immediate. The 2nd operand is the number of places to shift. It divides the register by 2 for each place the value is shifted. More efficient than a multiply instruction. Each bit shifted off the right side is placed in CF. sar bl, 2 shr bl, 2 10110011b (bl - 0xB3) result 11101100b (bl - 0xEC) 10110011b (bl - 0xB3) result 00101100b (bl - 0x2C) != mov cl, 2; sal bl, cl mov cl, 2; sal bl, cl 00110011b (bl - 0x33) result 00001100b (bl - 0x0C) 00110011b (bl - 0x33) result 00001100b (bl - 0x0C) ==

SAL - Shift Arithmetic Left Book p. 225 24 SAL - Shift Arithmetic Left Actually behaves exactly the same as SHL! First operand (source and destination) operand is an r/mX Second operand is either cl (lowest byte of rcx), or a 1 byte immediate. The 2nd operand is the number of places to shift. It divides the register by 2 for each place the value is shifted. More efficient than a multiply instruction. Each bit shifted off the left side is placed in CF. sal bl, 2 shl bl, 2 10110011b (bl - 0xB3) result 11001100b (bl - 0xCC) 10110011b (bl - 0xB3) result 11001100b (bl - 0xCC) == mov cl, 2; sal bl, cl mov cl, 2; sal bl, cl 00110011b (bl - 0x33) result 11001100b (bl - 0xCC) 00110011b (bl - 0x33) result 11001100b (bl - 0xCC) ==

ShiftExample2.c takeaways Compilers still prefer shifts for mul/div over powers of 2 But when the operands are signed rather than unsigned, it must use different instructions, and potentially do more work (than the unsigned case) to deal with a multiply CDQ isn’t important for beginners to know, left as an exercise for the reader for later ;) main: 0000000140001010 sub rsp,28h 0000000140001014 mov rcx,qword ptr [rdx] 0000000140001017 call qword ptr [40008368h] 000000014000101D shl eax,3 0000000140001020 cdq 0000000140001021 and edx,0Fh 0000000140001024 add eax,edx 0000000140001026 sar eax,4 0000000140001029 add rsp,28h 000000014000102D ret int main(){ int a, b, c; a = 0x40; b = a * 8; c = b / 16; return c; }

Instructions we now know (26) NOP PUSH/POP CALL/RET MOV ADD/SUB IMUL MOVZX/MOVSX LEA JMP/Jcc (family) CMP/TEST AND/OR/XOR/NOT INC/DEC SHR/SHL/SAR/SAL