; example: Array counting program using self-modifying code
; Count elements in array [1,3,5,4,7] that are < 4
; Expected result: 2 (elements 1 and 3)
; Based on original Machine W algorithm (from my labs)
start:
loop: POB n ; Load remaining count
ODE one ; Subtract 1
SOM end ; If n-1 < 0, we're done
LAD n ; Store decremented n
calc: POB tab ; Load array element (self-modifying!)
ODE a ; Subtract threshold
SOM cpp ; If negative, increment counter
return: POB calc ; Load address of calc instruction
DOD one ; Increment address by 1
LAD calc ; Modify calc instruction to point to next element
SOB loop ; Continue loop
; Variables
a: RST 4 ; Threshold = 4
tab: RST 1 ; Array element 0 = 1
RST 3 ; Array element 1 = 3
RST 5 ; Array element 2 = 5
RST 4 ; Array element 3 = 4
RST 7 ; Array element 4 = 7
n: RST 5 ; Array length = 5
one: RST 1 ; Constant 1
count: RST 0 ; Counter = 0
; Increment counter subroutine
cpp: POB count ; Load counter
DOD one ; Increment it
LAD count ; Store back
SOB return ; Return to main loop
; End program
end: POB count ; Load final result
WYJ ; Output it
STP ; Stop