Nios/accum1

From ASMBits

Write a function that performs a sequence of calculations (addition, subtraction, multiplication), following instructions encoded in an array of bytes.

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 ('+', '-', '*', or null), and the second byte is an 8-bit signed operand (Note that the accumulator is 32 bits). Implement the following 4 operations:

  1. '+': Accumulator <= Accumulator + operand
  2. '-': Accumulator <= Accumulator - operand
  3. '*': Accumulator <= Accumulator * operand
  4. null: Return from the function and return the current Accumulator value in r2.

For example, the (null-terminated) string "+0+@-P" encodes 4 operations:

  1. "+0": Add 0x30 to the accumulator. The accumulator starts off as 0, and now contains 0x30
  2. "+@": Add 0x40 to the accumulator. The accumulator now contains 0x70
  3. "-P": Subtract 0x50 from the accumulator. The accumulator now contains 0x20
  4. null: Quit. The function returns 0x20 in r2

Similarly, the following will compute 100*100:

.byte '+', 100, '*', 100, 0

Starting with an accumulator with 0, add 100, then multiply by 100, then return the result.

Write a function that will process the sequence of instructions and return the final value of the accumulator.

Sample Input

"+0+@-P"

Sample Output

r2=0x20

Write your solution here

Upload a source file...