/* AneoEngine hexadecmial to decimal and binary conversion function. AneoC source file */ #include //TDs TD U C U8; TD U S U16; TD U INT U32; //kernel externs extern VD print(CC *s); extern VD putc(C c); extern VD printx(U32 x); extern VD printint(U INT n); //logic U32 extract_bit(U32 high, U32 low, INT target) {//rand 64bit INT relative_bit_pos; if (target >= 32) { relative_bit_pos = target - 32; return (high >> relative_bit_pos) & 1; } return (low >> target) & 1; } U32 divide_64(U32 high, U32 low, U32 *quotient_high_out, U32 *quotient_low_out) {//64-bit unsigned division by 10. U32 quotient_high = 0; U32 quotient_low = 0; U32 remainder = 0; INT index; U32 bit; U32 overflow_bit; U32 quotient_bit; for (index = 63; index >= 0; index = index - 1) { bit = extract_bit(high, low, index); remainder = (remainder << 1) | bit; if (remainder >= 10) { remainder = remainder - 10; quotient_bit = 1; } else { quotient_bit = 0; } overflow_bit = (quotient_low >> 31) & 1; quotient_low = (quotient_low << 1) | quotient_bit; quotient_high = (quotient_high << 1) | overflow_bit; } *quotient_high_out = quotient_high; *quotient_low_out = quotient_low; return remainder; } VD print_64(U32 high, U32 low) {//64-bit x dec printer C buf[24]; INT index = 0; U32 quotient_high; U32 quotient_low; U32 remainder; if (high == 0 && low == 0) { putc('0'); return; } while (high != 0 || low != 0) { remainder = divide_64(high, low, "ient_high, "ient_low); buf[index] = (C)remainder + '0'; index = index + 1; high = quotient_high; low = quotient_low; } while (index > 0) { index = index - 1; putc(buf[index]); } } VD print_bin64(U32 high, U32 low) {//64-bit bin printer INT index; INT hide_zeros = 0; U32 bit; for (index = 31; index >= 0; index = index - 1) { bit = (high >> index) & 1; if (bit == 1) hide_zeros = 1; if (hide_zeros == 1) putc((C)bit + '0'); } for (index = 31; index >= 0; index = index - 1) { bit = (low >> index) & 1; if (bit == 1) hide_zeros = 1; if (hide_zeros == 1) putc((C)bit + '0'); } if (hide_zeros == 0) putc('0'); } VD hex_to_highlow(CC *hex, U32 *high_out, U32 *low_out) { U32 high = 0; U32 low = 0; C chr; U32 digit; while (*hex) { chr = *hex; if (chr >= '0' && chr <= '9') digit = chr - '0'; else if (chr >= 'a' && chr <= 'f') digit = chr - 'a' + 10; else if (chr >= 'A' && chr <= 'F') digit = chr - 'A' + 10; else break; high = (high << 4) | (low >> 28); low = (low << 4) | digit; hex = hex + 1; } *high_out = high; *low_out = low; } VD convert(CC *hex_string) {//main U32 high; U32 low; hex_to_highlow(hex_string, &high, &low); print("Decimal: "); print_64(high, low); print("\n"); print("Binary: "); print_bin64(high, low); print("\n"); }