global &stackPtr #32768 "0x8000 global &stackFuncReturn #0 " Increments the stack pointer " Then puts the value currently in the A register at the top of the stack func :PushToStack LDX &stackPtr INC STA &X STX &stackPtr JMP &stackFuncReturn " Loads the value from the top of the stack into the A register " Then decrements the stack pointer func :PopFromStack def &tmpValue #0 LDX &stackPtr LDA &X STA &tmpValue LDA #X SUB #1 STA &stackPtr LDA &tmpValue JMP &stackFuncReturn " Loads the value from the top of the stack into the A register " Does not move the stack pointer func :PeekStack LDX &stackPtr LDA &X JMP &stackFuncReturn " Multiply two uint values " Arguments should be placed into the stack in the following order: " Value A " Value B " Return address " Value on top of the stack is the result func :Mul def &return #0 def &valueA #0 def &valueB #0 def &total #0 " Get the return address and arguemnt &valueA and &valueB from the stack LDA #GotReturn STA &stackFuncReturn JMP #PopFromStack :GotReturn STA &return LDA #GotValueB STA &stackFuncReturn JMP #PopFromStack :GotValueB STA &valueB LDA #GotValueA STA &stackFuncReturn JMP #PopFromStack :GotValueA STA &valueA " Clear the total value incase the function has been used before LDA #0 STA &total " Check if either &valueA or &valueB is 0, if it is, return right away LDA &valueA SUB #0 JEZ #Return LDA &valueB SUB #0 JEZ #Return " Check if &valueA is smaller than &valueB SUB &valueA JLE #Loop " If &valueA is smaller than &valueB, swap them around using &total as a temp variable, then re-clear &total LDA &valueA STA &total LDA &valueB STA &valueA LDA &total STA &valueB LDA #0 STA &total " Loop &valueB times, adding &valueA to &total each time :Loop LDA &total ADD &valueA STA &total LDA &valueB SUB #1 JEZ #Return STA &valueB JMP #Loop " Push whatever is in &total to the stack :Return LDA #PushedTotal "@4 STA &stackFuncReturn LDA &total JMP #PushToStack :PushedTotal " Return to the location origionally specified JMP &return func :Main def &value #0 def &counter #0 LDA #32768 "0x8000 STA &stackPtr :Reset " Reset both the current value and counter back to #1 LDA #1 STA &value STA &counter :Loop " Add values to the stack for the Mul function LDA #PushedValue STA &stackFuncReturn LDA &value JMP #PushToStack :PushedValue LDA #PushedCounter STA &stackFuncReturn LDA &counter JMP #PushToStack :PushedCounter LDA #PushedReturn STA &stackFuncReturn LDA #DoneMul JMP #PushToStack :PushedReturn " Run the Mul function JMP #Mul :DoneMul " Get the result of the Mul function from the top of the stack and store it LDA #GotValue STA &stackFuncReturn JMP #PopFromStack :GotValue STA &value " Output the result OUT " Increment the counter and store it again LDA &counter ADD #1 STA &counter " If the counter has reached #9 reset the computer, otherwise perform another loop SUB #9 JEZ #Reset JMP #Loop