Search code examples
cassemblygreatest-common-divisor

Assignment of 0 to %ecx register


In the label .L0, when I check the value of %eax register, I get the correct value. But when I check the value of the ecx register, it gives me zero. I don't know why. Perhaps this is the reason I get a floating point segmentation fault. Can someone help me figure out the reason why.

The logic I am trying to generate is

while (sml > 0){
rem = lrg % sml;
lrg = sml;
sml = rem;
}

The assembly file which gives the floating point error is:

    .file "gcd.c"
    .section .rodata
.LC0:
    .string "%d"
.LC1:
    .string "%d\n"

    .text
    .globl main
    .type main, @function
main:
    pushl %ebp
    movl  %esp, %ebp
    andl  $-16, %esp
    subl  $32, %esp

    leal  -8(%ebp), %eax    #scan a value
    movl  %eax,  4(%esp)
    movl  $.LC0,  (%esp)
    call scanf

    leal  -12(%ebp), %eax   #scan a value
    movl  %eax,  4(%esp)
    movl  $.LC0,  (%esp)
    call scanf


.L2:
    movl $0, %eax
    cmpl -8(%ebp),%eax
    jle .L0
    jmp .L1

.L0:
    movl  -12(%ebp),%eax
    movl -8(%ebp),%ecx
    movl %eax,%edx
    sarl $31, %edx
    idivl %ecx
    movl %edx,%eax
    movl %eax, -16(%ebp)
    movl -8(%ebp),%edx
    movl %edx, -12(%ebp)
    movl -16(%ebp),%edx
    movl %edx, -8(%ebp)
    jmp .L2

.L1:
    movl -12(%ebp), %eax
    movl  %eax,  4(%esp)
    movl $.LC0, (%esp)
    call printf

    movl $0, %edx

    movl $0, %eax       #end of program
    leave
    ret

.LFE2:
    .size     main, .-main 
    .ident     "GCC: (GNU) 4.2.3 (4.2.3-6mnb1)" 
    .section    .note.GNU-stack,"",@progbits

Solution

  • I agree with Brett. Replace jle with a jl.