Search code examples
assemblyx86att

mov instruction doesn't work other way around


I'm trying to move the characters that are not digit from the char array "buffer" to new array "clean". "buffer" is created by using scanf function.

    .section bss
buffer:
    .skip 20
clean:
    .skip 20

...

if_digit:
    movl    $0, %ebx
cleanloop:  
    movl    $0, %ecx
    movb    buffer(%ebx), %cl
    pushl   %ecx
    call    isdigit     #nonzero if digit.
    addl    $4, %esp
    incl    %ebx
    cmpl    $0, %eax
    jne clean_buffer    #jmp to clean_buffer if digit
    jmp end_cleanloop


clean_buffer:   
    movb    %cl, clean(%ebx)
    jmp     cleanloop

end_cleanloop:
    movb    $0, clean(%ebx) #add null character at the end.     
    pushl   $clean
    call    atoi        #stores atoi value at eax
    addl    $4, %esp
    subl    $4, iIndex  
    pushl   %eax
    jmp     input

What's questionable is about these two lines.

movb buffer(%ebx), %cl

vs

movb %cl, clean(%ebx)

The first line stores the certain character in buffer to cl. However, second line doesn't take any action. Even when I checked with gdb, no value was stored in clean.

Why does mov instruction works in first line, but not in second line?


Solution

  • I made a mistake with incl %ebx. I increased the index stored in ebx before copying the value. This line should go after the movb %cl, clean(%ebx).

    It DOES work the other way around.