汇编能够实现显示当前日期,并输出当前日期是这一年中第多少天及第二天的日期。马上要交作业了,求大神!!
汇编能够实现显示当前日期,并输出当前日期是这一年中第多少天及第二天的日期。马上要交作业了,求大神!!
程序代码:
#include <stdarg.h> extern void puts (const char *s); /* 用汇编实现 */ extern void gets (char *s); /* 用汇编实现 */ #define is_white(c) (c == ' ' || c == '\t' || c == '\r' || c == '\n') #define to_int(c) ('0' <= c && c <= '9' ? c - '0' :\ 'a' <= c && c <= 'f' ? c - 'a' + 10 : \ 'A' <= c && c <= 'F' ? c - 'A' + 10 : 17) int vsscanf (const char *buff, const char *fmt, va_list ap) { int base = 0, sign = 0; int c, nr = 0, v; char *str; int *int; while ((c = *fmt++)) { if (is_white (c)) { while (is_white (*buff)) ++buff; } else if (c != '%' || (c = *fmt++) == '%') { if (c != *buff++) break; } else if (!*buff || is_white (*buff)) break; else switch ((c)) { case 'c': str = va_arg (ap, char *); *str = *buff++; ++nr; break; case 's': str = va_arg (ap, char *); *str while (*buff && !is_white(*buff)) *str++ = *buff++; *str = 0; ++nr; break; case 'o': base = 8; goto case_int; case 'd': base = 10; goto case_int; case 'x': base = 16; goto case_int; case_int: v = sign = 0; if (*buff == '-') sign = 1, ++buff; while ((c = to_int (*buff)) < base) { v = v * base + c; ++buff; } * va_arg (ap, int *) = sign ? -v : v; ++nr; break; } } } int scanf (const char *buff, const char *fmt, ...) { int res; va_list ap; char buff[512]; gets (buff); va_start (ap, fmt); res = vsscanf (buff, fmt, ap); va_end (ap); return res; } void vsprintf (char *buff, const char *s, va_list ap) { int c, n, base = 10, sign = 0; const char *str = NULL; char intbuff[65], *pint; static const char *alphas = "0123456789ABCDEF"; while ((c = *s++)) { if (c != '%') *buff++ = c; else switch ((c = *s++)) { case 0: goto outloop; case 'c': *buff++ = va_arg (ap, int); break; case '%': *buff++ = '%'; break; case 's': str = va_arg (ap, const char *); case_str: while ((*buff++ = *str++)); break; case 'x': base = 16; goto case_int; case 'o': base = 8; goto case_int; case 'd': base = 10; goto case_int; case_int: sign = 0; n = va_arg (ap, int); if (n < 0) n = -n, sign = 1; pint = intbuff + sizeof (intbuff) - 1; *pint-- = 0; do *pint-- = alphas [n % base]; while ((n /= base)); if (sign) *pint-- = 0; str = pint + 1; goto case_str; } } *buff++ = 0; } void printf(const char *fmt, ...) { va_list ap; char buff[512]; va_start (ap, fmt); vsprintf (buff, fmt, ap); va_end (ap); puts (buff); } inline int is_leap (int year) { return !(year % 4) && ((year % 25) || !(year % 16)); } static const int tab_daysofyear_acc[2][13] = { {非闰年的天数积累}, {闰年的天数积累}, }; int get_daysofyear_and_nextday(int *pyear, int *pmon, int *pdate) { const int *tab = tab [is_leap (year)]; int year = *pyear, mon = *pmon, *date = *pdate, days; days = tab[mon] + date; if (++date - 1 > tab[mon+1] - tab[mon]) { date -= (tab[mon+1] - tab[mon]); if (++mon > 12) { mon -= 12; year += 1; } } *pyear = year; *pmon = mon; *pdate = date; return days; } void do_main_loop (void) { int n, y, m, d, days; while (1) { printf ("input date(year month date)>> "); n = scanf ("%d %d %d", &y, &m, &d); if (n != 3) printf ("input error!\n"); else { printf ("the %04d-%02d-%02d is ", y, m, d); days = get_daysofyear_and_nextday (&y, &m, &d); printf ("%d days in this year.\n" "next date is: %04d-%02d-%02d \n", days, y, m, d); } } } int main(int argc, char **argv) { do_main_loop (); return 0; }
其中键盘的输入和屏幕的输出用汇编写,gcc -S把代码生成汇编,大概能达到楼主的要求,随手写的没测试过。