2013-05-12 59 views
0

我有一些錯誤與此代碼從二進制轉換爲十進制,十六進制值。該計劃顯示用戶菜單:相當於你想採取行動無法轉換從賓至12月/六角彙編語言

按數字:

  1. 如果您想爲十進制轉換爲二進制和十六進制。
  2. 如果您想將二進制數轉換爲十進制和十六進制數。
  3. 如果您想將十六進制數轉換爲十進制和二進制。
  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 
+0

請正確格式化代碼。這很難閱讀。 – SachinGutte 2013-05-12 05:47:22

+0

您是否嘗試過調試代碼以準確找出代碼的行爲不像預期的那樣? – Axarydax 2013-05-12 05:57:07

+0

'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

回答

0

這裏有一些東西,馬上伸出:

  • menu字符串不是空終止。

  • ValidateConvert循環可能已合併爲一個。我不明白你爲什麼需要兩次穿過繩子。

  • 在您的Convert循環中,您從左到右讀取字符串,即從索引0開始(因爲您使用CL作爲索引)。並且您將CL:th數字CL位移到左邊(換句話說,您將最右邊的位設置爲最重要的位)。所以,如果你得到"110"你最終與1<<0 | 1<<1 | 0<<2 == 3字符串,而不是6,本來正確的答案。

  • 您將Convert循環的循環計數器(DL)設置爲32,即使您的字符串可能沒有32位數字。爲什麼不把它設置爲BL(即字符串長度的LSB)?

  • 您不清除ValidateConvert循環之間的EAX。這個錯誤具有這樣的; last character部分是不必要的,因爲EAX將包含基於在所述輸入字符串中最右邊的位在進入Convert循環的值爲0或1,效果。