Xv6 with picoc & Linkage editor
v1.0
The project delineate mutual cohesion between c library, linkage editor ( linker), interpreter and operating system by porting the same on xv6 kernel
|
00001 /* example from http://barnyard.syr.edu/quickies/led.c */ 00002 00003 /* led.c: print out number as if on 7 line led display. I.e., write integer 00004 given on command line like this: 00005 _ _ _ 00006 | _| _| |_| |_ 00007 | |_ _| | _| etc. 00008 00009 We assume the terminal behaves like a classical teletype. So the top 00010 lines of all digits have to be printed first, then the middle lines of 00011 all digits, etc. 00012 00013 By Terry R. McConnell 00014 00015 compile: cc -o led led.c 00016 00017 If you just want to link in the subroutine print_led that does all the 00018 work, compile with -DNO_MAIN, and declare the following in any source file 00019 that uses the call: 00020 00021 extern void print_led(unsigned long x, char *buf); 00022 00023 Bug: you cannot call repeatedly to print more than one number to a line. 00024 That would require curses or some other terminal API that allows moving the 00025 cursor to a previous line. 00026 00027 */ 00028 00029 00030 00031 #include <stdlib.h> 00032 #include <stdio.h> 00033 00034 #define MAX_DIGITS 32 00035 #define NO_MAIN 00036 00037 00038 /* Print the top line of the digit d into buffer. 00039 Does not null terminate buffer. */ 00040 00041 void topline(int d, char *p){ 00042 00043 *p++ = ' '; 00044 switch(d){ 00045 00046 /* all these have _ on top line */ 00047 00048 case 0: 00049 case 2: 00050 case 3: 00051 case 5: 00052 case 7: 00053 case 8: 00054 case 9: 00055 *p++ = '_'; 00056 break; 00057 default: 00058 *p++=' '; 00059 00060 } 00061 *p++=' '; 00062 } 00063 00064 /* Print the middle line of the digit d into the buffer. 00065 Does not null terminate. */ 00066 00067 void midline(int d, char *p){ 00068 00069 switch(d){ 00070 00071 /* those that have leading | on middle line */ 00072 00073 case 0: 00074 case 4: 00075 case 5: 00076 case 6: 00077 case 8: 00078 case 9: 00079 *p++='|'; 00080 break; 00081 default: 00082 *p++=' '; 00083 } 00084 switch(d){ 00085 00086 /* those that have _ on middle line */ 00087 00088 case 2: 00089 case 3: 00090 case 4: 00091 case 5: 00092 case 6: 00093 case 8: 00094 case 9: 00095 *p++='_'; 00096 break; 00097 default: 00098 *p++=' '; 00099 00100 } 00101 switch(d){ 00102 00103 /* those that have closing | on middle line */ 00104 00105 case 0: 00106 case 1: 00107 case 2: 00108 case 3: 00109 case 4: 00110 case 7: 00111 case 8: 00112 case 9: 00113 *p++='|'; 00114 break; 00115 default: 00116 *p++=' '; 00117 00118 } 00119 } 00120 00121 /* Print the bottom line of the digit d. Does not null terminate. */ 00122 00123 void botline(int d, char *p){ 00124 00125 00126 switch(d){ 00127 00128 /* those that have leading | on bottom line */ 00129 00130 case 0: 00131 case 2: 00132 case 6: 00133 case 8: 00134 *p++='|'; 00135 break; 00136 default: 00137 *p++=' '; 00138 } 00139 switch(d){ 00140 00141 /* those that have _ on bottom line */ 00142 00143 case 0: 00144 case 2: 00145 case 3: 00146 case 5: 00147 case 6: 00148 case 8: 00149 *p++='_'; 00150 break; 00151 default: 00152 *p++=' '; 00153 00154 } 00155 switch(d){ 00156 00157 /* those that have closing | on bottom line */ 00158 00159 case 0: 00160 case 1: 00161 case 3: 00162 case 4: 00163 case 5: 00164 case 6: 00165 case 7: 00166 case 8: 00167 case 9: 00168 *p++='|'; 00169 break; 00170 default: 00171 *p++=' '; 00172 00173 } 00174 } 00175 00176 /* Write the led representation of integer to string buffer. */ 00177 00178 void print_led(unsigned long x, char *buf) 00179 { 00180 00181 int i=0,n; 00182 static int d[MAX_DIGITS]; 00183 00184 00185 /* extract digits from x */ 00186 00187 n = ( x == 0L ? 1 : 0 ); /* 0 is a digit, hence a special case */ 00188 00189 while(x){ 00190 d[n++] = (int)(x%10L); 00191 if(n >= MAX_DIGITS)break; 00192 x = x/10L; 00193 } 00194 00195 /* print top lines of all digits */ 00196 00197 for(i=n-1;i>=0;i--){ 00198 topline(d[i],buf); 00199 buf += 3; 00200 *buf++=' '; 00201 } 00202 *buf++='\n'; /* move teletype to next line */ 00203 00204 /* print middle lines of all digits */ 00205 00206 for(i=n-1;i>=0;i--){ 00207 midline(d[i],buf); 00208 buf += 3; 00209 *buf++=' '; 00210 } 00211 *buf++='\n'; 00212 00213 /* print bottom lines of all digits */ 00214 00215 for(i=n-1;i>=0;i--){ 00216 botline(d[i],buf); 00217 buf += 3; 00218 *buf++=' '; 00219 } 00220 *buf++='\n'; 00221 *buf='\0'; 00222 } 00223 00224 int main() 00225 { 00226 char buf[5*MAX_DIGITS]; 00227 print_led(1234567, buf); 00228 printf("%s\n",buf); 00229 00230 return 0; 00231 } 00232 00233 #ifndef NO_MAIN 00234 int main(int argc, char **argv) 00235 { 00236 00237 int i=0,n; 00238 long x; 00239 static int d[MAX_DIGITS]; 00240 char buf[5*MAX_DIGITS]; 00241 00242 if(argc != 2){ 00243 fprintf(stderr,"led: usage: led integer\n"); 00244 return 1; 00245 } 00246 00247 /* fetch argument from command line */ 00248 00249 x = atol(argv[1]); 00250 00251 /* sanity check */ 00252 00253 if(x<0){ 00254 fprintf(stderr,"led: %d must be non-negative\n",x); 00255 return 1; 00256 } 00257 00258 print_led(x,buf); 00259 printf("%s\n",buf); 00260 00261 return 0; 00262 00263 } 00264 #endif