flat assembler
Message board for the users of flat assembler.

Index > Main > Reversing two bits of byte

Author
Thread Post new topic Reply to topic
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 29 May 2015, 16:21
Hi. I need some help with this.
I must reverse two bits (eg , 3 to 5) in 8086 assembler and I don't know how to do that.
Thank you.
Post 29 May 2015, 16:21
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1619
Location: Toronto, Canada
AsmGuru62 29 May 2015, 16:32
XOR the needed bit with 1 and they will be reversed.
Or maybe you need to reverse them as a string?
Post 29 May 2015, 16:32
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 29 May 2015, 16:55
I need to make for example: 10100100 ->10110010
Post 29 May 2015, 16:55
View user's profile Send private message Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 29 May 2015, 16:57
AsmGuru62 wrote:
XOR the needed bit with 1 and they will be reversed.
Or maybe you need to reverse them as a string?


and by the way, how can I select a specigic bit from one byte?
Post 29 May 2015, 16:57
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1619
Location: Toronto, Canada
AsmGuru62 29 May 2015, 19:30
What do you mean by "reversing"?
Is it the order of bits or their values?
Bit is selected by a mask.
For example bit #0 - a very first bit is the value of 1:
Code:
mov     eax, 1
    

This gives the mask of:
Code:
EAX with bit #0 selected:
+-----------+-----------+-----------+-----------+
| 0000 0000 | 0000 0000 | 0000 0000 | 0000 0001 |
+-----------+-----------+-----------+-----------+
    

To select bits #3 and #5:
Code:
EAX with bits #3,#5 selected:
+-----------+-----------+-----------+-----------+
| 0000 0000 | 0000 0000 | 0000 0000 | 0010 1000 |
+-----------+-----------+-----------+-----------+
    

or using MOV:
Code:
mov     eax, 28h
    

Or if you have a value in memory or register, to reverse the bits use XOR opcode:
Code:
Value dd 71ADC32h
...
xor     [Value], 28h
    

But if you need to reverse ORDER of bit sequence, then it is a little more complicated.
Basically you select bit sequence by a constant value (like 28h for bits #3,#5).

To extract a bit from byte - you need to use a TEST opcode with SETNZ opcode, like this:
Code:
mov     ecx, [some value]
test    ecx, 28h
setnz   al      ; AL will be 1 if EITHER #3 or #5 is 1 and 0 if BOTH #3 and #5 are 0
    
Post 29 May 2015, 19:30
View user's profile Send private message Send e-mail Reply with quote
Bargest



Joined: 09 Feb 2012
Posts: 79
Location: Russia
Bargest 29 May 2015, 20:13
I don't know, is it a GOOD idea, but anyway it solves the problem of exchanging bits with no jumps.Smile (if you mean this by "reversing")
Code:
bt ax, 3 ; get 3 bit in Carry flag
setc bl  ; save in bl
bt ax, 5 ; get 5 bit
setc cl ; save in cl
xor  cl, bl ; check if bits are different
mov bl, cl ; save difference  \
shl bl, 3   ;                  |these 4 comands can be replaced with IMUL cl, 28h, but it's too easy:)
shl cl, 5   ;                  |
or cl, bl    ; get mask in CL /
xor al, cl  ; invert both bits
    

This code is based on the following idea: if bits are similar (both are 0 or both are 1), they will remain the same after exchanging, but if they are different, they will get inverted. So we check, if they are same or not, by extracting and XORing them. We receive 1 if bits are different and 0 if not. After it we build mask, moving this bit to 3 and 5 positions, and XOR original value with it. If we get mask 101000, bits will be inverted, otherwise they will be the same.

If it is not what you need, it will be at least an example of working with bits. But I really don't understand, how did you get 10110010 from 10100100.

EDIT / P.S.:
Here is one more totally useless (but interesting) method to determine, if bits 3 and 5 are different, without extractiong these bits one-by-one:
Code:
        mov ebx, eax   ; copy value
        and ebx, 00101000b ; mask bits
        or  ebx, 01000000b  ; set 6-th bit (for further operations)
        mov ecx, ebx
        dec ecx
        and ecx, ebx  ; these 3 commands clear least-significant "1" bit in ebx and save result in ecx
        add ecx, ebx  ; after it 6-th bit in ecx will be 0 if bits 3 and 5 were different and 1 if they were similar
        ; the next code is not needed, we can read 6-th bit by BT or TEST, but...
        shr ecx, 6
        and ecx, 1
        xor cl, 1 ; invert result, so cl[0] will be equal to eax[5] ^ eax[3]
    


EDIT2: if you need reverse-order of bits as a string, you can do the following:
Code:
mov bl, al
shr bl, 3    ; move the part we need to reverse to 0 bit
xor dl, dl 
; the next code will be a cycle in general case, but now we know, that we reverse order of exactly 3 bits
   shr bl,1  ; move bit to carry
   rcl dl,1   ; move carry to dl
   shr bl,1
   rcl dl,1
   shr bl,1
   rcl dl,1
shl dl, 3  ; move reversed part back to 3 bit
and al, 11000111b ; clear reversed part
or  al, dl
    
Post 29 May 2015, 20:13
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 29 May 2015, 22:38
setnz, bt... Anyone noticed, the instruction set is limited to 8086?

Code:
mask  = 0000'0000'0010'1000b ;bit mask
rmask = 0010'1000'0000'0000b ;mirrored and shifted by 1 bit mask

mov ax,mask
mov dx,rmask
and ax,[value]
mul dx
shr dx,1
sbb dx,dx
and dx,mask
xor [value],dx    

_________________
Faith is a superposition of knowledge and fallacy


Last edited by l_inc on 29 May 2015, 22:40; edited 1 time in total
Post 29 May 2015, 22:38
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.