简体   繁体   中英

From C to Assembly jge and jg?

Does jg and jge execute or jump to the following label if the second register in the statement eg., cmpl %esi, %edi is greater than or equal to (or only greater than) the first register %esi ? And is the result of which one is greater stored in the second register and used to determine if jump executes the consecutive label?

sum1.c

 int sum(int first, int last)
 {
      int sum = 0;
      int in_between;
      for (in_between = first; in_between <= last; in_between++)
      {
           sum += in_between;
      }
      return sum;
 }

sum1.s:

    .file   "sum1.c"
    .text
.globl sum
    .type   sum, @function
sum:
.LFB0:
    .cfi_startproc
    movl    %edi, %edx ; puts first into in_between
    movl    $0, %eax ; sets sum to zero
    cmpl    %esi, %edi ;compares first and last, checking if first– last < 0
    jg      .L3 ; jumps to .L3 if first is greater than last, otherwise 
 ;executes .L6
.L6:
    addl    %edx, %eax ;adds in_between to sum
    addl    $1, %edx ; increments in_between
    cmpl    %edx, %esi ; makes the comparison between in_between and last, 
    ;last < in_between
    jge     .L6 ; jumps to .L6 if last is greater than or equal to 
    ;in_between. (the result jump uses is stored in last).    
.L3:
    rep
    ret ;returns the value stored in %eax register.
    .cfi_endproc
.LFE0:
    .size   sum, .-sum
    .ident  "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-17)"
    .section        .note.GNU-stack,"",@progbits

The CPU updates the processor status (PS sometimes PSL) register after [nearly] every instruction. The CMPL instruction does an implicit subtraction and updates the values of the PS rester. You'd get the same effect if you did a SUBL instruction, except that SUBL puts the result in the destination operand while CMPL does not.

The Jxx instructions conditionally branch depending upon the value of the PS register.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM