...
{
int *r1, *r2;
r1 = GetCorrectRegister(first);
r2 = GetCorrectRegister(second);
switch ((OpCode)current.opcode)
{
case OpCode.ADDR:
r2 = r1 + r2;
break;
}
}
...
This is the obvious, easiest way to solve this problem. However, I'd prefer not to use an unsafe method if I can avoid it. Basically, I have a bunch of integers which represent registers. There are assembly instructions that take register mnemonics as arguments. I'd prefer not to have the logic to determine which variable to assign to in each branch of the switch statement or make some sort of kludgey GetRegisterValue(mnemonic) and SetRegisterValue(mnemonic) functions. Is there some C#y way to do something similar?
The Maryland Safety Assessment for Every Child (SAFE-C) form (Attachment A) is a tool designed to alert staff to situations that pose an imminent danger to children.
C and C++ are unsafe in a strong sense: executing an erroneous operation causes the entire program to be meaningless, as opposed to just the erroneous operation having an unpredictable result. In these languages erroneous operations are said to have undefined behavior.
Since memory safety bugs are often security issues, memory safe languages are more secure than languages that are not memory safe. Memory safe languages include Rust, Go, C#, Java, Swift, Python, and JavaScript. Languages that are not memory safe include C, C++, and assembly.
I have a bunch of integers which represent registers
The obvious thing here is not to have a bunch of integers. Create an array
int[] registers = new int[NUMBER_OF_REGISTERS];
Your above code turns into
{
int r1Index, r2Index;
r1Index = GetCorrectRegisterIndex(first);
r2Index = GetCorrectRegisterIndex(second);
switch ((OpCode)current.opcode)
{
case OpCode.ADDR:
registers[r1Index] = registers[r1Index] + registers[r2Index];
break;
}
}
Additionally, use an enum
to create symbolic names for your register names where each name gives you the right index.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With