13 static CERR cerr_word[] = {
14 { 114, "not integer" },
16 { 116, "out of hex range" },
22 WORD n2word(const char *str)
24 assert(isdigit(*str) || *str == '-');
29 n = strtol(str, &check, 10);
31 setcerr(114, str); /* not integer */
34 /* nが-32768から32767の範囲にないときは、その下位16ビットを格納 */
35 if(n < -32768 || n > 32767) {
44 WORD h2word(const char *str)
51 if(*str == '-' || strlen(str) > 4) {
52 setcerr(116, str-1); /* out of hex range */
56 word = (WORD)strtol(str, &check, 16);
58 setcerr(115, str-1); /* not hex */
65 * 10進数または16進数の文字列をWORD値に変換
67 WORD nh2word(const char *str)
69 assert(sizeof(WORD)*8 == 16); /* WORD型のサイズが16ビットであることを確認 */
70 addcerrlist(ARRAYSIZE(cerr_word), cerr_word); /* エラーの設定 */
74 if(!isdigit(*str) && *str != '-' && *str != '#') {
75 setcerr(114, str); /* not integer */
89 char *word2n(WORD word)
92 MAXLEN = 6, /* WORD値を10進数で表したときの最大桁数 */
94 char *p = malloc_chk(MAXLEN, "word2n.p"), *digit = malloc_chk(MAXLEN, "word2n.digit");
98 *(p + i++) = word % 10 + '0';
99 } while((word /= 10) > 0);
100 for(j = 0; j < i; j++) {
101 *(digit + j) = *(p + (i - 1) - j);
103 *(digit + j + 1) = '\0';
104 free_chk(p, "word2n.p");
111 char *word2bit(const WORD word)
114 MAXLEN = 16, /* WORD値を2進数で表したときの最大桁数 */
119 bit = p = malloc_chk(MAXLEN + 1, "word2bit.bit");
121 *p++ = (word & mask) ? '1' : '0';
122 } while((mask >>= 1) > 0);
130 void print_dumpword(WORD word, bool logicalmode)
132 if(logicalmode == true) {
133 fprintf(stdout, "%6d", word);
135 fprintf(stdout, "%6d", (signed short)word);
137 fprintf(stdout, " = #%04X = %s", word, word2bit(word));
138 /* 「文字の組」の符号表に記載された文字と、改行(CR)/タブを表示 */
139 if(word >= 0x20 && word <= 0x7E) {
140 fprintf(stdout, " = \'%c\'", word);
141 } else if(word == 0xA) {
142 fprintf(stdout, " = \'\\n\'");
143 } else if(word == '\t') {
144 fprintf(stdout, " = \'\\t\'");
146 fprintf(stdout, "\n");