Wie wandle ich eine Binärzahl (String) in eine Dezimalzahl (String) um?
Die Antwort besteht aus einer Kombination von Bin-zu-Int und Int-zu-Dez:
BITS 32 SEGMENT .data binz: db "0111 0010 0110 1011 0110 1000 0110 0010",0 ; = 1919641698d SEGMENT .bss dezz: resb 12 ; 11 Ziffern, 1 Abschlussnull SEGMENT .text Bin2Dword: ; Übergabe ESI: Zeiger auf ASCIIZ .Schleife: lodsb cmp al, '0' jb .Leerzeichen ; keine Binärziffer: Leerzeichen probieren cmp al, '1' ja .Ende ; keine Binärziffer: Ausstieg shr al, 1 rcl edx, 1 jmp .Schleife .Leerzeichen: cmp al, 32 ; Leerzeichen? je .Schleife ; ja: weitermachen .Ende: ret ; Ergebnis EDX: Dword Dword2Dez: ; Übergabe EAX: Dword; EDI Zeiger auf Ergebnis (12 Bytes) mov ebx, 10 ; Divisor (Zahlensystem 10) xor ecx, ecx ; ECX=0 (Anzahl der Ziffern) .Schleife_1: xor edx, edx div ebx ; EDX:EAX / EBX = EAX Rest EDX push dx ; LIFO add cl,1 ; ADD soll schneller sein als INC or eax, eax ; AX = 0? jnz .Schleife_1 ; nein: nochmal .Schleife_2: pop ax ; gepushte Ziffern zurückholen or al, 00110000b ; Umwandlung in ASCII stosb ; Nur AL nach [EDI] (EDI ist ein Zeiger auf den String) loop .Schleife_2 ; bis keine Ziffern mehr da sind mov byte [edi], 0 ; ASCIIZ-Abschlussnull ret ; Ergebnis in [EDI] Bin2Dez: mov esi, binz ; Vorgabe in [binz] call Bin2Dword mov eax, edx mov edi, dezz call Dword2Dez ret ; Ergebnis in [dezz]
Ralph 'rkhb' Bauer 09.08.2008