1 / 24

Assembly

Assembly. תרגול 5 תכנות באסמבלי. 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.

kato
Download Presentation

Assembly

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Assembly תרגול 5תכנות באסמבלי

  2. 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. 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.

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

  5. 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. • 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).

  6. 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.

  7. %bx The File Register

  8. 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.

  9. Operand Forms

  10. Move to / from memory Instructions

  11. Important Suffixes • ‘l’ - double word. • ‘w’ - word. • ‘b’ - byte • ‘s’ - single (for floating point) • ‘t’ - special extension (– we won’t get into that!)

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

  13. 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 instruction serve to copy a byte and to set the remaining bits in the destination: • movsbl - signed extension. • movzbl - zero extension.

  14. Another example (Assume initially that %dh = 8D, %eax = 98765432) • movb %dh,%al %eax = 9876548D • movsbl %dh,%eax %eax = FFFFFF8D • movzbl %dh,%eax %eax = 0000008D

  15. C vs. Assemblyexample

  16. Arithmetic & Logical Operations

  17. 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

  18. “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. • This instruction can be used to generate pointers for later memory references.

  19. leal (2) • 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 = x+6 = x+y = x+4y = 9x+7 = 4y+10 =x+2y+9

  20. Shift • 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  movl 12(%ebp), %ecx  movl 8(%ebp), %eax  sall $2,%eax  sarl %cl,%eax

  21. C vs. Assembly example

  22. mul & div Instructions

  23. 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

  24. 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

More Related