Thursday, February 2, 2012

Conditional Branching & Loop

Conditional Branching
 * JXX - JA, JAE, JE, JG, JZ, JNZ etc
 * These Jump depends on the state of eflags
    - Zero Flag (ZF)
    - Parity Flag (PF)
    - OverFlowFlag (OF)
    - Sign Flag (SF)
    - Carry Flag (CF)
 * Only Short & Near jumps are supported, Far Jumps not allowed

Program Start here
.data
    HelloWorld:
        .asciz "Hello Earth!\n"
    ZeroFlagSet:
        .asciz "Zero Flag was Set!\n"
    ZeroFlagNotSet:
        .asciz "Zero Flag Not Set!\n"
.text
    .globl _start
    _start:
        nop
        movl $10, %eax
        xorl %eax, %eax  #To set Zero Flag
        jz FlagSetPrint
    FlagNotSetPrint:
        # Write CallDemo
        movl $4, %eax
        movl $1, %ebx
        leal ZeroFlagNotSet, %ecx
        movl $20, %edx
        int $0x80
        jmp ExitProgram
    FlagSetPrint:
        # Write CallDemo
        movl $4, %eax
        movl $1, %ebx
        leal ZeroFlagSet, %ecx
        movl $20, %edx
        int $0x80
        jmp ExitProgram
    ExitProgram:
        # Exit the program
        movl $1, %eax
        movl $10, %ebx
        int $0x80

Jump on Zero Example
================

bala@bala-laptop:~/ASM$ as -ggstabs -o ConditionalBranch.o ConditionalBranch.s
bala@bala-laptop:~/ASM$ ld -o ConditionalBranch ConditionalBranch.o
bala@bala-laptop:~/ASM$ ./ConditionalBranch
Zero Flag was Set!
bala@bala-laptop:~/ASM$ gdb ./ConditionalBranch
(gdb) list 1
8    .text
9        .globl _start
10        _start:
(gdb) break *_start+1
Breakpoint 1 at 0x4000b1: file ConditionalBranch.s, line 12.
(gdb) run
Starting program: /home/bala/ASM/ConditionalBranch
Breakpoint 1, _start () at ConditionalBranch.s:12
12            movl $10, %eax
(gdb) s
13            xorl %eax, %eax  #To set Zero Flag
(gdb) info registers
rbp            0x0    0x0
rsp            0x7fffffffe3b0    0x7fffffffe3b0
rip            0x4000b6    0x4000b6 <_start+6>
eflags         0x202    [ IF ]
cs             0x33    51
ss             0x2b    43
(gdb) s
14            jz FlagSetPrint
(gdb) info registers
rbp            0x0    0x0
rsp            0x7fffffffe3b0    0x7fffffffe3b0
rip            0x4000b8    0x4000b8 <_start+8>
eflags         0x246    [ PF ZF IF ]
cs             0x33    51
ss             0x2b    43
(gdb) s
FlagSetPrint () at ConditionalBranch.s:25
25            movl $4, %eax
(gdb) s
26            movl $1, %ebx
(gdb) s
27            leal ZeroFlagSet, %ecx
(gdb) s
28            movl $20, %edx
(gdb) s
29            int $0x80
(gdb) s
Zero Flag was Set!
30            jmp ExitProgram

LOOP Instruction

 * This is a normal Loop instruction
 * Number of times to Loop given in ECX, i-- automatically
 *

Program Start here

.data
    HelloWorld:
        .asciz "Hello Earth!\n"
.text
    .globl _start
    _start:
        nop

        movl $5, %ecx
    PrintFiveTimes:
        pushq %rcx
        movl $4, %eax
        movl $1, %ebx
        leal HelloWorld, %ecx
        movl $13, %edx
        int $0x80
        popq %rcx
    loop PrintFiveTimes
    jmp ExitProgram

ExitProgram:
        # Exit the program
        movl $1, %eax
        movl $10, %ebx
        int $0x80


bala@bala-laptop:~/ASM$ gdb ./Loop
(gdb) break *_start+1
Breakpoint 1 at 0x4000b1: file Loop.s, line 9.
(gdb) run
Starting program: /home/bala/ASM/Loop
Breakpoint 1, _start () at Loop.s:9
9            movl $5, %ecx
(gdb) s
PrintFiveTimes () at Loop.s:11
11            pushq %rcx
(gdb) print /x $rsp
$1 = 0x7fffffffe3d0
(gdb) x/1xw 0x7fffffffe3d0
0x7fffffffe3d0:    0x00000001
(gdb) s
PrintFiveTimes () at Loop.s:12
12            movl $4, %eax
(gdb) print /x $rsp
$2 = 0x7fffffffe3c8
(gdb) x/1xw 0x7fffffffe3c8
0x7fffffffe3c8:    0x00000005
(gdb) s
13            movl $1, %ebx
(gdb) s
14            leal HelloWorld, %ecx
(gdb) s
15            movl $13, %edx
(gdb) s
16            int $0x80
(gdb) s
Hello Earth!
17            popq %rcx
(gdb) s
PrintFiveTimes () at Loop.s:18
18        loop PrintFiveTimes
(gdb) print /x $rcx
$4 = 0x5
(gdb) s
11            pushq %rcx
(gdb) s
PrintFiveTimes () at Loop.s:12
12            movl $4, %eax
(gdb) s
13            movl $1, %ebx
(gdb) s
14            leal HelloWorld, %ecx
(gdb) s
15            movl $13, %edx
(gdb) s
16            int $0x80
(gdb) s
Hello Earth!
17            popq %rcx
(gdb) print /x $rsp
$5 = 0x7fffffffe3c8
(gdb) x /1xw 0x7fffffffe3c8
0x7fffffffe3c8:    0x00000004
(gdb)

No comments:

Post a Comment