Christopher B. answered 04/18/24
Bachelors in Computer Science, looking to help someone get there!
Below is a Wombat1 machine language program tailored to calculate the function ๐(๐) as defined, where the function behaves differently based on the value of ๐ (<2, =2, >2). This program reads an input integer value ๐, computes ๐(๐) accordingly, and displays the output. The program is written for execution in the CPUSim simulator with Wombat1 machine language, including comments for clarity.
; Binary; Line; Decimal-Opcode, Decimal-Operand; Comments
0001 000000000001; 00; 1, 1; Read n into R1
0110 000000000010; 01; 6, 2; Store n from R1 to memory location 2
0010 000000000001; 02; 2, 1; Load n from R1 to AC
1101 000000001010; 03; 13, 10; JUMP+ to line 10 if AC > 2
1110 000000000101; 04; 14, 5; JUMP0 to line 5 if AC = 0
1101 000000001001; 05; 13, 9; JUMP+ to line 9 if AC > 0 (This checks for n=2)
; Case n < 2:
0000 000000000001; 06; 0, 1; Load n into AC
0000 000000000001; 07; 0, 1; Load n into AC (redundant, for clarity in multiplication)
0111 000000000011; 08; 7, 3; Store result (n^2) to memory location 3, end case
; Case n = 2:
0110 000000000010; 09; 6, 2; Store n (2) to memory location 2
0111 000000000011; 10; 7, 3; Store result (2-8=-6) to memory location 3, end case
; Case n > 2:
0000 000000000001; 11; 0, 1; Load n into AC
0000 000000000001; 12; 0, 1; Load n into AC (for multiplication)
0000 000000000001; 13; 0, 1; Load n into AC (redundant, for clarity in multiplication)
0000 000000000001; 14; 0, 1; Load n into AC (for addition)
1000 000000001101; 15; 8, 13; Add 6n
1000 000000000101; 16; 8, 5; Add 5
0111 000000000011; 17; 7, 3; Store result (n^2 + 6n + 5) to memory location 3
0101 000000000011; 18; 5, 3; Output from memory location 3
1111 000000000000; 19; 15, 0; HALT