Page 1 of 2

Chaning bits individually? or merging registers x86

Posted: Thu Jul 25, 2013 2:34 pm
by computertrick
Hi I am trying to convert decimal into character I have noticed a pattern in the binary

0x01 = 00000001b
1 = 00110001b

My question is how can I change the last four bits of 0x01 into 0011 so it can be seen as a character?

If you could also give some examples that would be great thanks. I have looked into this already and can't find an answer

Many thanks :)

Re: Chaning bits individually? or merging registers x86

Posted: Thu Jul 25, 2013 2:55 pm
by Casm
You convert an ascii encoded digit into its corresponding integer by the simple means of subtracting 48 from it (and vice versa).

Re: Chaning bits individually? or merging registers x86

Posted: Thu Jul 25, 2013 3:00 pm
by computertrick
Ok thanks, Do you know how I would change individual bits just for future reference?

Re: Chaning bits individually? or merging registers x86

Posted: Thu Jul 25, 2013 3:22 pm
by bluemoon
Do this has anything to do with OS development?

Re: Chaning bits individually? or merging registers x86

Posted: Thu Jul 25, 2013 3:36 pm
by computertrick
Of course it does read the title. My question was about changing bits individually or merging registers which is a processor question and it is important for OS development

Re: Chaning bits individually? or merging registers x86

Posted: Thu Jul 25, 2013 3:49 pm
by Casm
computertrick wrote:Of course it does read the title. My question was about changing bits individually or merging registers which is a processor question and it is important for OS development
That is the sort of thing any assembly language or C programmer should know, long before he/she even thinks of embarking upon OS development.

Look up the assembly language OR, AND, XOR and NOT instructions, or the C ~, |, &, ^ operators.

Re: Chaning bits individually? or merging registers x86

Posted: Thu Jul 25, 2013 4:38 pm
by computertrick
I am aware of all of those instructions my question was is their an instruction in 8086 to change a single bit that's what I was trying to get across

Re: Chaning bits individually? or merging registers x86

Posted: Thu Jul 25, 2013 4:40 pm
by dozniak
computertrick wrote:I am aware of all of those instructions my question was is their an instruction in 8086 to change a single bit that's what I was trying to get across
No, that is absolutely not possible.

Re: Chaning bits individually? or merging registers x86

Posted: Thu Jul 25, 2013 4:42 pm
by Casm
computertrick wrote:I am aware of all of those instructions my question was is their an instruction in 8086 to change a single bit that's what I was trying to get across
Those instructions do precisely that, and more besides. For example, xor ax, 4 would flip bit 2 of the ax register.

For that matter, you can do it even without those instructions. add ax, 0x80 would set bit 7, and sub ax, 0x80 would clear it. That is elementary, and, if you haven't got that far, it will be a long time before you are ready for osdev.

Re: Chaning bits individually? or merging registers x86

Posted: Thu Jul 25, 2013 7:07 pm
by bluemoon
computertrick wrote:Of course it does read the title. My question was about changing bits individually or merging registers which is a processor question and it is important for OS development
Summon the triple /faceplam...

While OS development require excellent skills of all aspect, including flipping a bit, I suggest you read the Required_Knowledge.

Re: Chaning bits individually? or merging registers x86

Posted: Fri Jul 26, 2013 12:15 am
by iansjack
BTS, BTR (80386 and later).

Re: Chaning bits individually? or merging registers x86

Posted: Fri Jul 26, 2013 12:33 am
by iansjack
For that matter, you can do it even without those instructions. add ax, 0x80 would set bit 7, and sub ax, 0x80 would clear it. That is elementary, and, if you haven't got that far, it will be a long time before you are ready for osdev.
That is incorrect. What if the bit is already set (or clear)? Adding (or subtracting) would not have the desired result.

Re: Chaning bits individually? or merging registers x86

Posted: Fri Jul 26, 2013 2:05 am
by Casm
iansjack wrote:
That is incorrect. What if the bit is already set (or clear)? Adding (or subtracting) would not have the desired result.
What has that got to do with anything? or ax, 0x80 wouldn't change a bit which was already set, and "and ax, 0xff7f" wouldn't clear a bit which was already clear. That is in the nature of bitwise operations.

Re: Chaning bits individually? or merging registers x86

Posted: Fri Jul 26, 2013 2:36 am
by iansjack
Casm wrote:
iansjack wrote:
That is incorrect. What if the bit is already set (or clear)? Adding (or subtracting) would not have the desired result.
What has that got to do with anything? or ax, 0x80 wouldn't change a bit which was already set, and "and ax, 0xff7f" wouldn't clear a bit which was already clear. That is in the nature of bitwise operations.
Read the bit of your post that I quoted again. add is not and, sub is not or. add would clear a set bit (and affect other bits), sub would set a clear bit (and affect other bits). That is the nature of arithmetic operations.

Re: Chaning bits individually? or merging registers x86

Posted: Fri Jul 26, 2013 3:46 am
by Casm
iansjack wrote: Read the bit of your post that I quoted again. add is not and, sub is not or. add would clear a set bit (and affect other bits), sub would set a clear bit (and affect other bits). That is the nature of arithmetic operations.
If I wanted to convert a digit into its corresponding integer value, I could either do and ax, 0xcf or sub ax, 48, and which I did would depend upon the mood I was in at the time. Similarly, and ax, 0xdf and sub ax, 32 will both convert a lower case letter into an upper case letter (or clear the archive bit in an MS-DOS fille attribute byte).

I don't remember saying that sub was the same as or (or and).