#include /* for setting serial baudrate */ __sfr __at 0X89 TMOD; __sfr __at 0x98 SCON; __sfr __at 0x8D TH1; __sbit __at 0x8E TR1; /** TCON.6 */ /* for serial transmit */ __sfr __at 0x99 SBUF; __sbit __at 0x98 RI; /** SCON.0 */ __sbit __at 0x99 TI; /** SCON.1 */ /* SDCC printf needs this to be defined */ void putchar(char c) { SBUF = c; while (!TI); TI = 0; } /* SDCC gets (no scanf) needs this to be defined */ char getchar(void) { char c; while(!RI); c = SBUF; RI = 0; return c; } #define NAME_SIZE_MAX 16 #define BUFF_SIZE_MAX 4 #define THIS_YEAR 2013 char name[NAME_SIZE_MAX], buff[BUFF_SIZE_MAX]; int year, age; int convert_int(char* buff) { int calc = 0, length = 0, mult = 1; /** get string length */ while(buff[length]) length++; /** browse through the array - from right! */ while(length>0) { length--; /** check if really numeric */ if(buff[length]<0x30||buff[length]>0x39) return 0; /** get digit actual value */ calc = calc + (buff[length]-0x30)*mult; /** prepare next iteration */ mult = mult * 10; } return calc; } /* main function */ void main() { TMOD = 0x21; SCON = 0x50; TH1 = 253; TR1 = 1; while(1) { /** get user input */ printf("Enter name [MAX=%d]: ",NAME_SIZE_MAX); gets(name); printf("Enter your age (Max=99): "); gets(buff); /** process year of birth */ age = convert_int(buff); year = THIS_YEAR - age; /** display */ printf("Hi, %s! You were born in %d\n",name,year); } }