0

I'm getting this error when I try to run my assembly code that uses a loop command:

"rip.s:190: Error: value of -288 too large for field of 1 bytes at 497".

This error occurs right when the program is supposed to loop. I tried initially putting "3" in the %ecx register before it enters the loop but I still get the same error. I'm not sure why this is happening so I don't really know how to go about fixing it. I've attached the pertinent code below:

    //Other code
    movl    %ecx, -20(%ebp)
    movl    $0, %ecx
    addl    $3, %ecx
    .L24:
    xorl    %edi, %edi
    movl    -32(%ebp), %ebx
    movl    -44(%ebp), %esi
    cmpl    %esi, -52(%ebp)
    movl    %ebx, -48(%ebp)
    jg  .L11
    movl    -52(%ebp), %eax
    movl    %ebx, %edx
    xorl    %edi, %edi
    subl    $2, %edx
    movl    %edx, -40(%ebp)
    movl    %eax, -36(%ebp)
    .p2align 4,,7
    .p2align 3
    .L19:
    movl    -36(%ebp), %eax
    testl   %eax, %eax
    js  .L42
    movl    16(%ebp), %ebx
    cmpl    %ebx, -36(%ebp)
    je  .L11
    .L13:
    movl    -40(%ebp), %ebx
    cmpl    %ebx, -32(%ebp)
    jl  .L14
    movl    -36(%ebp), %esi
    addl    16(%ebp), %esi
    movl    %edi, -60(%ebp)
    movl    %esi, -28(%ebp)
    jmp .L31
    .p2align 4,,7
    .p2align 3
    .L15:
    movl    %edx, -24(%ebp)
    movl    -20(%ebp), %edx
    cmpl    %edx, %ebx
    movl    %edx, -20(%ebx)
    movl    -24(%ebp), %edx
    je  .L38
    .L16:
    movl    -28(%ebp), %edx
    movl    8(%ebp), %edi
    movl    %edx, %eax
    sarl    $31, %edx
    idivl   16(%ebp)
    movl    %edx, %esi
    movl    %eax, -24(%ebp)
    movl    -20(%ebp), %eax
    leal    (%ebx,%eax), %edx
    movl    %eax, -20(%ebp)
    movl    %edx, %eax
    sarl    $31, %edx
    idivl   -20(%ebp)
    movl    (%edi,%esi,4), %eax
    cmpl    $1, (%eax,%edx,4)
    sbbl    $-1, -60(%ebp)
    addl    $1, %ebx
    cmpl    -32(%ebp), %ebx
    jg  .L38
    .L31:
    testl   %ebx, %ebx
    jns .L15
    addl    $1, %ebx
    jmp .L16
    .p2align 4,,7
    .p2align 3
    .L38:
    movl    -60(%ebp), %edi
    .L14:
    addl    $1, -36(%ebp)
    movl    -44(%ebp), %eax
    cmpl    %eax, -36(%ebp)
    jle .L19
    .L11:
    movl    -44(%ebp), %ebx
    movl    8(%ebp), %esi
    movl    -32(%ebp), %edx
    movl    -4(%esi,%ebx,4), %eax
    addl    $1073741823, %edx
    movl    -56(%ebp), %esi
    movl    (%eax,%edx,4), %eax
    movl    -4(%esi,%ebx,4), %ebx
    cmpl    $1, %eax
    adcl    $-1, %edi
    cmpl    $3, %edi
    movl    %eax, -28(%ebp)
    movl    $1, %eax
    je  .L22
    xorb    %al, %al
    cmpl    $2, %edi
    je  .L43
    .L22:
    addl    $1, -32(%ebp)
    movl    %ebx, -24(%ebp)
    movl    -20(%ebp), %ebx
    cmpl    -48(%ebp), %ebx
    movl    %ebx, -20(%ebp)
    movl    -24(%ebp), %ebx
    movl    %eax, (%ebx,%edx,4)
    loop .L24 <--Error occurs right here
    .L9:
    addl    $1, -44(%ebp)
    jmp .L8

What is causing the error?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
AbhishekSaha
  • 705
  • 3
  • 9
  • 24

1 Answers1

3

That's a compile (assembly) time error, not a runtime one. The loop instruction only has a 8 bit signed offset for the jump target, the assembler is trying to tell you that your target is out of range.

You should replace the loop instruction with the equivalent dec ecx; jnz .L24 at the bottom of your loop (which is recommended anyway for optimization purposes). The only difference is that dec overwrites EFLAGS.

x86 loops don't have to use the loop instruction; it's a peephole optimization for code-size when it's convenient to use ECX as a down-counter. (And comes at the expense of speed on Intel CPUs.)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Jester
  • 56,577
  • 4
  • 81
  • 125
  • Nah I'm required to use the loop command unfortunately. Can you expand on what's causing this error and how to fix it while using a loop? – AbhishekSaha Mar 26 '14 at 21:14
  • If you can't shrink the code, and you must use `loop` then you should `loop` to a nearby trampoline label that subsequently jumps to `.L24`. This is of course totally silly. – Jester Mar 26 '14 at 21:33
  • 2
    or put the code in a function then call that function on each loop – phuclv Mar 27 '14 at 01:35