Joseph L. answered 05/03/26
Coding, Game Dev, AI and Tech Solutions Professional
0011000000000000 ; 0 : 3 0 read n into accumulator
0010000000111100 ; 2 : 2 60 store n at location 60
0001000000111100 ; 4 : 1 60 load n
0110000001000110 ; 6 : 6 70 subtract 1 (acc = n - 1)
1001000000101100 ; 8 : 9 44 if acc < 0 (n < 1) jump to neg_case at 44
1000000000110100 ; 10 : 8 52 if acc == 0 (n == 1) jump to eq_case at 52
; --- fall through: n > 1, acc still holds n-1 ---
0010000000111110 ; 12 : 2 62 store m = n-1 at location 62
0010000001000000 ; 14 : 2 64 store counter = m at location 64
0001000001000100 ; 16 : 1 68 load constant 0
0010000001000010 ; 18 : 2 66 store result = 0 at location 66
; --- loop: result += m, counter-- ---
0001000001000000 ; 20 : 1 64 load counter
1000000000100110 ; 22 : 8 38 if counter == 0 jump to done at 38
0001000001000010 ; 24 : 1 66 load result
0101000000111110 ; 26 : 5 62 add m
0010000001000010 ; 28 : 2 66 store result
0001000001000000 ; 30 : 1 64 load counter
0110000001000110 ; 32 : 6 70 subtract 1
0010000001000000 ; 34 : 2 64 store counter
0111000000010100 ; 36 : 7 20 jump back to loop at 20
; --- done: output result = (n-1)^2 ---
0001000001000010 ; 38 : 1 66 load result
0100000000000000 ; 40 : 4 0 output result
0000000000000000 ; 42 : 0 0 halt
; --- neg_case: n < 1, output n - 2 ---
0001000000111100 ; 44 : 1 60 load n
0110000001001000 ; 46 : 6 72 subtract 2
0100000000000000 ; 48 : 4 0 output n - 2
0000000000000000 ; 50 : 0 0 halt
; --- eq_case: n == 1, output n + 5 = 6 ---
0001000000111100 ; 52 : 1 60 load n
0101000001001010 ; 54 : 5 74 add 5
0100000000000000 ; 56 : 4 0 output n + 5
0000000000000000 ; 58 : 0 0 halt
; --- storage and constants ---
0000000000000000 ; 60 : 0 0 storage for n
0000000000000000 ; 62 : 0 0 storage for m (= n - 1)
0000000000000000 ; 64 : 0 0 storage for loop counter
0000000000000000 ; 66 : 0 0 storage for result
0000000000000000 ; 68 : 0 0 constant 0
0000000000000001 ; 70 : 0 1 constant 1
0000000000000010 ; 72 : 0 2 constant 2
0000000000000101 ; 74 : 0 5 constant 5