如何修复十六进制转二进制汇编程序出现反向转换的问题?
First, let's break down what's going wrong here — your current code is built to read binary digits (0/1), pack them into a single byte, then output that byte as a hexadecimal character. No wonder it's backwards! Let's fix this step by step to make it convert hex input to binary output, plus track those 1's and 0's counts properly.
Key Modifications Needed
1. Update Input Logic to Accept Hexadecimal Characters
Your original input only allows 0/1. We need to expand this to accept valid hex characters: 0-9, A-F (uppercase), and a-f (lowercase). We'll also handle up to 2 hex digits since a byte is 8 bits, and 2 hex digits equal exactly 8 bits.
2. Rewrite Output to Print Binary Digits
Instead of converting the byte to a hex character, we'll loop through each of the 8 bits in the byte, print '0' or '1' for each bit, and count how many of each we encounter.
3. Implement Proper Count Tracking
Your code declares countone and countzero but never uses them. We'll initialize these to 0, increment them as we print each bit, then convert the counts to ASCII for readable output.
Full Fixed Code
.model small .stack .data msg1 db "Enter hexadecimal number (1-2 digits):$" msg2 db "The binary value is:$" msg3 db "The number of 1's is:$" msg4 db "The number of 0's is:$" countone db ? countzero db ? .code main proc mov ax,@data mov ds,ax clear: ; Print newline mov ah,2 mov dl,0dh int 21h mov dl,0ah int 21h ; Prompt for hex input mov ah,9 lea dx,msg1 int 21h xor bh,bh ; Initialize bh to 0 (will hold our 8-bit value) mov cl,0 ; Track number of hex digits entered (max 2) input: mov ah,1 int 21h mov ch,al cmp ch,0dh ; Check if user pressed enter je print ; Validate and convert hex character cmp ch, '0' jl exit ; Invalid character, exit cmp ch, '9' jle convert_digit cmp ch, 'A' jl check_lower cmp ch, 'F' jle convert_upper check_lower: cmp ch, 'a' jl exit cmp ch, 'f' jg exit convert_lower: sub ch, 'a' - 10 ; Convert a-f to 10-15 jmp process_hex convert_upper: sub ch, 'A' - 10 ; Convert A-F to 10-15 jmp process_hex convert_digit: and ch, 0Fh ; Convert 0-9 to 0-9 (same as sub ch, '0') process_hex: shl bh, 4 ; Shift existing bits left to make space for new 4 bits or bh, ch ; Add the new 4-bit value to bh inc cl cmp cl,2 ; Stop after 2 hex digits jl input print: ; Print newline mov ah,2 mov dl,0dh int 21h mov dl,0ah int 21h ; Print binary value prompt mov ah,9 lea dx,msg2 int 21h ; Initialize counts to 0 mov countone, 0 mov countzero, 0 ; Print 8 binary bits mov cx,8 print_bit: shl bh,1 ; Shift left, carry flag holds the highest bit jc print_one ; If carry is set, it's a 1 print_zero: mov dl,'0' inc countzero jmp print_char print_one: mov dl,'1' inc countone print_char: mov ah,2 int 21h loop print_bit ; Print newline mov ah,2 mov dl,0dh int 21h mov dl,0ah int 21h ; Print number of 1's mov ah,9 lea dx,msg3 int 21h mov al,countone add al,'0' ; Convert count to ASCII mov dl,al mov ah,2 int 21h ; Print newline mov ah,2 mov dl,0dh int 21h mov dl,0ah int 21h ; Print number of 0's mov ah,9 lea dx,msg4 int 21h mov al,countzero add al,'0' mov dl,al mov ah,2 int 21h jmp clear exit: ; Print newline before exiting mov ah,2 mov dl,0dh int 21h mov dl,0ah int 21h mov ah,4ch int 21h main endp END main
What Changed?
- Input Handling: Now accepts 1-2 hex digits (0-9, A-F, a-f) and packs them into an 8-bit value in
bh - Binary Output: Loops through each bit of
bh, prints '0'/'1', and increments the respective count - Count Tracking: Properly initializes and updates
countone/countzero, then converts the counts to ASCII for readable output - User Feedback: Updated the input prompt to clarify we accept 1-2 hex digits
内容的提问来源于stack exchange,提问作者eljonc

