i need to create an extension of MAL. So, first i had tried to add on mic1sim.mal
MAR = SP = SP + 1; rd // read top of the stack
H = TOS // H will be a SP
MDR = H = H >> 1; wr // SHIFT 1 and write on MDR
MDR = H = H >> 1; wr // SHIFT 1 + 1 = 2
MDR = H = H >> 1; wr // SHIFT 1 + 1 + 1 = 3
MDR = H = H >> 1: wr // 4th shift
TOS = MDR; goto Main1 // TOS updated
But doesn't work! Can someone explain me where i wrong?
SP points to the top of the stack. SP + 1 points to data that is no longer on the stack. Reading this value will return garbage. You have a condition where two different values are being written to MDR in the third instruction: the result from the ALU operation and the data from memory from the read in the first instruction.
It is not necessary to write after each shift. Calculate the result in H. When done, store the value of H into MDR and write the value to the stack.
TOS already has the top value on the stack at the start of the instruction. It is not necessary to read it.