flat assembler
Message board for the users of flat assembler.
Index
> Main > Reversing two bits of byte 
Author 

MariaSM
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. 

29 May 2015, 16:21 

AsmGuru62
XOR the needed bit with 1 and they will be reversed.
Or maybe you need to reverse them as a string? 

29 May 2015, 16:32 

MariaSM
I need to make for example: 10100100 >10110010


29 May 2015, 16:55 

AsmGuru62
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 

29 May 2015, 19:30 

Bargest
I don't know, is it a GOOD idea, but anyway it solves the problem of exchanging bits with no jumps. (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 onebyone: Code: mov ebx, eax ; copy value and ebx, 00101000b ; mask bits or ebx, 01000000b ; set 6th bit (for further operations) mov ecx, ebx dec ecx and ecx, ebx ; these 3 commands clear leastsignificant "1" bit in ebx and save result in ecx add ecx, ebx ; after it 6th 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 6th 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 reverseorder 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 

29 May 2015, 20:13 

l_inc
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 

29 May 2015, 22:38 

< Last Thread  Next Thread > 
Forum Rules:

Copyright © 19992020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.