Bryand and O'Hallaron Chapter 3 Section 3.6.3 Jump Instructions 1. Figure 3.11 The Jump Instruction Instruction Synonym Jump condition Description jmp Label 1 Direct jump jmp *Operand 1 Indirect jump je Label jz ZF Equal / zero jne Label jnz ~ZF Noe equal / not zero jg Label jnle ~(SF^OF)&~ZF Greater (signed >) jge Label jnl ~(SF^OF) Greater or equal (signed >) jl Label jnge (SF^OF) Less (signed <) jle Label jng SF^OF)|ZF Less or equal (signed <=) ja Label jnbe ~CF&~ZF Above (unsigned >) jae Label jnb ~CF Above or equal (unsigned >) jb Label jnae CF Below (unsigned <) jbe Label jna CF&~ZF Below or equal (unsigned <=) 2. Examples jmp .L1 # direct jump (address part of instruction) .p2align 4,,15 .L1 # start at an aligned address leal .L1,%eax jmp *%eax # indirect jump (address in %eax) .p2align 4,,15 .L1 # start at an aligned address leal .L1,-8(%ebp) jmp *-8(%ebp) # indirect jump (address on stack) .p2align 4,,15 .L1 # start a loop at an aligned address leal .L1,jmpadr jmp *jmpadr # indirect jump (address in jmpadr) .p2align 4,,15 .L1 # start a loop at an aligned address ... .bss # data segment for data with initial value 0 .align 4 # make sure the long is aligned .size accum,4 # its a long jmpadr: .zero 4 # initial value zero 3. PC (Program Counter) Relative Addressing (The Program Counter is a synonym for the Instruction Pointer (IP). ----------------------------------------------------- storm:~/bando/chap3/sec6>more silly.c extern int j; main() { int i = j; if (i <= 0) goto end; while (i > 0) i = i - (i >> 1); end: return; } storm:~/bando/chap3/sec6>gcc -S -O2 silly.c ----------------------------------------------------- storm:~/bando/chap3/sec6>more silly.s .file "silly.c" .text .p2align 4,,15 .globl main .type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx #--------------------------- movl j, %edx testl %edx, %edx jle .L5 .p2align 4,,7 .L6: movl %edx, %eax sarl %eax subl %eax, %edx testl %edx, %edx jg .L6 .L5: popl %ecx #--------------------------- popl %ebp leal -4(%ecx), %esp ret .size main, .-main .ident "GCC: (GNU) 4.1.2 20070502 (Red Hat 4.1.2-12)" .section .note.GNU-stack,"",@progbits storm:~/bando/chap3/sec6> ----------------------------------------------------- storm:~/bando/chap3/sec6>objdump -d silly.o silly.o: file format elf32-i386 Disassembly of section .text: 00000000
: 0: 8d 4c 24 04 lea 0x4(%esp),%ecx 4: 83 e4 f0 and $0xfffffff0,%esp 7: ff 71 fc pushl 0xfffffffc(%ecx) a: 55 push %ebp b: 89 e5 mov %esp,%ebp d: 51 push %ecx #--------------------------- e: 8b 15 00 00 00 00 mov 0x0,%edx 14: 85 d2 test %edx,%edx 16: 7e 0a jle 22 18: 89 d0 mov %edx,%eax 1a: d1 f8 sar %eax 1c: 29 c2 sub %eax,%edx 1e: 85 d2 test %edx,%edx 20: 7f f6 jg 18 22: 59 pop %ecx #--------------------------- 23: 5d pop %ebp 24: 8d 61 fc lea 0xfffffffc(%ecx),%esp 27: c3 ret ----------------------------------------------------- storm:~/bando/chap3/sec6>more definej.c int j = 0; storm:~/bando/chap3/sec6>gcc -o silly silly.s definej.c storm:~/bando/chap3/sec6>objdump -d silly ... 08048380
: 8048380: 8d 4c 24 04 lea 0x4(%esp),%ecx 8048384: 83 e4 f0 and $0xfffffff0,%esp 8048387: ff 71 fc pushl 0xfffffffc(%ecx) 804838a: 55 push %ebp 804838b: 89 e5 mov %esp,%ebp 804838d: 51 push %ecx #--------------------------- 804838e: 8b 15 f4 95 04 08 mov 0x80495f4,%edx 8048394: 85 d2 test %edx,%edx 8048396: 7e 0a jle 80483a2 8048398: 89 d0 mov %edx,%eax 804839a: d1 f8 sar %eax 804839c: 29 c2 sub %eax,%edx 804839e: 85 d2 test %edx,%edx 80483a0: 7f f6 jg 8048398 80483a2: 59 pop %ecx #--------------------------- 80483a3: 5d pop %ebp 80483a4: 8d 61 fc lea 0xfffffffc(%ecx),%esp 80483a7: c3 ret 80483a8: 90 nop 80483a9: 90 nop 80483aa: 90 nop ... 4. Practice Problem 3.9 on page 157