Search code examples
assemblyx86fasm

How to set the bit in position?


mov al, 100d ; 01100100
shr eax, 1 ; cf = 0
           ; 00110010

How to burn cf in 5th position?

For example: My number 10000111. CF = 1 => 10001111

My main task is to make reverse byte using shr (shl). I want to do it myself, but I do not know how to set the bit in position


Solution

  • For reversing the bits, just shift them in to the destination operand from the same end you shifted them out from the source (that is use opposite shifts). Sample code:

        mov al, 100  ; input = 01100100
        mov ah, 0    ; output
        mov ecx, 8   ; 8 bits to process
    next:
        shr al, 1    ; shift next bit out to the right
        rcl ah, 1    ; shift it in from the right
        loop next
        ; result in ah = 38 = 0010 1100
    

    Nevertheless, to answer your question: shift carry into a zeroed temporary register to the given position, then use bitwise OR to set it in the destination. Sample code:

    mov dl, 0 ; zero temp register, without affecting CF
    rcl dl, 5 ; move CF into bit #5 (use CL register for dynamic)
    or al, dl ; merge into al