Processor Design (CPU Design) The esoteric language LMCode

 3r3886. 3r3-31. Processor Design (CPU Design) The esoteric language LMCode
 3r3886. Movement I
 3r3886. Movement II
 3r3886. Part III
 3r3886. Movement IV
 3r3886.
 3r3886. This article is devoted to the creation of an interpreter of a certain esoteric language LMCode, which is based on the architecture. Little Man Computer .
 3r3886. About Little Man Computer can be found in previous articles.
 3r3886. Let r3r3872.  3r3886. 3r3149.  3r3886.
INP command corresponds to 3r3641. , [/b]
 3r3886.
OUT command corresponds to r3r3641. . [/b]
 3r3886.
ADD team corresponds to r3r3641. + [/b]
 3r3886.
SUB command corresponds to r3r3641. - 3r3629.
 3r3886.
STA command corresponds to r3r3641. ~ [/b]
 3r3886.
LDA team corresponds to r3r3641. ^ [/b]
 3r3886. 3r3161.
 3r3886.
 3r3886. We write a program that loads a number from an input device into a battery, stores a number in memory, adds a number from memory to a battery (doubles the number), and outputs a double number to an output device.
 3r3886. On the LMC assembler, this program will look like this (the starting cell will be 20) 3r3-33872.  3r3886. 3r3778. INP 3r3886. STA 20
ADD 20
OUT
3r33838. 3r33871.
 3r3886.
3r33875.
 3r3886. In the LMCode language, this program will look like , ~ +.
 3r3886. In our LMCode machine, the code memory and the data memory are separated (Harvard architecture), we create the line 3r3641. str_arr [/b] for loading LMCode code. Line 3r3641. str_arr [/b] will represent the memory of commands. Also create an array array data_arr that will represent the data memory.
 3r3886. Download in 3r3641. str_arr [/b] the program , ~ +.
 3r3886. 3r3778. #include
3r3886. int main (void) {3r3886. int i = 0; //index of the row
int j = 0; //index of the data array
int acc = 0; 3r3886. char str_arr[100]= ", ~ +."; 3r3886. int data_arr[10]= {0}; 3r3886. 3r3886. while (str_arr! = 'data_arr[j]= acc; 3r3886. if (str_arr== '^')
acc = data_arr[j]; 3r3886. if (str_arr[i]== '.') {
printf ("Output:% d", acc); 3r3886. printf (""); 3r3886.}; 3r3886. if (str_arr[i]== '}')
pz_prev = 1; 3r3886. 3r3886. if (str_arr[i]== '?') {
while (str_arr[i]! = '!')
i ++; 3r3886.}
3r3886. if (str_arr[i]== '{' && pzf == 1 && pz_prev == 0) {
while (str_arr[i]! = '}')
i ++; 3r3886.}
if (str_arr[i]== '{' && pzf == 1 && pz_prev == 1) {
while (str_arr[i]! = '}')
i--; 3r3886.}
if (acc> = 0) {3r3886. pzf = 1;} 3r3886. else {
pzf = 0;} 3r3886. 3r3886. //printf ("i =% d", i); printf (""); 3r3886. i ++; 3r3886.}
printf ("n"); 3r3886. for (int k = 0; k <10; k++)
printf ("% d", data_arr[k]); r3r3886. return 0;
)  3r3886. 3r33526. ideone.com
 3r3886.
 3r3886. To multiply two numbers A and 3r3641. B [/b] , you need A times to 3r3641. B [/b] add B .
 3r3886. In the loop at each iteration, subtract from 3r3641. A [/b] unit, and while And no zero, add 3r3641. In 3r3629. towards In 3r3629. .
 3r3886. LMCode program } ^ <+> ~ ^> - 3r33556. multiplies the numbers A + 1 and 3r3641. In 3r3629. i.e. one factor must be deliberately reduced by one.
 3r3886. This happens because the cycle will end only when in acc will be -1 .
 3r3886. For example, multiply 5 on 3r3641. 5
.
 3r3886. To do this, first place the necessary values ​​in data_arr
 3r3886. 3r3778. data_arr[0]= 4; 3r3886. data_arr[1]= 1; 3r3886. data_arr[2]= 5; 3r3886. 3r33838. 3r33871.
 3r3886. 3r33588. ideone.com
 3r3886.
 3r3886. Add unconditional transitions [i] ago
.
 3r3886. To do this, add the variable prev .
 3r3886. Also add transitions forward /backward under the condition 3r32424. acc = 0 r3r3625. . For such transitions, create a flag zf and the variable z_prev .
 3r3886. Transitions by condition zf == 1 will implement teams 3r3641. (
And )
 3r3886. Multiply 5 and 3r3641. 5 [/b] using the unconditional transition and the transition condition 3r3624. zf == 1
.
 3r3886. First, we place the necessary values ​​in 3r3641. data_arr [/b]
 3r3886. 3r3778. data_arr[0]= 5; 3r3886. data_arr[1]= 1; 3r3886. data_arr[2]= 5; 3r3886. 3r33838. 3r33871.
 3r3886. LMCode progamme ! ^ <+> ~ ^> - <~(?)^. corresponds to the assembler program
 3r3886. 3r3778. INP 3r3886. STA 20
INP 3r3886. STA 21
INP 3r3886. STA 22
LDA 23 3r3886. ADD 22 3r3886. STA 23 3r3886. LDA 20
SUB 21 3r3886. STA 20
BRZ 14-3r3886. BRA 06 3r3886. LDA 23 3r3886. OUT
Hlt 3r3886. 3r33838. 3r33871.
 3r3886. C code
 3r3886. 3r3778. #include
3r3886. int main (void) {3r3886. int i = 0; 3r3886. int j = 0; 3r3886. int acc = 0; 3r3886. int pzf = 1; 3r3886. int zf = 1; 3r3886. int pz_prev = 0; 3r3886. int z_prev = 0; 3r3886. int prev = 0; 3r3886. char str_arr[100]= "! ^ <+> ~ ^> - 3r388685. 3r3886. Int data_ar[10]3 = = {0}; 3r3886. Data_ar. C. . while (str_arr[i]! = '874. Ideone.com
3r33882. 3r3886. 3r3886. 3r33879. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () (); 3r33880. 3r3886. 3r33882. 3r3886. 3r3886. 3r3886. 3r3886.
+ 0 -

Add comment