asm3 by balu051989


									Still More Assembler                                                                                       8/29/2008

                                                                       Goals for Today
                   Still More                              • Introduce assembler language constructs to:
                                                             – If statements
             Assembler Programming                           – Loops
                                                             – Index an array

                 COMP375 Computer Architecture
                        d Organization
                      and O    i ti                                                 programs
                                                           • Write simple assembler programs.

                         No Operation                               Intel Status Register
            • The NOP instruction is a one byte            • The status register records the results of
              i t ti that does nothing.
              instruction th t d        thi                          ti th i t ti
                                                             executing the instruction.
            • Executing a NOP does not change any          • Performing arithmetic sets the status
              registers or status bits.                      register.
            • When patching a machine language             • The compare instruction does a
              program it is sometimes useful to be able      subtraction,       doesn’t        results
                                                             subtraction but doesn t store the results.
              to add a few extra instructions that don’t     It just sets the status flags.
              change the program.                          • All jump instructions are based on the
                                                             status register

COMP375 Computer Architecture and Organization                                                                    1
Still More Assembler                                                                                            8/29/2008

                       Intel Status Register                  Flag Setting During Execution
                                                          int dog=-3, cat=3, bird=5, cow;
                                                          _asm { // cow = dog + cat - bird;
                                                                                   Zero Sign Carry   Overflow
                                                                                   0    0    0       0

                                                                mov   eax,dog      0    0    0       0

                                                                add   eax,cat      1    0    0       0

                                                                                   0    1    0       0
                                                                sub   eax bird
                                                                                   0    1    0       0
                                                                mov   cow,eax

                       Compare Instruction                            Jump statements
                                                          • A JMP instruction (sometimes called
            • The cmp instruction compares two values
                                                            branch) causes the flow of execution to g go
                d t the t t fl                i t l
              and sets th status flags appropriately.       to a specified location.
                   cmp register, operand                  • A JMP instruction loads the Program
            • where the operand can be a memory             Counter with the specified address.
              location or a register                      • An unconditional jump always jumps.
            • The compare instruction subtracts the       • A conditional jump will do nothing if the
              operand from the register value, but does     condition is not met.
              not save the results.
                                                          • Some architectures have a separate
                                                            compare instruction.

COMP375 Computer Architecture and Organization                                                                         2
Still More Assembler                                                                                          8/29/2008

                       Labels in Assembler                   Jumps Based on Status Flags
                                                               JE     Jump if equal          ZF=1
            • You can attach a name to a memory
                                                               JZ     Jump if zero
                                                                         p                   ZF=1
              l    ti in          bl    This ll
              location i assembler. Thi allows you t   to
                                                               JNE    Jump if not equal      ZF=0
              use the name instead of numerical address
                                                               JNZ    Jump if not zero       ZF=0
            • Labels start in first column and end with a      JLE    Jump if less or equal ZF=1 or SF≠OF
              colon :                                          JL     Jump if less           SF≠OF
                       jmp rabbit                              JNS    J          t i
                                                                      Jump if not sign       SF 0
                       // some other stuff here                JS     Jump if sign           SF=1
            rabbit:    mov eax, dog                            JGE    Jump if greater or equal SF=OF
                                                               JG     Jump if greater        ZF=0 and SF=OF

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

COMP375 Computer Architecture and Organization                                                                       3
Still More Assembler                                                                                       8/29/2008

                          If statements                                        Loops
            • The high level language IF statement is       • There are usually no hardware instructions
              easily implemented by a conditional jump.                   y p             p (i.e. for,
                                                              that directly implement loops (
                                      MOV       eax,cat       while, do)
             if (cat ==dog)           CMP       eax,dog     • Loops are implemented with conditional
                cow = goat;           JNE       noteq         jumps.               again: mov eax,what
             else                     MOV       edx,goat                                  cmp eax,ever
                cow = bull;           JMP       after      while (what == ever) {
                                                                 (            )
                                                                                          jne       dl
                               noteq: MOV       edx,bull
                               after: MOV       cow,edx         // something             // something
                                                                                          jmp again

                                 Try It                                Possible Solution
            • Complete this program in assembler              cow=0; dog=0; cat=3;
            int cow=0, dog=0, cat=3;                            mov eax, dog   ; put dog in eax
            _asm{                                           again:
                 do {       // convert this to assembler        inc cow        ; cow++
                      cow ;                                     add eax, cat   ; add cat to dogg
                      dog = dog + cat;                          cmp eax, 12    ; < 12 ?
                 } while (dog < 12);                            jl again       ; repeat if not
            }                                                   mov dog, eax   ; save result to dog

COMP375 Computer Architecture and Organization                                                                    4
Still More Assembler                                                                                          8/29/2008

                           Addresses                              Intel Assembler Addresses
            • Assembler programs often have to                • You can load a register with the address of
                   i l t dd
              manipulate addresses.                             a memory l     ti by i the Load
                                                                           location b using th L d
            • A pointer in C++ represents an address in         Effective Address, lea, instruction.
              assembler.                                           lea eax, dog ; eax = addr of dog
            • You may need to use addresses to follow         • If the memory location is based on
              links in a data structure or to get an            indexing, the lea instruction will compute
              element from an array.                            the correct address
                                                               lea     eax, dog[esi]      ; effective addr

                             Indexing                                   Assembler Pointers
            • To specify that the address of the data is             int  *p;        // p is a pointer
              in a register in Intel assembler, you put the          i t
                                                                     int      5,
                                                                          x = 5 y;           l i t
                                                                                     // regular integers
              register in the operand field in [brackets].           p = &x;         // p points to x
            //       char cat[47], goat;                          y = *p;      // y = x = 5
            //       goat = cat[10];                          _asm {
                  lea ebx, cat ; ebx = addr of cat                       ebx,
                                                                  mov ebx p             b       i t
                                                                                     ; ebx = pointer p
                  add ebx, 10           ; add 10 to address       mov eax, [ebx]     ; eax = x
                  mov al, [ebx] ; al = cat[10]                    mov y, eax         ; put value in y
                  mov goat, al ; save in goat                 }

COMP375 Computer Architecture and Organization                                                                       5
Still More Assembler                                                                                                            8/29/2008

                          Indexing Arrays                                 Program to Sum 5 Numbers
                                                                        int main(){
            • An array is a sequential collection of data
                                                                        int arrayA[5] = { , 5, 7, 11, 13};
                                                                                 y [ ]   {3, , ,    ,   };
                 l     t         ti    dd
              values at consecutive addresses.
                                                                        int sum = 0;
            • The first element of an array (index 0) is at               /* Sum in C++ */
              the start address of the array.                             int i;
            • The second element’s address is the start                   for (i = 0; i < 5; i++) {
              address of the array plus the size of each                     sum = sum + arrayA[i];
              element.                                                    }

                /* Sum in Assembler */                                     /* Sum in Assembler */
            _asm{                                                       _asm{
               push    esi        ; save value of esi pointer              push    esi          ; save value of esi pointer
               lea     esi, arrayA ; esi = start addr of arrayA            mov     esi, 0       ; esi is index into array
               mov     eax, 0     ; eax = 0, initialize sum                mov     eax, 0       ; eax = 0, initialize sum
               mov     ebx, 5     ; ebx = 5, loop counter                  mov     ebx, 5       ; ebx = 5, loop counter
            forloop:                                                    forloop:
               add     eax, [esi]    ; add next value of array to eax      add     eax, arrayA[esi]; add next value of array
               add     esi, 4        ; increment esi to next element       add     esi, 4     ; increment esi to next element
               sub     ebx, 1        ; decrement loop counter              sub     ebx, 1     ; decrement loop counter
               jnz     forloop       ; repeat if not zero                  jnz     forloop    ; repeat if not zero
               mov     sum, eax      ; move result to sum                  mov     sum, eax   ; move result to sum
               pop     esi           ; restore esi pointer                 pop     esi        ; restore esi pointer
            }                                                           }

COMP375 Computer Architecture and Organization                                                                                         6
Still More Assembler                                                                                      8/29/2008

                 Two Dimensional Arrays                         2D Array in Assembler
            • Consider the two dimensional array           int aray[2][3], i, j, rat;
                int aray[2][3];                          _asm {            //     rat = aray[i][j];
            • This is allocated in memory as:                  mov ebx, i               ; ebx = i
                  0,0   0,1   0,2   1,0   1,1   1,2            imul ebx, 3              ; ebx = i*3
            • To set x = aray[i][j];                           add ebx, j               ; ebx = i*3 + j
            • temp = size of int * (i*3 + j)                   mov eax, aray[ebx] ; eax = aray[i][j]
            • get the value at the address computed as         mov rat, eax             ; store in rat
              the address of the start of aray + temp    }

COMP375 Computer Architecture and Organization                                                                   7

To top