0

2 進値から 10 進値および 16 進値に変換する際に、このコードにエラーがあります。プログラムはユーザーにこのメニューを表示します:

実行したい操作に対応する番号を押してください:

  1. 10 進数を 2 進数と 16 進数に変換したい場合。
  2. 2 進数を 10 進数と 16 進数に変換したい場合。
  3. 16 進数を 10 進数と 2 進数に変換したい場合。
  4. アプリケーションを終了したい場合。

プログラムは、選択肢 2 を除いて実行されます。

INCLUDE irvine32.inc

.data
num DWORD 5
binCounter DWORD ?
binVal BYTE 32 DUP(0)
temp BYTE 0
prompt BYTE "Welcome to the Binary/Decimal/Hex Converter", 0
invalid BYTE "Invalid value, please try again", 0
hexPrompt BYTE "Enter the hexadecial number and press enter key: ",0
DecPrompt BYTE "Enter the decimal number and press enter key: ",0
binPrompt BYTE "Enter the binary number and press enter key: ",0
numInHex BYTE "The number in hexadecimal format: ",0
numInDec BYTE "The number in decimal format: ",0
numInBin BYTE "The number in binary format: ",0
msg_InvalidInputs BYTE "Invalid binary value! Number in binary formate include only 0,1",0
msg_TooManyInputs BYTE "ERROR! The entered binary is more than 32 digit"
menu BYTE "Press number corresponding to the ation you would like to take.",0DH,0AH
    BYTE "1. If you would like to convert a decimal to binary and hexadecimal.",0DH,0AH
    BYTE "2. If you would like to convert a binary nuber to decimal and hexadecimal.",0DH,0AH
    BYTE "3. If you would like to convert a hexadecimal number to decimal and binary.",0DH,0AH
    BYTE "4. If you would like to quit the application.",0DH,0AH

.code
main PROC

    mov edx, OFFSET prompt
    call WriteString
another:
    call Crlf
    mov edx,OFFSET menu
    call WriteString
    call ReadDec
    cmp eax,4
    je stop
    cmp eax,3
    je fromHex
    cmp eax,2
    je fromBin
    cmp eax,1
    je fromDec
    mov edx,OFFSET invalid
    call WriteString
    ;call Crlf
    jmp another

fromHex:
    mov edx,OFFSET hexPrompt
    call WriteString
    call ReadHex
    call print_dec
    call print_bin
    jmp another

fromDec:
    mov edx,OFFSET decPrompt
    call WriteString
    call ReadDec
    call print_hex
    call print_bin
    jmp another

fromBin:
    mov edx,OFFSET binPrompt
    call WriteString
    call ReadBin        ;**Problem root is here**
    call print_dec
    call print_hex
    jmp another

stop:
    call Crlf
    exit
 main ENDP


;-----------------------------------------------
ReadBin PROC
;
;  THE PROBLEM IS IN THIS PROCEDURE
;-----------------------------------------------
        
        push eax
    push ebx
    push ecx
    push edx

    ; get input string
    mov edx, offset binVal
    mov ecx, sizeof binVal

    call ReadString
    cmp eax, 0 ; check zero input
    jz  ZeroInput
    cmp eax, 32 ; at max 32 input characters. 32 characters for 32 bits
    jg  TooManyInputs


    ; validate input string
    mov ebx, eax ; save input string length
    xor ecx, ecx ; set counter to zero
Validate:
    mov al, binVal[ecx] ; move character at ecx to eax
    sub eax, 30h ; subtract character '0' from input to get digit value
    cmp eax, 0 ; compare value with zero
    jl  InvalidInputs ; if less than zero signal error
    cmp eax, 1 ; compare value with one
    jg  InvalidInputs ; if greater than one signal error
    inc ecx ; increment ecx by 1
    cmp ebx, ecx ; compare length
    jg  Validate

; input string is valid, convert value
    mov dl,32;BYTE PTR ebx ; save string size in cl
    dec dl
    xor cl, cl
    ; cl have index of character inside binVal
    ; dl have index of bit inside eax

Convert:
    mov ebx, binVal[cl] ; move character at cl to ebx
    sub ebx, 30h ; subtract character '0' from input to get digit value
    shl ebx, cl  ; shift 0 or 1 by dl bits
    or  eax, ebx ; or ebx with eax
    dec dl ; get location of next bit
    inc cl ; get location of next character
    cmp dl, 0
    jnz Convert

    ; last character
    mov ebx, binVal[cl] ; move character at index 0 to ebx
    sub ebx, 30h ; subtract character '0' from input to get digit value
    or  eax, ebx
    jmp ProcEnd

ZeroInput:
    xor eax, eax
    jmp ProcEnd

TooManyInputs:
    mov edx,OFFSET msg_TooManyInputs
    call WriteString
    xor eax, eax
    jmp ProcEnd

InvalidInputs:
    mov edx,OFFSET msg_InvalidInputs
    call WriteString
    xor eax, eax

ProcEnd:
    pop edx
    pop ecx
    pop ebx
    pop eax
    ret
    ReadBin ENDP    

 ;-----------------------------------------------
print_hex PROC
;
;-----------------------------------------------
     mov      edx,OFFSET numInHex
     call  WriteString
     call WriteHex
     call Crlf
    
     ret
print_hex ENDP


;-----------------------------------------------
print_dec PROC
;
;-----------------------------------------------
     mov      edx,OFFSET numInDec
     call  WriteString
     call WriteDec
     call Crlf 
     ret
print_dec ENDP

;-----------------------------------------------
print_bin PROC
;
;-----------------------------------------------
     mov      edx,OFFSET numInBin
     call  WriteString
     call WriteBin
     call Crlf
     
     ret
print_bin ENDP

END main
4

1 に答える 1