ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Structured Assembly Language
Programming Techniques
Control Transfer Instructions
More on Conditional Jumps

  Instructions that check the eFLAGs
   register before jumping
  The FLAGs checked by Conditional
   jumps
    – Carry
    – Parity
    – Zero
    – Sign
    – Overflow flags
The eFLAGs Register

  A special purpose register
  Certain bits in this register serve as Flags
The eFLAGs Register



          FLAGS        SET (1)      CLEARED (0)
   Overflow       OV [overflow]   NV [no overflow]
   Sign           NG [negative]   PL [positive]
   Zero           ZR [zero]       NZ [not zero]
   Parity         PE [even]       PO [odd]
   Carry          CY [carry]      NC [no carry]
Conditional Jumps
Sequential Statements

  Fetch-Decode-Execute
       CS:EIP is the PC (Program Counter)
  When the fetched instruction is copied into
   the instruction register, EIP is automatically
   incremented by X.
       X = instruction length (in bytes)
  Since EIP is automatically INCREMENTED by X,
   the instructions are executed SEQUENTIALLY
   by default.
Recall


             CPU         MEMORY    address

              CU
                          inst 1   07E2:0000
  INPUT                   inst 2   07E2:0002
           PC = ?
                          inst 3   07E2:0004
  OUTPUT   IR = inst 1
                           ...     07E2:0006



              ALU
How can we change the default execution?

   What will happen to EIP?
    High-level code/algorithm:

  Current instruction â–º if (condition) then do1
      IP points here â–º do1: code 0
                               code 1
                               code 2
                        else
                        do2: code x
                               code y
                               code z
JMP Statement
 JMP means jump (an UNCONDITIONAL jump)
  This instruction takes one operand: a label
  For example:        jmp doon

          LABEL

                     dito_ba: mov eax, 4
                              mov ebx, 1
                              mov ecx, msg1
                              mov edx, len1
                              int 80h

                     doon:     mov eax, 1
                               mov ebx, 0
                               int 80h
JMP Statement

 nasm –f elf sample.asm –l sample.lst
JMP Statement

        machine code/
         opcode of
JMP Statement

        machine code/
         opcode of


                ?
JMP Statement

      jmp   a value in backwords storage format;
            therefore actual value is: 00000016




                                           So we have:
                                           E9 00000016
JMP Statement

 When Instruction Register contains     E9 00000016
 CS:EIP will ‘point’ here
 So EIP =
 00000005 h




 Note: As the instruction in IR is executed:
 EIP will be: EIP+00000016 = 0000001B h
JMP Statement

 When Instruction Register contains     E9 00000016
 CS:EIP will ‘point’ here
 So EIP =
 00000005 h




 Note: As the instruction in IR is executed:
 EIP will be: EIP+00000016 = 0000001B h
Addition of hex numbers

 Decimal:
    19
   + 9
    28

 Hexadecimal:
    19
   + 9
    22
JMP Statement

 Reminder:
  The displacement is in Backwords Storage
   Format.
  A displacement in a jump can either be a
   positive or a negative value.
JMP Statement




                FFFF FFE5
JMP Statement




                           The value is negative, so
                           this jumps to a label ‘above’

                   FFFF FFE5
        sign-bit   F = 1111 (binary)
JMP Statement




                                        IR



                                       EIP


                        The value is negative, so
                        this jumps to a label ‘above’

                  FFFF FFE5
                + 0000 001B

More Related Content

Chapter2c

  • 1. Structured Assembly Language Programming Techniques Control Transfer Instructions
  • 2. More on Conditional Jumps  Instructions that check the eFLAGs register before jumping  The FLAGs checked by Conditional jumps – Carry – Parity – Zero – Sign – Overflow flags
  • 3. The eFLAGs Register  A special purpose register  Certain bits in this register serve as Flags
  • 4. The eFLAGs Register FLAGS SET (1) CLEARED (0) Overflow OV [overflow] NV [no overflow] Sign NG [negative] PL [positive] Zero ZR [zero] NZ [not zero] Parity PE [even] PO [odd] Carry CY [carry] NC [no carry]
  • 6. Sequential Statements  Fetch-Decode-Execute CS:EIP is the PC (Program Counter)  When the fetched instruction is copied into the instruction register, EIP is automatically incremented by X. X = instruction length (in bytes)  Since EIP is automatically INCREMENTED by X, the instructions are executed SEQUENTIALLY by default.
  • 7. Recall CPU MEMORY address CU inst 1 07E2:0000 INPUT inst 2 07E2:0002 PC = ? inst 3 07E2:0004 OUTPUT IR = inst 1 ... 07E2:0006 ALU
  • 8. How can we change the default execution?  What will happen to EIP? High-level code/algorithm: Current instruction â–º if (condition) then do1 IP points here â–º do1: code 0 code 1 code 2 else do2: code x code y code z
  • 9. JMP Statement JMP means jump (an UNCONDITIONAL jump)  This instruction takes one operand: a label  For example: jmp doon LABEL dito_ba: mov eax, 4 mov ebx, 1 mov ecx, msg1 mov edx, len1 int 80h doon: mov eax, 1 mov ebx, 0 int 80h
  • 10. JMP Statement nasm –f elf sample.asm –l sample.lst
  • 11. JMP Statement machine code/ opcode of
  • 12. JMP Statement machine code/ opcode of ?
  • 13. JMP Statement jmp a value in backwords storage format; therefore actual value is: 00000016 So we have: E9 00000016
  • 14. JMP Statement When Instruction Register contains E9 00000016 CS:EIP will ‘point’ here So EIP = 00000005 h Note: As the instruction in IR is executed: EIP will be: EIP+00000016 = 0000001B h
  • 15. JMP Statement When Instruction Register contains E9 00000016 CS:EIP will ‘point’ here So EIP = 00000005 h Note: As the instruction in IR is executed: EIP will be: EIP+00000016 = 0000001B h
  • 16. Addition of hex numbers Decimal: 19 + 9 28 Hexadecimal: 19 + 9 22
  • 17. JMP Statement Reminder:  The displacement is in Backwords Storage Format.  A displacement in a jump can either be a positive or a negative value.
  • 18. JMP Statement FFFF FFE5
  • 19. JMP Statement The value is negative, so this jumps to a label ‘above’ FFFF FFE5 sign-bit F = 1111 (binary)
  • 20. JMP Statement IR EIP The value is negative, so this jumps to a label ‘above’ FFFF FFE5 + 0000 001B