Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same registers.

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Advertisements

Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Department of Computer Science and Software Engineering
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Machine-Level Programming I: Introduction Apr. 14, 2008 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Assembly Language – Lab 5
6.828: PC hardware and x86 Frans Kaashoek
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Linked Lists in MIPS Let’s see how singly linked lists are implemented in MIPS on MP2, we have a special type of doubly linked list Each node consists.
INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Multiplication and Division Instructions & the 0Ah function.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Assembly תרגול 5 תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University Machine Level Programming.
1 Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2015 Instructor: John Barr * Modified slides.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
1 Carnegie Mellon Assembly and Bomb Lab : Introduction to Computer Systems Recitation 4, Sept. 17, 2012.
University of Washington x86 Programming I The Hardware/Software Interface CSE351 Winter 2013.
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 21, 2013 These slides are from website which accompanies the.
תרגול 5 תכנות באסמבלי, המשך
ICS 312 SET 10 Multiplication & Division & input using function 0Ah.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
Multiplication and Division instructions Dr.Hadi AL Saadi.
1 Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
Instruction Set Architecture
Assembly Language Programming IV: shift, struct, recursion
Homework Reading Lab with your assigned section starts next week
Assembly language.
Credits and Disclaimers
Data Transfers, Addressing, and Arithmetic
IA32 Processors Evolutionary Design
Credits and Disclaimers
Homework In-line Assembly Code Machine Language
Homework Reading Continue work on mp1
The University of Adelaide, School of Computer Science
Assembly Language Programming V: In-line Assembly Code
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming 1 Introduction
Condition Codes Single Bit Registers
Machine-Level Programming 2 Control Flow
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
MIPS Procedure Calls CSE 378 – Section 3.
Machine-Level Programming 2 Control Flow
Multiplication and Division Instructions
Machine-Level Programming: Introduction
Week 2: Buffer Overflow Part 1.
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming II: Control Flow
X86 Assembly Review.
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
Credits and Disclaimers
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Assembly תכנות באסמבלי

Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same registers.  We need to know what we are working it in order to use the right instructions.  Memory = a large, byte-addressable array. Only a limited set of registers is used to store data while running the program.  If we need more room we must save the data into memory and later reread it. No special structures (instructions) for “if” / “switch” / “loops” (for, while, do-while), or even functions!

3 תזכורת: תהליך קומפילציה

4 תמיכת שפת C בכתיבת מרובת קבצים  כתיבת מודולים הוטמעה בשפת C בכך שבניית התוכנה נעשית בשני שלבים: 1. הידור 1. הידור (compilation) של כל קובץ מקור (.c ) בנפרד 2. קישור 2. קישור (linking) של כל הקבצים ביחד 4

Linux Compilation Process Five stages : Preprocessing, Parsing, Translation, Assembling, and Linking All 5 stages are implemented by one program in UNIX, gcc gcc is the C compiler of choice for most UNIX. Actually just a front end that executes various other programs corresponding to each stage in the compilation process. To get it to print out the commands it executes at each step, use gcc -v. 5

How to - Disassembly of code Compilation of code:  gcc -c code.c  We get the file: code.o Disassembly:  objdump -d code.o  We get an assembly-like code that represents the c code appeared in file code.c Or:  gcc -s code.c  We get a code.s file that contains an assembly code created by the compiler.

Standard data types Assembly In Assembly: size = type of variable.

Words, double words …. Due to its origins as a 16-bit architecture that expanded into a 32-bit one, Intel uses the term “word” to refer to a 16-bit data type and not a 32- bit data type as we mentioned before. Backward Compatibility. 32-bit quantities as “double words”. 64-bit quantities as “quad words”. Most instructions we will encounter operate on bytes or double words. Each instruction has 3 variants, depending on its suffix (‘b’ – byte / ‘w’ – word / ‘l’ – double word).

The Registers An IA32 CPU contains a set of eight registers storing 32-bit values. These registers are used to store integer data as well as pointers. The registers names all begin with %e (extend), but otherwise they have peculiar names. In the original 8086 CPU each register had a specific target (and hence it got its name). Today most of these targets are less significant.  Some instructions use fixed registers as sources and/or destinations.  Within procedures there are different conventions for saving and restoring the first three registers (%eax, %ecx, and %edx), than for the next three (%ebx, %edi, and %esi).  %ebp and %esp contain pointers to important places in the program stack.

The File Register

Partial access to a register The low-order two bytes of the first four registers can be independently read or written by the byte operation instructions. This feature was provided to allow backward compatibility. When a byte instruction updates one of these single-byte “register elements,” the remaining three bytes of the register do not change. Same goes for the low-order 16 bits of each register, using word operation instructions.

Operand Forms

Move to / from memory Instructions

Important Suffixes ‘l’ - double word. ‘w’ - word. ‘b’ - byte ‘s’ - single (for floating point)

movl Operand Combinations Cannot do memory-memory transfers with single instruction !!! movl Imm Reg Mem Reg Mem Reg Mem Reg SourceDestination movl $0x4,%eax movl $-147,(%eax) movl %eax,%edx movl %eax,(%edx) movl (%eax),%edx C Analog temp = 0x4; *p = -147; temp2 = temp1; *p = temp; temp = *p;

Important prefixes i – immediate (constant) value. r – register. m – memory.  rrmovl = move double word from one register to the other.  irmovw = move the word given as immediate value into the register.

movb & movw The movb instruction is similar, but it moves just a single byte. When one of the operands is a register, it must be one of the eight single-byte register elements. Similarly, the movw instruction moves two bytes. When one of its operands is a register, it must be one of the eight two-byte register elements. Both the movsbl and the movzbl instructions serve to copy a byte and to set the remaining bits in the destination:  movsbl - signed extension.  movzbl - zero extension.

MOVSBL and MOVZBL MOVSBL sign-extends a single byte, and copies it into a double-word destination MOVZBL expands a single byte to 32 bits with 24 leading zeros, and copies it into a double-word destination Example: %eax = 0x %edx = 0xAAAABBBB MOVB %dh, %al %eax = 0x123456BB MOVSBL %dh, %eax %eax = 0xFFFFFFBB MOVZBL %dh, %eax %eax = 0x000000BB 18

Another example (Assume initially that %dh = 8D, %eax = ) movb %dh,%al  %eax = D movsbl %dh,%eax  %eax = FFFFFF8D movzbl %dh,%eax  %eax = D

C vs. Assembly example

Arithmetic & Logical Operations

Arithmetic & Logical Operations (2) With the exception of leal, each of these instructions has a counterpart that operates on words (16 bits) and on bytes (by replacing the suffix). Again, cannot do memory-memory transfers with single instruction

“Load Effective Address” (leal) The “Load Effective Address” (leal) instruction is actually a variant of the movl instruction. Its first operand appears to be a memory reference, but instead of reading from the designated location, the instruction copies the effective address to the destination. Doesn’t access memory !!! This instruction can be used to generate pointers for later memory references without accessing memory.

The leal Instruction can be used to compactly describe common arithmetic operations. If register %edx contains value x, then the instruction: leal 7(%edx,%edx,4), %eax will set register %eax to 5x + 7. It is commonly used to perform simple arithmetic:  (%eax = x; %ecx = y)  leal 6(%eax), %edx  leal (%eax,%ecx), %edx  leal (%eax,%ecx,4), %edx  leal 7(%eax,%eax,8), %edx  leal 0xA(,%ecx,4), %edx  leal 9(%eax,%ecx,2), %edx leal (2) = x+6 = x+y = x+4y = 9x+7 = 4y+10 =x+2y+9

Logical Shift Logical Shift - every bit in the operand is simply moved a given number of bit positions, and the vacant bit-positions are filled in, usually with zeros. Useful as multiplication or division of unsigned integers by powers of two. 25

Arithmetic Right Shift A bitwise operation that shifts all of the bits of its operand Every bit in the operand is simply moved a given number of bit positions, and the vacant bit- positions are filled in with the leftmost bit. When shifting to the right - leftmost bit- sign bit - sort of sign extension. Dividing by 2 n 26

Either logical or arithmetic k is a number between 0 and 31, or the single-byte register %cl Suppose that x and n are stored at memory locations with offsets 8 and 12, respectively, relative to the address in register %ebp  get n  get x  x <<= 2  x >>= n Shift  movl 12(%ebp), %ecx  movl 8(%ebp), %eax  sall $2,%eax  sarl %cl,%eax

C vs. Assembly example

mul & div Instructions

Imull Example mov , eax mov -423, ebx imul ebx ; What happens is : EDX:EAX = FFFFFFFFh:86635D80h תמר שרוט, נועם חזון 30

Cltd - converts the signed long in EAX to a signed double long in EDX:EAX by extending the most-significant bit (sign bit) of EAX into all bits of EDX iDiv - The idiv instruction divides the contents of the 64 bit integer EDX:EAX (EDX as the most significant four bytes and EAX as the least significant four bytes) by the specified operand value. The quotient result of the division is stored into EAX, while the remainder is placed in EDX. תמר שרוט, נועם חזון 31

Code example (x at %ebp+8, y at %ebp+12) movl 8(%ebp),%eax  Put x in %eax imull 12(%ebp)  Multiply by y pushl %edx  Push high-order 32 bits pushl %eax  Push low-order 32 bits

Yet, another example (x at %ebp+8, y at %ebp+12) movl 8(%ebp),%eax  Put x in %eax cltd  Sign extend into %edx idivl 12(%ebp)  Divide by y pushl %eax  Push x / y pushl %edx  Push x % y