My dream CPU

Program Examples




Hello World - Sum 2 numbers
0 I: rA <-- #2 ;Load register rA from Int unit with 2
2 I: rB <-- #3 ;Load register rB from Int unit with 3
4 I: AplsB --> M[]300 ;Store( rA + rB ) in address 300
6 ENDPRG ;End of program. This is not instruction, it is assembler directive.

Hello World - Sum 2 floating numbers
0 A: rBs1 <-- #1000 ;Load register rBs1 from Adress unit with 1000
2 D: rA <-- M[rBs1,]0 ;Load register rA from Int unit from address pointed from register rBs1
4 I: rB <-- M[rBs1,]2 ;Load register rB from Int unit from address pointed from register rBs1 with offset 2
6 I: AplsB --> M[rBs1]4 ;Store( rA + rB ) in address pointed from register rBs1 with offset 4
8 ENDPRG ;End of program. This is not instruction, it is assembler directive.
. . .
1000 2.0
1001 0.4
1002 5.0
1003 0.7

Stack example
0 I: rA <-- #7 ; Load register rA with 7
2 I: rB <-- #-10 ; Load register rA with -10
4 I: push rA ; Push register rA in stack
6 I: push rB ; Push register rB in stack.
8 I: pop rA ; Pop register rA from stack. Now it is -10
10 I: pop rB ; Pop register rB from stack. Now it is 7
12 ENDPRG ;

Result is: values in rA and rB in Integer unit are exchanged.

If example
0 I: rA <-- #2 ; Load rA with 2
2 I: rB <-- #3 ; Load rB with 3
4 I: if ( A < B ) jump #10 ; If rA < rB then jump to address 10
6 I: AplsB --> M300 ; Calc sum rA + rB and store in address 300
8 A: rIP --> #12 ; Jump to END program
10 I: AmnsB --> M300 ; Calc subtraction rA - rB and store result in address 300
12 ENDPRG ;

Result is -1 in memory address 300.

Hello World - For loop Example
0 I: rA <-- #1 ; Load rA with 1
2 I: rB <-- #5 ; Load rB with 5
4 A: rMaxX <-- #5 ; Load rMaxX with 5
6 A: IfMaxX rX1 >= rMaxX ; #16 ; If rX1 > rMaxX jump over the body cycle mem adr 16. Cheking FOR loop condition
8 I: AmulB --> M120 ; Calc rA * rB store result in memory address 120
10 I: rA <-- M120 ; Load rA from memory address 120
12 A: IncX rX1 #1 ; Increment rX1 with 1
14 A: rIP <-- #6 ; Jump to memory address 6. End loop body.
16 ENDPRG ; program end


Hello World - Register to Register Example
0 I: rA <-- #1 ; Load I rA with 1
2 I: rB <-- #2 ; Load I rB with 2
4 D: rA <-- #3.4 ; Load D rA with 3.4
7 D: rA <-- #5.6 ; Load D rB with 5.6
10 I: rA --> D: rA ; Set D rA whth value from I rA, 1
12 D: rB --> I: rB ; Set I rB with value from D rB. Int part only 5
14 ENDPRG ;


Hello World - Boolean operations Example
0 I: rA <-- #1 ; Load rA with 1
2 I: rB <-- #2 ; Load rB with 2
4 I: AandB --> M200 ; Calc rA & & rB and store in memory address 200
6 I: AorB --> M201 ; Calc rA | rB and store in memory address 201
8 I: AxorB --> M202 ; Calc rA ^ rB and store in memory address 202
10 ENDPRG ;


Hello World - Shift operations Example
0 I: rA <-- #1000 ; Load rA with 1000
2 I: rB <-- #2 ; Load rB with 2
4 I: AshlB --> M200 ; Shift rA 2 times in left, store result in memory address 200
6 I: AshrB --> M201 ; Shift rA 2 times in right, store result in memory address 201
8 I: AshrrB --> M202 ; Shift rA 2 times - unsigned right shift, store result in memory address 202
10 ENDPRG ;


Bubble sort

This simple program demonstrates bubble sort algorithm, using SortAB instruction. Program can be optimized, but it is only for demo use.

0 A: rBs1 <-- #1000 ; Load rBs1 with 1000. Here is the beginnig of array to be sorted.
2 A: rX1 <-- # ; Load rX1 with 0. clear
4 A: rMaxX <-- M950 ; Set rMaxX with value in address 950. Array size.
6 A: IfMaxX rX1 >= rMaxX ; #22 ; Start loop, loop condition check. If rX1 > rMaxX, then skip body loop.
8 I: rA <-- M[rBs1, rX1]0 ; Load rA with element from the array. Element adr is rBs1 + rX1
10 I: rB <-- M[rBs1, rX1]1 ; Load rB with next element from array. Element adr is rBs1 + rX1 + 1
12 I: SortAB < & IfNoExch #18 ; Sort A,B. If there is no exchange, jump to 18 - Skip store of elements.
14 I: rA --> M[rBs1, rX1]0 ; Store rA in array in position rBs1 + rX1
16 I: rB --> M[rBs1, rX1]1 ; Store rB in array in position rBs1 + rX1 + 1
18 A: IncX rX1 #1 ; Increment rX1, wich point element in the array to be sorted.
20 A: rIP <-- #6 ; Set rIP to 6. Unconditional JUMP. Jump to begenning of the loop.
22 A: rX1 <-- #0 ; Clear rX1, wich point element in the array.
24 I: rA <-- M950 ; Load rA with array size from adr 950.
26 I: rB <-- #1 ; Set rB to 1
28 I: AmnsB --> M950 ; Decrease array size with 1 and store result in adr 950.
30 I: rA <-- M950 ; Load Array size. The rest part from it, unsorted.
32 I: rB <-- #0 ; Set rB = 0
34 I: if ( A > B ) jump #4 ; If rest array size is > 0 jump to adr 4 - repeat sort with rest array part.
36 ENDPRG ; End of program directive.
. . . . . . ;
950 19 ; Contain array size. Decreased on every loop. ; 20 elements
. . . . . . ;
1000 57 ; Begin of array which will be sorted. element#0
1001 100 ;
1002 33 ;
1003 21 ;
1004 17 ;
1005 1 ;
1006 6 ;
1007 18 ;
1008 81 ;
1009 28 ;
1010 83 ;
1011 38 ;
1012 85 ;
1013 68 ;
1014 87 ;
1015 88 ;
1016 89 ;
1017 12 ;
1018 23 ;
1019 77 ; End of array. Element #20.
. . . . . . ;

Hello World -
0 ;