-5

Is there a way to unpack the contents of a single register?

Say, for example, that you have a 16-bit register A. I need to figure out a way to say:

  • Bits 0 - 5 need to go to Register B.
  • Bits 6 - 10 need to go to Register C.

If there is a simpler way of using a Bit array just mapped to a memory location and just pointing to the desired bits or are there some algorithms out in assembly code that will work?

I am working with Atmel microcontrollers.

I want to start developing things that share a single data bus to simplify the design. If I figure out how to partition the data registers, this can allow a device to send a message with the required data to go along.

Say, for example, that I connect and specify that a device sends its ID on the last 8 bits and relevant data for the first 8 bits required for the microcontroller to make a decision based on that data.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ben Madison
  • 105
  • 1
  • 7
  • 1
    Would you know how to copy *all* the bits from register A to register B? After you did that, would you know how to empty all the bits except 0-5? – David Wohlferd May 19 '18 at 03:39
  • Do you have a specific architecture in mind? 8080 has 8-bit registers called A, B, and C (and D, E, H, and L: [Why are first four x86 GPRs named in such unintuitive order?](https://retrocomputing.stackexchange.com/q/5121)). Pretty much all architectures have AND instructions, but some toy architectures (like LC-3) don't have right shift. Anyway, look at C compiler output for bit-field extraction, e.g. for `struct foo{ unsigned a:6, b:5; };` on whatever architecture you want. You'll see ARM `ubfx`, or x86 shift/and (or `bextr`), or PowerPC `rlwinm` – Peter Cordes May 19 '18 at 04:17

1 Answers1

3

This may be what you are looking for:

Register_B = Register_A & 0x003F; // Take the 6 LSB  (bits 0-5)
Register_C = Register_A & 0x07C0; // Take the bits 6-10
Register_C = (Register_A & 0x07C0) >> 6; // Or maybe you want to place these bits elsewhere using bit shift?

Here is more clarification about using bits in C. Mostly for microcontrollers, but the exact same can be used in Windows, except you don't use registers directly in Windows.

Decimal 5 = binary 0000 0101 = hexadecimal 0x05 Decimal 16 = hex 0x10

Now, this an example in binary:

x = (5 << 1);
5       = 0000 0101
5 << 1  = 0000 1010  (Same sa 5, but bits are shifted left by 1)
5 << 2  = 0001 0100  (5 shifted by 2 bits to the left.)

Finally, you can also compare bits, of 'MASK' bits: This code shows how to compare bits.

x = 5;
if( x & 0x01)
{  .. code here is executed because at least 1 bit from "0x01" is present in "decimal 5"
}
  • (variable & var2) means that at least one bit is "1" in both variable
  • (Var1 | Var2) means that any of the two variables are true.
  • (Var1 ^ Var2) means that at least one bit is "1" in one variable and "0" in the other. Basically if Var1 is different from Var2, this will be true.

You can also MASK bits:

Var1 = 254;  // All 8 bits are "1" except the least significant bit which is 0.
Var2 = Var1 & 0x0F;  // 0x0F = only last 4 bits "1".

In that case, Var2 will end up with 0000 1110 because 1111 1110 & 0000 1111 gives that result. All the bits that are "1" in both values will become "1" for Var2, but all others will be 0. This is a bitwise AND operation, where each bit of the result depends only on the corresponding bits of the inputs.

I know you don't need this a lot in Windows, but programming microchip means playing a lot with bits directly.

About registers again... just think about them as normal variables, but the microcontroller reads and writes to them all the time, so writing a value there can have an effect, like blinking an LED or sending a byte on the USART/COM port. Reading the datasheet will even show you that some registers are read-only or write-only, and will normally explain in details what the register do. If you program an MCU like a PIC or AVR, you will need to understand datasheets. They are your most valuable tool.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Ok thanks! the question had general example, I think you got it what I was looking for. I just need the steps to do this in a general way so I can apply what you did to any circumstance. Could you expand your answer just a bit, your on the right track I just need bit more guidance. The stuff that put in my question was for example, thank you. – Ben Madison May 25 '18 at 03:21
  • OK, here's a small lesson about bits... check out my other answer so I can format more easily the answer. – Jonathan Corriveau May 27 '18 at 02:01