Arm/accum2
From ASMBits
Write a function that performs a sequence of calculations (addition, subtraction, multiplication, conditional branch), following instructions encoded in an array of bytes. This is an extension of Nios/accum1, by adding two conditional branch instructions.
This calculator is based on a single 32-bit "accumulator": A 32-bit value that all of the operations will operate on. Set the accumulator to zero at the beginning of your function.
Your function will be passed one parameter: A pointer to the beginning of the byte array. In the byte array, every two bytes encode an instruction. The first byte is a character that indicates which operation to perform ('+', '-', '*', 'b', 'B', or null), and the second byte is an 8-bit signed operand (Note that the accumulator is 32 bits). Implement the following 4 operations:
- '+': Accumulator <= Accumulator + operand
- '-': Accumulator <= Accumulator - operand
- '*': Accumulator <= Accumulator * operand
- 'b': If Accumulator < 0, then skip over operand instructions and keep going. operand = 0 means continue with the following instruction, while operand = -1 means go backwards one instruction (equivalent to repeating the 'b' instruction forever.). If accumulator >= 0, then do nothing and continue reading the next instruction.
- 'B': This is exactly the same as 'b', but with the condition reversed. Branch if accumulator >= 0.
- null: Return from the function and return the current Accumulator value in r0.
For example the following will compute 1+3:
.byte '+', 1, 'B', 1, '+', 2, '+', 3, 0
Starting with an accumulator with 0, add 1, then skip over 1 instruction because the accumulator is non-negative, then add 3. (The "add 2" is skipped over because the branch-if-non-negative occurred with an accumulator of 1)
But the following will compute 1+2+3, because the branch-if-negative is not taken:
.byte '+', 1, 'b', 1, '+', 2, '+', 3, 0
Write a function that will process the sequence of instructions and return the final value of the accumulator.
Sample Input
+ 1, B 1, + 2, + 3
Sample Output
r0=0x4