我正在编写一堆宏来处理MASM中的大数字,我发现自己需要从一系列数字转换为数字 . 基本上,为了绕过MASM大小限制,我一直把bignums作为字符串传递 . 所以,bignum调用看起来像:
MOV_BIG_NUM [eax], <1234567891011121314151617181920212223>
我有一个实现,可以处理(据我所知)在2的幂的基础中传递的字符串 . 也就是说,调用如下:
MOV_BIG_NUM [eax], <1001101111010101101011110101000011101b> ;Base 2
MOV_BIG_NUM [eax], <0123456710121314151617202122232425262o> ;Base 8
MOV_BIG_NUM [eax], <123456789ABCDEF101112131415161718191Ah> ;Base 16
将处理好 . 但是,方法很好(或者实际上)很好地转换为不是2的幂的基数 . 当然,最重要的是十进制,但是我已经使用2的幂来访问字节顺序,如有必要,移位数字,并按现有数字按位移位.2354588_ . 所以我的十六进制数字方法看起来像这样(这是一个高度简化的版本):
;foreach digit in list of digits
;eax contains the start of memory for the bignum
IF bit_offset EQ 0 ;Only move a new value in when the offset has just changed
mov ebx, dword ptr [eax + byte_offset] ;Get the current value at the dword's offset
ENDIF
digit = digit SHL bit_offset ;Shift the digit by a certain offset, so that we can store multiple digits in one byte
or ebx, digit
bit_offset = bit_offset + 4
IF bit_offset EQ ( 32 ) ;Number of bits in a dword
mov dword ptr [eax + byte_offset], ebx ;Move the dword back
byte_offset = byte_offset + 4 ;Number of bytes in a dword
bit_offset = 0;
ENDIF
;end foreach
但是,这种方法显然不适用于任意基础 . 我如何将十进制数字的顺序列表转换为十六进制数?
1 回答
您可以执行算术乘法10并添加带有十六进制字符串的数字 . 从那里你可以组成十进制到十六进制的字符串转换 .
C中的插图:
输出(ideone):