Hello I can’t figure out why in the add instruction I need to and by 7 this is the cpp code for the Add instruction
uint16_t dr = (instr >> 9) & 0b111;
uint16_t sr1 = (instr >> 6) & 0b111;
uint16_t sr2 = instr & 0b111;
uint16_t second = registers[sr2];
uint16_t immediateFlag = (instr >> 5) & 0b1;
if (immediateFlag) {
uint16_t imm5 = instr & 0b11111;
second = signExtend(imm5, 5);
}
registers[dr] = registers[sr1] + second;
all the lines anded with 7 are the parts I don’t get. This is how the instruction looks like:
- bits 15-12 opcode(0001)
- bits 11-9 destination register
- bits 8-6 source1
- bits 5 0 or 1 (immediate mode)
- bits 4-3 nothing
- bits 2-0 source2
How does this 0b111 (7 in decimal) come into play and why?