Docstoc

asm2 _1_

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




                                                                  Assembler Programming
                      More                                    “A programming language is low level
                                                                  h its                      i tt ti t th
                                                               when it programs require attention to the
                Assembler Language                             irrelevant.” - Alan J. Perlis

                                                              “You can do everything in assembler, but
                             COMP375                                            p g
                                                               no one wants to program in assembler
                Computer Architecture and Organization         anymore.” - Yukihiro Matsumoto




                         Goals for Today                                 Intel Registers
                                                             • The Intel Pentium has eight 32-bit general-
             • Introduce assembler language constructs to:     p p        g
                                                               purpose registers
               – Compute integer equations
               – If statements
               – loops


                                      programs.
             • Write simple assembler programs




COMP375                                                                                                             1
Intro to Assembler                                                                                               8/24/2010




                          mov Instruction                                          Constants
             • The mov instruction moves data between             • Assembler programs can also use small
                          d
               memory and a register or b t
                                 i t    between t
                                                two                 constants.
                                                                    constants
               registers.                                         • If you want to move the number 47 to eax
             • The format is                                                mov eax,47
                   mov destination, source                        • Constants can also be characters
             • where destination and source can be                               al, Z
                                                                           mov al ‘Z’
                – register, memory to load data into a register   • You can also move constants to memory
                – memory, register to store data into memory               mov aardvark,15
                – register, register to move data between regs




                           Memory Model                                  Hardware Data Types
             • Memory is a huge one dimensional array             • The hardware provides only a few primitive
               of bytes.
                fb t                                                data types
                                                                    – long, int and short   (8, 4 & 2 bytes)
             • Both data and instructions are stored in
                                                                    – float and double      (4 & 8 bytes)
               memory.
                                                                    – char or byte          (1 byte)
             • Registers can hold copies of the data or
               the address of the data in memory
                                          memory.                       g         yp            g
                                                                  • Integer data types can be signed or
                                                                    unsigned




COMP375                                                                                                                 2
Intro to Assembler                                                                                  8/24/2010




                      Software Data Types                            Arithmetic
             • All other data types are created by   • All arithmetic and logical functions (AND,
                  ft
               software                                OR XOR etc.) appear to be done in the
                                                       OR, XOR, t )             t b d       i th
                   • strings                           registers.
                   • objects                         • Each instruction has one operand in a
                   • boolean                           register and the other in memory or
                                                       another register.
                   • multi-dimensional arrays
                                                                    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                                                                                                    3
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




                                 Big Operands                                        Multiplication
                                                                      • The imul signed multiply instruction has
                    • Multiplication and Division use two               three forms.
                         i t    t t                 l
                      registers to store a 64 bit value.
                                                                      • Multiply memory * EAX, save in EDX:EAX
                    • A number is stored in EDX:EAX with the
                                                                                 imul     memorreg
                      most significant bits in the EDX register
                      and the least significant bits in EAX.          • Multiply memory * register, save in register
                              EDX                     EAX                        imul     reg,
                                                                                          reg memorreg
                     bits 63, 62, … 33, 32   bits 31, 30 … 2, 1, 0




COMP375                                                                                                                       4
Intro to Assembler                                                                                              8/24/2010




                               Division                               Arithmetic Example 2
             • The 64 bit number in the EDX:EAX pair of        int dog=9, cat=4, bird=5, cow;
               registers is divided by the 32 bit value in a
               memory location or another register.            _asm { // cow = dog * cat / bird;
             • The resulting quotient is stored in EAX             mov   eax,dog ; move dog to eax reg
             • The resulting remainder is stored in EDX            imul cat          ; edx:eax = dog*cat
             • Since the EDX:EAX registers are always
                                       g                y          idiv bird         ; eax = dog*cat/bird
               used, you do not have to specify them.              mov   cow,eax ; save result in cow
                        idiv       memoryAddr                  }




                     Arithmetic Example 3                                    Shifting Bits
             int dog=9, cat=4, bird=5, cow;                    • You can move the bits in a register right or
                                                                 left.
             _asm { // cow = dog % cat + bird;
                 mov   eax,dog ; move dog to eax reg             00000101       Left shift 2     00010100
                 mov   edx,0      ; clear EDX
                 idiv cat         ; edx = dog % cat              11111011      Right shift 1     01111101
                 add   edx,bird ; add bird to dog % cat
                 mov   cow,edx ; save result in cow              11111011      Arith R shift 1   11111101
             }




COMP375                                                                                                                5
Intro to Assembler                                                                                                   8/24/2010




                                 Shifts                                        Why Shift?
             • The SHR and SHL instructions shift the bits      • Sometimes you need to separate some bits
               right or left by the specified number of bits.     from a word
             • The SAR and SAL instructions shift the bit       • Example: separate bits 2 – 5 of an int
               right or left, but not the sign bit. The SAR
               copies the sign bit into the emptied bits.              1 1        1 0 1 1         0 1
             • The shift count can be a constant or a
               number in the cl register                            mov   eax, someint ; get word
                                                                             ,           ge wo d
                               sar      eax, 5                      shr   eax, 2       ; shift desired bits to end
                               shl      eax, cl                     and   eax, 15       ; move other bits
                                                                    mov   results, eax  ; save results




                          Shift Arithmetic                                   Shift Example
                                                                int dog=3;
             • Shifting a number to the left multiplies it by   _asm {
               2 for each bit you shift.                            mov    eax,dog         ; eax = 3
             • Shifting a number to the right divides it by         sal    eax,2           ; eax = 12
               2 for each bit you shift.                            sar    eax,1           ; eax = 6
                                                                }




COMP375                                                                                                                     6
Intro to Assembler                                                                                          8/24/2010




              What is the result after shr ax,5                               Try It
               when the initial value of ax is             • Complete this program to compute the
                   1100000001100001                          average of two numbers
            1.   1111111000000011
                                                           int bull, dog, goat, two=2;
            2.   0000011000000011
                                                           cin >> bull >> dog;
            3.   0000110000100000
                                                           _asm{
            4.   1100000001100001
                                                               // set goat to the average of dog and bull
                                                           }
                                                           cout << goat << endl;




                        Possible Solution                      Another Possible Solution
             int bull, dog, goat,    two=2;                int bull, dog, goat;
             cin >> bull >> dog;                           cin >> bull >> dog;
             _asm{                                         _asm{
                 mov eax, bull        ; eax = bull             mov eax, bull    ; eax = bull
                 add eax, dog         ; eax = bull + dog       add eax, dog     ; eax = bull + dog
                 mov edx, 0           ; clear for divide       sar eax, 1       ; divide by 2
                 idiv    two          ;divide by 2
                 mov goat, eax        ; save result            mov goat, eax                ; save result
             }                                             }
             cout << goat << endl;                         cout << goat << endl;



COMP375                                                                                                            7
Intro to Assembler                                                                                       8/24/2010




                     Increment and Decrement                      Intel Status Register
             • The inc and dec instructions are one of    • The status register records the results of
               th few that can run on memory locations
               the f    th t                     l ti               ti th i t ti
                                                            executing the instruction.
               without using the registers.               • Performing arithmetic sets the status
             • You can increment or decrement the value     register.
               in a register for memory location          • The compare instruction does a
                                                            subtraction,       doesn’t        results
                                                            subtraction but doesn t store the results.
                            inc   eax                       It just sets the status flags.
                            dec   memoryAddr              • All jump instructions are based on the
                                                            status register




                       Intel Status Register                      Compare Instruction
                                                          • The cmp instruction compares two values
                                                              d t the t t fl                i t l
                                                            and sets th status flags appropriately.
                                                                 cmp register, operand
                                                          • where the operand can be a memory
                                                            location or a register
                                                          • The compare instruction subtracts the
                                                            operand from the register value, but does
                                                            not save the results.




COMP375                                                                                                         8
Intro to Assembler                                                                                              8/24/2010




                         Jump statements                                  Labels in Assembler
             • A JMP instruction (sometimes called
               branch) causes the flow of execution to g
                      )                                  go     • You can attach a name to a memory
               to a specified location.                           location i assembler. Thi allows you t
                                                                  l    ti in          bl    This ll        to
                                                                  use the name instead of numerical address
             • A JMP instruction loads the Program
               Counter with the specified address.              • Labels start in first column and end with a
                                                                  colon :
             • An unconditional jump always jumps.
                                                                           jmp rabbit
             • A conditional jump will do nothing if the
               condition is not met.                                       // some other stuff here
             • Some architectures have a separate               rabbit:    mov eax, dog
               compare instruction.




                                                                    The JS will jump if the last
               Jumps Based on Status Flags
                                                               arithmetic operation set the sign bit.
                 JE    Jump if equal          ZF=1
                 JZ    Jump if zero
                          p                   ZF=1
                                                                   This means the number was
                 JNE   Jump if not equal      ZF=0
                 JNZ   Jump if not zero       ZF=0                   1.   Positive
                 JLE   Jump if less or equal ZF=1 or SF≠OF           2.   Negative
                 JL    Jump if less           SF≠OF                  3.   Even
                 JNS   J          t i
                       Jump if not sign       SF=0
                                              SF 0
                                                                     4.   Odd
                 JS    Jump if sign           SF=1
                 JGE   Jump if greater or equal SF=OF
                                                                     5.   Zero
                 JG    Jump if greater        ZF=0 and SF=OF




COMP375                                                                                                                9
Intro to Assembler                                                                                 8/24/2010




                        Program Counter                              Software Controls
                                                           • Assembler only has a simple compare
             • The Program Counter or Instruction
                                                             instruction. Jumps are based on the
                                         the dd
               Pointer Register contains th address          compare.
               of the next instruction to be               • Assembler does not have:
               executed.                                          –for
             • At the beginning of the fetch/execute              –while
               cycle, the CPU fetches the instruction             –do while
               whose address is in the program counter.           –switch
             • A jump instruction is just a load of the           –break
               program counter register.                          –else portion of an if




                           If statements                                          Try It
             • The high level language IF statement is     • Complete this program in assembler
               easily implemented by a conditional jump.   int cow=0, dog=0, cat=3;
                                                                              g
                                      MOV    eax,cat       cin >> dog >> cat;
              if (cat ==dog)          CMP    eax,dog       _asm{
                 cow = goat;          JNE    noteq
              else                    MOV    edx,goat          if (dog > cat)
                 cow = bull;          JMP    after                   cow = 1;
                               noteq: MOV    edx,bull
                                                               else
                               after: MOV    cow,edx
                                                                     dog = cat;
                                                           }




COMP375                                                                                                  10
Intro to Assembler                                                                                         8/24/2010




                                Loops                                           Try It
             • There are usually no hardware instructions   • Complete this program in assembler
                           y p             p (i.e. for,
               that directly implement loops (
               while, do)                                   int cow=0, dog=0, cat=3;
             • Loops are implemented with conditional       _asm{
               jumps.               again: mov eax,what
                                                                do {       // convert this to assembler
                                           cmp eax,ever
            while (what == ever) {
                  (            )                                     cow++;
                                                                     cow ;
                                           j
                                           jne       dl
                                                  endloop
                 // something             // something               dog = dog + cat;
                                           jmp again            } while (dog < 12);
                 }
                                     endloop:               }




                        Possible Solution                                No Operation
               cow=0; dog=0; cat=3;
                                                            • The NOP instruction is a one byte
               asm{
              _as {
                                                              instruction th t d
                                                              i t ti that does nothing. thi
                  mov eax, dog  ; put dog in eax
                                                            • Executing a NOP does not change any
              again:                                          registers or status bits.
                  inc cow       ; cow++                     • When patching a machine language
                  add eax cat
                      eax,      ; add cat to dog              program it is sometimes useful to be able
                                                              program,
                  cmp eax, 12   ; < 12 ?                      to add a few extra instructions that don’t
                  jl again      ; repeat if not               change the program.
               }




COMP375                                                                                                          11

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:10
posted:4/14/2011
language:English
pages:11
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