asm1

Document Sample
asm1 Powered By Docstoc
					Intro to Assembler                                                                                        8/24/2010




                                                                      Goals for Today
                                                           • Introduce Intel assembler language.
                Assembler Language                         • Learn how to write simple assembler
                                                             programs.

                             COMP375
                Computer Architecture and Organization




                     High and Low Levels                                  Microcode
             • C++, Java, Pascal, Cobol, Basic and the     • Assembler or machine language is the
               like      ll hi h level l
               lik are all high l                 They
                                     l languages. Th are     lowest level access a programmer h t
                                                             l      tl    l                      has to
               machine independent. The machine              the hardware.
               dependent compilers translate the high      • Internally, the machine language is
               level language to machine language.           implemented with microcode.
             • Assembler language is a low level           • Microcode is a series of instruction words
               language. Each different computer             that turn switches on and off to implement
               architecture has its own assembler            each machine language instruction.
               language.




COMP375                                                                                                          1
Intro to Assembler                                                                                                                               8/24/2010




                            Compilers                                          Compiled Results
                                                             // C++ or Java method                    // Portion of same method in assembler
                                                                                                                 mov     eax, PTR hand+32
                                                             int isflush( void ) {
             • A compiler translates a high level            int i, ok;
                                                                                                                 add     eax, 117
                                                                                                                 mov             [ p],
                                                                                                                          PTR ok[ebp], eax
               l              h
               language, such as C or C C++, i t machine
                                             into    hi      ok = FLUSHBASE + hand[4].face - 5;                  mov      PTR i[ebp], 0
                                                                                                                 jmp     SHORT label4
                                                             for (i = 0; i < 4; i++)
               language.                                          if (hand[i].suit != hand[i+1].suit)
                                                                                                      label3:    mov     eax, PTR i[ebp]
                                                                                                                 add     eax, 1
             • Each line of a high level language usually                ok = 0;
                                                                                                      label4:
                                                                                                                 mov
                                                                                                                 cmp
                                                                                                                          PTR i[ebp], eax
                                                                                                                          PTR i[ebp], 4
                                                             return ok;
               produces many machine instructions.           }
                                                                                                                 jge
                                                                                                                 mov
                                                                                                                         SHORT label2
                                                                                                                         eax, PTR i[ebp]
                                                                                                               mov     ecx, PTR i[ebp]
             • Compilers can rearrange the instructions                                                        mov      d        h d[   *8 4]
                                                                                                                       edx, PTR hand[eax*8+4]
                                                                                                               cmp     edx, PTR hand[ecx*8+12]
               to produce optimal code.                                                                        je      SHORT label1
                                                                                                               mov     PTR ok[ebp], 0
                                                                                                     label1:   jmp     SHORT label3
                                                                                                     label2:   mov     eax, PTR ok[ebp]




                     Architecture Specific                                    Writing Assembler
             • The same C++, Fortran, Pascal or Java        • You need an assembler program that will
                         d       be      il d
               source code can b compiled on any              t
                                                              translate your assembler source i t machine
                                                                   l t              bl         into      hi
               architecture. When executed, it will give      language.
               the same results.                            • Microsoft Visual Studio (any version) will
             • Each architecture has its own assembler        allow you to embed assembler into a C++
               language. Assembler for one type of            program.
               machine will not run on another machine.     • Details on using inline assembler are at
             • Assembler language is a simplified way of    http://msdn2.microsoft.com/en-us/library/4ks26t93(VS.80).aspx
               writing machine language.




COMP375                                                                                                                                                 2
Intro to Assembler                                                                                                  8/24/2010




                 But I only know Java!                                        Inline Assembler
             • Our simple C programs look                          • You can insert assembler code in a C++
               just lik Java
               j t like J                                                       i Microsoft Vi
                                                                     program using Mi              l Studio.
                                                                                           ft Visual St di
             • The source code for all of the                           _asm {
               C programs shown in the                                        assembler code here
               slides are available on the class website                }
             • Cut and paste your stuff into the examples          • You can reference C++ program variables
                                                                     by name.

             •   These are simple programs
             •   We don’t need no stinking classes




                          Assembler Programmer’s
                                                                                   Registers
                           Model of the Processor
             • Registers                                           • Registers are high speed, temporary
                  – Everything moves through the registers            t      i the
                                                                     storage in th processor.
                  – Arithmetic appears to occur in the registers   • User registers are the ones you can
             • Status Register                                       manipulate directly with assembler.
                  – Updated automatically by most instructions     • The number of registers varies with the
                  – Status bits are the basis for jumps              architecture The Pentium has 8 IBM
                                                                     architecture.                    8.
             • Instructions and data are in memory                   mainframes have 16, Itanium has 32.
                  – The assembler program deals with addresses     • In some architectures, all registers are the
                                                                     same. In others, registers are specialized.




COMP375                                                                                                                    3
Intro to Assembler                                                                                               8/24/2010




                     Registers Do Everything                             Usual Assembler
                                                                                 dog = cat;
             • All data moves through the registers.
                                                              • move the value from memory location cat into a
                – Register to register instructions             register
                – Memory to register instructions             • move the value from the register to memory
                – Memory to memory instructions (rare)          location dog
             • Although arithmetic is done in the ALU, it                    dog = cat + cow;
               appears to be done in the register.                                         y
                                                              • move the value from memory location cat into a
             • Registers can hold addresses.                    register
             • Instructions accessing data in memory can      • Add the value from memory location cow to the
                                                                register
               use an index register to specify the address
                                                              • Move the value from register to memory dog




            Intel Pentium has 8 User Registers                           Changing Names

                 EAX   General use, division only in EAX      • The first IBM PC had an Intel 8088 with 16
                       General use
                                                                       i t
                                                                bit registers.
                 EBX
                       General use
                                                              • The registers were named AX, BX, etc.
                 ECX
                       General use
                                                              • When Intel extended the processor to 32
                 EDX
                                                                bit registers, they called the longer
                 EBP   Base pointer
                                                                           EAX, EBX, etc.
                                                                registers EAX EBX etc
                 ESI   Source pointer
                                                              • AX is the lower 16 bits of EAX.
                 EDI   Destination pointer
                                                              • AH and AL are the high and low byte of
                 ESP   Stack pointer                            the 16 bit register, now bytes 3 & 4.




COMP375                                                                                                                 4
Intro to Assembler                                                                                           8/24/2010




                            Intel Registers                        General or Specialized
             • The Intel Pentium has eight 32-bit general-   • In some architectures all of the registers
                                                               have the same functionality. In other
               purpose registers
               p p        g                                    machines the registers each have a specific
                                                               purpose.
                                                             • The Intel registers have special purposes,
                                                               although most can do several operations.
                                                               – EAX   Accumulator for arithmetic
                                                               – EBX   Pointer to d t
                                                                       P i t t data
                                                               – ECX   Counter for loop operations
                                                               – EDX   I/O pointer
                                                               – ESP   Stack pointer




               How many user registers does                              Load and Store
                 the Intel Pentium have?
                                                             • A Load instruction copies a value from
                     A.4
                     A4                                                  into    i t    (Reads
                                                               memory i t a register. (R d memory)      )
                                                             • A Store instruction copies a value from a
                     B.8
                                                               register into memory. (Writes memory)
                     C.16                                    • The Intel assembler is confusing because
                     D.32
                     D 32                                     it uses th same mnemonic mov f both
                                                                      the           i      for b th
                                                              load and store.




COMP375                                                                                                             5
Intro to Assembler                                                                                                                 8/24/2010




                           mov Instruction                                      Assignment Statements
                                                                           int    cat=3, dog=5;
             • The mov instruction moves data between                      short bird=2, worm=7;
                          d
               memory and a register or b t
                                 i t            two
                                        between t                          char cow 41 goat 75; //note: char is one byte integer
                                                                                  cow=41, goat=75;
                                                                         _asm {
               registers.
                                                                               mov eax, cat        ; dog = cat
             • The format is                                                   mov dog, eax
                   mov destination, source                                     mov   cx, bird          // worm = bird
             • where destination and source can be                             mov   worm, cx
                – register, memory to load data into a register                mov   bl, goat          /* cow = goat */
                – memory, register to store data into memory                   mov   cow, bl
                – register, register to move data between regs             }
                                                                  asm1




                     Hardware Data Types                                          Software Data Types
             • The hardware provides only a few primitive                • All other data types are created by
               data types                                                     ft
                                                                           software
                – long, int and short     (8, 4 & 2 bytes)                  – strings
                – float and double        (4 & 8 bytes)                     – objects
                – char or byte            (1 byte)                          – boolean
                   g         yp            g
             • Integer data types can be signed or                            multi dimensional
                                                                            – multi-dimensional arrays
               unsigned




COMP375                                                                                                                                   6
Intro to Assembler                                                                                       8/24/2010




                         Memory Model                                     Arithmetic
             • Memory is a huge one dimensional array     • All arithmetic and logical functions (AND,
                fb t
               of bytes.                                    OR XOR etc.) appear to be done in the
                                                            OR, XOR, t )             t b d       i th
             • Both data and instructions are stored in     registers.
               memory.                                    • Each instruction has one operand in a
             • Registers can hold copies of the data or     register and the other in memory or
               the address of the data in memory
                                          memory.           another register.
                                                                         add eax, dog
                                                          • The result is saved in the first register.




            Arithmetic and Logical Instructions                    Arithmetic Example
                     mnemonic   operation
                                                          int dog=3, cat=4, bird=5;
                        ADD     Add
                                                          _asm { // bird = dog + cat;
                        SUB     Subtract
                                                              mov eax,dog
                        MUL     Unsigned Multiply
                                                              add eax,cat
                       IMUL     Signed Multiply
                                                              mov bird,eax
                        DIV     Unsigned Divide
                                                          }
                        IDIV    Signed Divide
                        AND     Logical AND
                         OR     Logical OR




COMP375                                                                                                         7
Intro to Assembler                                                                                              8/24/2010




                           Arithmetic Example 2                  What value is in EAX at the end?
                                                                 int dog=4, cat=3,
                                                                 bird=5;
                    int dog=3, cat=4, bird=5, cow;               _
                                                                 _asm {
                    _asm { // cow = dog + cat - bird;                mov eax,dog
                        mov eax,dog                                  sub eax,cat
                                                                     mov bird,eax
                        add eax,cat                              }
                        sub eax,bird                                    1.   1
                        mov cow,eax                                     2
                                                                        2.   2
                    }                                                   3.   3
                                                                        4.   4
           asm1-2                                                       5.   5




                        Increment and Decrement                                  Big Operands
                    • The inc and dec instructions are one of     • Multiplication and Division use two
                      th few that can run on memory locations
                      the f    th t                     l ti           i t    t t                 l
                                                                    registers to store a 64 bit value.
                      without using the registers.                • A number is stored in EDX:EAX with the
                    • You can increment or decrement the value      most significant bits in the EDX register
                      in a register for memory location             and the least significant bits in EAX.
                                     inc eax
                                     dec memoryAddr




COMP375                                                                                                                8
Intro to Assembler                                                                                                8/24/2010




                          Multiplication                                          Division
             • The imul signed multiply instruction has         • The 64 bit number in the EDX:EAX pair of
               three forms.                                       registers is divided by the 32 bit value in a
             • Multiply memory * EAX                              memory location or another register.
                        imul     memory                         • The resulting quotient is stored in EAX
             • Multiply memory * register                       • The resulting remainder is stored in EDX
                        imul     reg, memory                    • Since the EDX:EAX registers are always
                                                                                          g                y
             • Multiply the value in the memory location          used, you do not have to specify them.
               times the constant and store the result in the              idiv       memoryAddr
               register
                        imul       reg, memory, const




                     Arithmetic Example 3                               Arithmetic Example 4
             int dog=3, cat=4, bird=5, cow;                     int dog=3, cat=4, bird=5, cow;
             _asm { // cow = dog * cat / bird;                  _asm { // cow = dog % cat - bird;
                 mov   eax,dog                                      mov   eax,dog
                 imul cat                                           mov   edx,0      ; clear EDX
                 idiv bird                                          idiv cat
                 mov   cow,eax                                      sub   edx,bird
             }                                                      mov   cow,edx
                                                                }




COMP375                                                                                                                  9
Intro to Assembler                                                                                8/24/2010




                                Shifts                                    Shift Example
             • The shift instructions can shift the values in
               a register or memory location.                   int dog=3;
             • The SHR and SHL instructions shift the bits      _asm {
               right or left by the specified number of bits.       mov    eax,dog   ; eax = 3
             • The SAR and SAL instructions shift the bit           sal    eax,2     ; eax = 12
               right or left, but not the sign bit. The SAR         sar    eax,1     ; eax = 6
                                                      bits.
               copies the sign bit into the emptied bits
                                                                }
             • The shift count can be a constant or the cl
               reg
               sar       eax, 5           shl eax, cl




COMP375                                                                                                 10

				
DOCUMENT INFO
ALAPATI RAVEENDRA ALAPATI RAVEENDRA ALAPATI http://photos-bloger.blogspot.com
About i am a good person, i am sudying mca in viajayawada,my birth place is prakasam district