基于MASM32实现二进制转十六进制,求替代实现代码
Alternative MASM32 Binary to Hex Conversion Implementations
Approach 1: String-to-String Direct Conversion (With Validation)
This implementation directly processes the input binary string, converts it to a 32-bit integer, then formats that integer into a hexadecimal string. It includes input validation to ensure only '0' and '1' characters are entered.
Key Features:
- Input Validation: Rejects non-binary characters
- 32-bit Limit: Handles binary numbers up to 32 bits (matches DWORD size)
- Proper String Handling: Trims newline characters from input and null-terminates output
include \masm32\include\masm32rt.inc .data input_buf db 33 dup(0) ; Max 32 bits binary + null terminator output_buf db 9 dup(0) ; 8 hex digits + null terminator prompt db "Enter binary number (max 32 bits): ", 0 .code ; Converts binary string to DWORD, returns value in EAX. Sets CF on invalid input. bin_str_to_dword proc uses ebx ecx edx, str_ptr:dword mov eax, 0 mov ecx, str_ptr next_char: mov bl, byte ptr [ecx] test bl, bl jz done cmp bl, '0' jb invalid cmp bl, '1' ja invalid shl eax, 1 ; Multiply current value by 2 sub bl, '0' ; Convert char to numeric value (0 or 1) add eax, ebx ; Add to total inc ecx jmp next_char invalid: stc ; Set carry flag for error done: ret bin_str_to_dword endp ; Converts DWORD to hex string, stores result in provided buffer. dword_to_hex_str proc uses ebx ecx edx, val:dword, buf_ptr:dword mov eax, val mov ecx, 8 ; Process 8 hex digits (32 bits) mov ebx, buf_ptr add ebx, 7 ; Start writing from last character of buffer next_digit: mov edx, eax and edx, 0Fh ; Isolate lower 4 bits cmp dl, 9 jbe is_digit add dl, 7 ; Convert 10-15 to 'A'-'F' is_digit: add dl, '0' ; Convert to ASCII character mov byte ptr [ebx], dl dec ebx shr eax, 4 ; Shift right to next 4 bits loop next_digit mov byte ptr [ebx+8], 0 ; Null-terminate the string ret dword_to_hex_str endp start: ; Prompt user for input invoke StdOut, addr prompt invoke StdIn, addr input_buf, sizeof input_buf ; Trim newline character from input mov ecx, addr input_buf trim_newline: mov bl, byte ptr [ecx] cmp bl, 10 jne skip_trim mov byte ptr [ecx], 0 skip_trim: inc ecx test bl, bl jnz trim_newline ; Convert binary string to DWORD invoke bin_str_to_dword, addr input_buf jc invalid_input ; Jump if invalid characters found ; Convert DWORD to hex string and print invoke dword_to_hex_str, eax, addr output_buf invoke StdOut, addr output_buf invoke StdOut, chr$(13,10) jmp exit_prog invalid_input: invoke StdOut, chr$("Invalid input: only '0' and '1' are allowed!",13,10) exit_prog: invoke ExitProcess, 0 end start
Approach 2: Using MASM32 Library Helpers (Simpler Version)
If you prefer to leverage MASM32's built-in functions for string handling, here's a more concise version that converts the binary input to an integer first, then uses hex$ to format it as hexadecimal.
include \masm32\include\masm32rt.inc .data input_str db 33 dup(0) prompt db "Enter binary number: ", 0 error_msg db "Invalid binary input!", 13, 10, 0 .code ; Converts binary string to DWORD, returns EAX, CF on error parse_binary proc uses ebx ecx edx, str_ptr:dword mov eax, 0 mov ecx, str_ptr parse_loop: mov bl, byte ptr [ecx] test bl, bl jz parse_done cmp bl, '0' jb parse_error cmp bl, '1' ja parse_error shl eax, 1 add eax, ebx sub eax, '0' inc ecx jmp parse_loop parse_error: stc parse_done: ret parse_binary endp start: invoke StdOut, addr prompt invoke StdIn, addr input_str, sizeof input_str ; Remove newline mov ecx, addr input_str .while byte ptr [ecx] != 0 .if byte ptr [ecx] == 10 mov byte ptr [ecx], 0 .break .endif inc ecx .endw invoke parse_binary, addr input_str jc show_error ; Convert to hex string and print invoke StdOut, hex$(eax) invoke StdOut, chr$(13,10) jmp exit_app show_error: invoke StdOut, addr error_msg exit_app: exit end start
Notes on Both Implementations:
- Input Handling: Both trim the newline character added by
StdInto avoid processing invalid characters. - Validation: Both check for non-binary characters and display an error message if found.
- Hex Formatting: The first approach builds the hex string manually, while the second uses MASM32's
hex$macro for simplicity.
内容的提问来源于stack exchange,提问作者Thiện Hoàn
相关产品推荐
相关产品推荐

