我正在编写一堆宏来处理 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 的幂的方法是按顺序访问字节,必要时移动数字,or
然后用现有数字按位。所以我的十六进制数字方法看起来像这样(这是一个高度简化的版本):
;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
但是,这种方法显然不适用于任意基础。我将如何将十进制数字的顺序列表转换为十六进制数字?