Reputation: 45
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
Upvotes: 2
Views: 5271
Reputation: 2642
"...but I do not know how to set the bit in position..."
Understandable. There are a zillion instructions.
The first one, SHR
(and SHL
, ignored for this discussion) which you have chosen to use, will fiddle the carry flag to contain the bit on the edge (of the source register) that you shifted out.
There is another instruction, RCL
(and RCR
, similarly ignored for this discussion) which will put the carry bit into the bit on the other edge (of the target register) which you will "shift in" so to speak
Do this eight times in a row and you'll have your "reverse-o-matic" procedure done.
Here are two pages that describe these two instructions, including a little picture...
Penguin Explains SHR instruction
Penguin Explains RCL instruction
Hey, this is easy, so, free code, just because I feel nice today...
Mov AL,Some_Number ;Define this somewhere
Sub AH,AH ;Not really needed, placed here to help newcomers understand
SHR AL,1 ;Get the bottom bit (bit #0) from AL
RCL AH,1 ;Put it into the top bit of AH
SHR AL,1 ;Now get bit #1
RCL AH,1 ;Put it into the top bit of AH
SHR AL,1 ;Now get bit #2
RCL AH,1 ;Put it into the top bit of AH
SHR AL,1 ;Now get bit #3
RCL AH,1 ;Put it into the top bit of AH
SHR AL,1 ;Now get bit #4
RCL AH,1 ;Put it into the top bit of AH
SHR AL,1 ;Now get bit #5
RCL AH,1 ;Put it into the top bit of AH
SHR AL,1 ;Now get bit #6
RCL AH,1 ;Put it into the top bit of AH
SHR AL,1 ;Now get bit #7
RCL AH,1 ;Put it into the top bit of AH
Mov Reverse_Pattern,AH ;The pattern is now exactly backwards
Tested it and let us know if this works
Upvotes: 1
Reputation: 58772
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
Upvotes: 1