我有一些錯誤與此代碼從二進制轉換爲十進制,十六進制值。該計劃顯示用戶菜單:相當於你想採取行動無法轉換從賓至12月/六角彙編語言
按數字:
- 如果您想爲十進制轉換爲二進制和十六進制。
- 如果您想將二進制數轉換爲十進制和十六進制數。
- 如果您想將十六進制數轉換爲十進制和二進制。
- 如果您想退出應用程序。
程序運行,除了選擇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
請正確格式化代碼。這很難閱讀。 – SachinGutte 2013-05-12 05:47:22
您是否嘗試過調試代碼以準確找出代碼的行爲不像預期的那樣? – Axarydax 2013-05-12 05:57:07
'mov ebx,binVal [cl]'......會不會組裝?對我來說,這看起來不是一個有效的有效地址。你可能需要的是'movzx ebx,byte ptr binval [edx]''movzx',因爲你只需要字符串中的一個字符(但最終需要在一個32位寄存器中) - '[edx] '因爲......好吧,有一部分你的評論與代碼不匹配......'shl ebx,cl'需要'cl',所以'cl'就是這樣。這使'edx'成爲您的字符串的索引... – 2013-05-12 09:56:36