I am new to assembly, and I was wondering if there was a way for me to directly manipulate the bits that form a certain int value in order to simulate bit stuffing.
For example, if I have a 32bit int that represents the message I want to stuff, is there a way for me to insert a bit in the middle of that number in assembly?
Also, since the size of that int would become greater than 32, and I don't want that since the registers are 32bits, the last bits of the number will just get deleted, so no worries about that.
Among two popular instruction set architectures I've worked with (x86 and MIPS), there are no machine instructions to insert bits into the middle of an integer like you described.
However, the functionality can be implemented in terms of bitwise shifts, AND, OR, NOT, which are available on all processors and all programming languages (such as C).
Example: Inserting 5 bits at position 12:
// Inputs given
uint32 original = (...); // [xxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyy]
uint32 insert = (...); // [000000000000000000000000000zzzzz]
// Computation
uint32 mask = (1 << 12) - 1; // [00000000000000000000111111111111]
uint32 a = original & mask; // [00000000000000000000yyyyyyyyyyyy]
uint32 b = original & ~mask; // [xxxxxxxxxxxxxxxxxxxx000000000000]
uint32 c = b << 5; // [xxxxxxxxxxxxxxx00000000000000000]
uint32 d = b | c; // [xxxxxxxxxxxxxxx00000yyyyyyyyyyyy]
uint32 e = insert << 12; // [000000000000000zzzzz000000000000]
uint32 result = d | e; // [xxxxxxxxxxxxxxxzzzzzyyyyyyyyyyyy]
// All together compactly
uint32 result = (original & mask) | ((original & ~mask) << 5) | (insert << 12);