首页 > 其他分享 >c: machine0 - 机器语言的模型机

c: machine0 - 机器语言的模型机

时间:2023-02-26 01:55:40浏览次数:60  
标签:directive int 模型 char directives void printf 机器语言 machine0

c:  machine0 - 机器语言的模型机

 

 

 

 

一、源码

 1 [wit@eagle src]$ cat  machine0.c
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 
 6 
 7 
 8 
 9 // explains everyone of directives
10 void execute(char* in)
11 {
12   if( in == "0000")
13   {
14     printf("directive: mov \n");
15   }
16   else
17   {
18     printf("directive: '%s' directive not defined.\n", in);
19   }
20 }
21 
22 
23 
24 // deal with diretvies
25 void directive(char *in)
26 {
27 
28   // parameter 'in' limited 4 bits
29   int len = strlen(in);
30   if( len == 4 )
31   {
32     printf("os: directive()  running... \n");
33     execute(in);
34   }
35   else
36   {
37     printf("\n");
38     printf("error: directives of inputing is wrong, reinput directives ...\n");
39     printf("directive: 4bits; eg:\"0000\",\"0101\". \n");
40     printf("\n");
41   }
42 
43 }
44 
45 
46 // main programming of directives
47 void run(char* in)
48 {
49   directive(in);
50 }
51 
52 
53 
54 
55 int main(int argc, char* argv[], char* envp[])
56 {
57   // printf("argc = %d\n", argc);
58   for(int i=1; i<argc; i++)
59   {
60     printf("\n");
61     printf("directive = %s\n", argv[i]);
62     run(argv[i]);
63     printf("\n");
64   }
65 
66 
67   return 0;
68 }

 

 

 

二、运行结果:

 1 [wit@eagle src]$ cat  machine0.c
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 
 6 
 7 
 8 
 9 // explains everyone of directives
10 void execute(char* in)
11 {
12   if( in == "0000")
13   {
14     printf("directive: mov \n");
15   }
16   else
17   {
18     printf("directive: '%s' directive not defined.\n", in);
19   }
20 }
21 
22 
23 
24 // deal with diretvies
25 void directive(char *in)
26 {
27 
28   // parameter 'in' limited 4 bits
29   int len = strlen(in);
30   if( len == 4 )
31   {
32     printf("os: directive()  running... \n");
33     execute(in);
34   }
35   else
36   {
37     printf("\n");
38     printf("error: directives of inputing is wrong, reinput directives ...\n");
39     printf("directive: 4bits; eg:\"0000\",\"0101\". \n");
40     printf("\n");
41   }
42 
43 }
44 
45 
46 // main programming of directives
47 void run(char* in)
48 {
49   directive(in);
50 }
51 
52 
53 
54 
55 int main(int argc, char* argv[], char* envp[])
56 {
57   // printf("argc = %d\n", argc);
58   for(int i=1; i<argc; i++)
59   {
60     printf("\n");
61     printf("directive = %s\n", argv[i]);
62     run(argv[i]);
63     printf("\n");
64   }
65 
66 
67   return 0;
68 }
69 [wit@eagle src]$
70 [wit@eagle src]$
71 [wit@eagle src]$ gcc -g -o machine0 machine0.c && ./machine0 0000 1100
72 
73 directive = 0000
74 os: directive()  running...
75 directive: '0000' directive not defined.
76 
77 
78 directive = 1100
79 os: directive()  running...
80 directive: '1100' directive not defined.
81 
82 [wit@eagle src]$
83 [wit@eagle src]$

 

 

 

 

三、参考资料:无

 

标签:directive,int,模型,char,directives,void,printf,机器语言,machine0
From: https://www.cnblogs.com/lnlidawei/p/17156026.html

相关文章