Reputation: 31800
In x86 assembly language, is there any efficient way to convert a byte to a string of binary digits (represented as a byte array of 0s and 1s)? As far as I know, there isn't any 'toString' function in x86 assembly, as in most high-level programming languages.
.stack 2048
.data
theString byte 0, 0, 0, 0, 0, 0, 0, 0 ;store eax as a binary string here.
ExitProcess proto, exitcode:dword
.code
start:
mov eax, 3;
;now I need to convert eax to a binary string somehow (i. e., a byte array of 0s and 1s)
invoke ExitProcess, 0
end start
Upvotes: 0
Views: 4777
Reputation: 62048
Was it that hard?:
.data
mystr db 33 dup(0)
.code
EaxToBinaryString:
mov ebx, offset mystr
mov ecx, 32
EaxToBinaryString1:
mov dl, '0' ; replace '0' with 0 if you don't want an ASCII string
rol eax, 1
adc dl, 0
mov byte ptr [ebx], dl
inc ebx
loop EaxToBinaryString1
ret
Upvotes: 1
Reputation: 18217
Using SSE intrinsics, one could code this like:
char in[2];
char string[16];
__m128i zeroes = _mm_set1_epi8('0');
__m128i ones = _mm_set1_epi8('1');
__m128i mask = _mm_set_epi8(
0x80, 0x40, 0x20, 0x10, 8, 4, 2, 1,
0x80, 0x40, 0x20, 0x10, 8, 4, 2, 1);
__m128i val = _mm_set_epi8(
in[1], in[1], in[1], in[1], in[1], in[1], in[1], in[1],
in[0], in[0], in[0], in[0], in[0], in[0], in[0], in[0]);
val = _mm_cmplt_epi8(val, _mm_and_si128(val, mask));
val = _mm_or_si128(_mm_and_si128(val, zeroes), _mm_andnot_si128(val, ones));
_mm_storeu_si128(string, val);
The code performs the following steps:
_mm_set1_epi...()
0xffff
or 0x0
if the bit was clear, or set.'0'
and '1'
characters using that mask, combine them.This gets away with shift-and-test sequences, but at the price of the _mm_set*()
which expands into sequences of a few SSE instructions each. It's still faster than 128 iterations of a bit-test loop.
Upvotes: 0