BCDbyte tba ; BCDbyte converts the packed BCD byte in B anda #\$0F ; to the equivalent hex value. andb #\$F0 ; The result is returned in both A and B lsrb ; No other registers or memory are affected aba ; Equivalent C code: lsrb ; lsrb ; int BCDbyte(int i) aba ; {return ((i&0xF0)>>4)*10 + (i&0x0F);} tab rts BCDword psha ; BCDword converts the packed BCD word in D xgdx ; to the equivalent hex value. pulb ; The result is returned in D. jsr BCDbyte ; The X register is destroyed. ldab #100 ; Equivalent C code: mul ; xgdx ; int BCDword(int i) { jsr BCDbyte ; return BCDbyte((i&0xFF00)>>8) * 100 + abx ; BCDbyte(i&0x00FF); xgdx ; } rts ------------------------------------------------------------------------------ struct Date { // Note: This sturcture does not generate code int year; // It only declares what a Date looks like char month; // January = 1, etc. char day; }; Date dates[3] = {0x2001, 0x10, 0x31, // Note values are initialized in BCD 0x1900, 0x01, 0x01, 0x1776, 0x07, 0x04}; char moonAge[3]; // In the following use integer division (discard remainders) // X%Y means X modulo Y; i.e., the remainder when X is divided by Y int getAge(Date* d) { // Compute the Age of the Moon (days since new) int C; // Note that C, T, Y, M, and D are local variables int T; // (they live on the stack) int Y = BCDword(d->year); char M = BCDbyte(d->month); char D = BCDbyte(d->day); if (M <= 2) { M = M + 12; Y = Y - 1; } M = M - 2; C = Y / 100; // Discard remainder T = 11 * (Y%19) + 38 + C/3 + C/4 - C + M + D; return T%30; } void main(void) { char i; for (i=0; i<3; i++) moonAge[i] = getAge(&dates[i]); // Pass the ADDRESS of date on stack } // return value on stack, too.