x86将ASCII字符转换为数字

时间:2012-02-25 02:54:10

标签: assembly x86 ascii masm irvine32

我正忙着完成家庭作业,并且会喜欢一些指导。分配要求您在任何基础中转换ASCII字符串并输出到任何基础。当我调用AsciiToDigit过程(嵌套在ReadInteger过程中)时程序挂起,我无法弄清楚原因。调试器没有任何帮助,因为它没有做任何事情,只是在它到达程序的那一部分时挂起。

我相当肯定我需要先将字符转换为数字才能正确地执行此操作,但也许我错过了一些东西。感谢任何人提供的任何帮助。

    TITLE MASM Template                     (main.asm)

; Description:
; 
; Revision date:

INCLUDE Irvine32.inc
.data
basePrompt BYTE "What base (2-16 or 0 to quit): ",0
numPrompt BYTE "Number please: ",0
invalid BYTE "Invalid base, please try again.",0
base2 BYTE "Base 2: ",0
base8 BYTE "Base 8: ",0
base10 BYTE "Base 10: ",0
base16 BYTE "Base 16: ",0
base DWORD 0


.code
main PROC
        call Clrscr
    State0:                             ; initial state to accept base number
        mov edx, OFFSET basePrompt
        call ReadBase
        cmp al, '0'                     ; check if 0 entered    
        je ProgEnd                      ; jump to ProgEnd if 0 entered
        mov base, ebx
        mov edx, OFFSET numPrompt
        call WriteString
        call ReadInteger

        mov ebx, 2
        mov edx, OFFSET base2
        call WriteString
        call WriteInteger
        mov ebx, 8
        mov edx, OFFSET base8
        call WriteString
        call WriteInteger
        mov ebx, 10
        mov edx, OFFSET base10
        call WriteString        
        call WriteInteger
        mov ebx, 16
        mov edx, OFFSET base16
        call WriteString
        call WriteInteger


        call Crlf
        jmp State0                      ; jump back to beginning of program

    ProgEnd:                            ; jump point to end of programt

    exit
main ENDP

;-----------------------------------------------------
ReadInteger PROC
;
; ReadInteger is passed one argument in bl representing the base of the number to be input. 
; Receives: bl register
; Returns:  EAX
;-----------------------------------------------------
nextChar:
     call ReadChar          ; Get the next keypress
     call WriteChar         ; repeat keypress
     call AsciiToDigit      
     shl   ebx,1            ; shift to make room for new bit
     or    ebx,eax          ; set the bit to eax
     cmp al, 13             ; check for enter key
     jne   nextChar
     ret
ReadInteger ENDP

;-----------------------------------------------------
WriteInteger PROC
;
; Will display a value in a specified base
; Receives: EAX register (integer), bl (base)
; Returns:  nothing
;-----------------------------------------------------

     mov   ecx, 0         ;count the digits
nextDigit:
     mov   edx, 0         ;prepare unsigned for divide
     div   ebx
     push  edx            ;remainder will be in dl
     inc   ecx            ;count it!
     cmp   eax,0          ;done when eax becomes 0
     jne   nextDigit

;now the digits are on the stack
;pop them off and convert to ASCII for output
outDigit: 
     pop   eax              ;digits come off left to right
     add    eax, '30'       ;add 0011 to front to get ASCII
     call  WriteChar
     loop  outDigit

     call Crlf
     ret



    ret
WriteInteger ENDP

;-----------------------------------------------------
ReadBase PROC
;
; Prompts the user for input and stores input into EAX.
; Receives: EDX register
; Returns:  EAX
;-----------------------------------------------------
    Call WriteString

    xor ebx, ebx                ; clear ebx
        call ReadChar
        call WriteChar
        cmp al, '0'
        je Done
        cmp al, 13              ; look for return carriage, jump to end
        je Done
        mov ebx, eax
        shl ebx, 1              ; shift ebx left one
        call ReadChar
        call WriteChar
        or ebx, eax

    Done:
        call Crlf
    ret
ReadBase ENDP

;-----------------------------------------------------
AsciiToDigit PROC
;
; This procedure receives the ASCII code of a digit and returns the numerical digit value.
; Receives: EAX register
; Returns:  EAX
;-----------------------------------------------------
    cmp eax, 61h
    jb Upper
    sub eax,61h
    jmp done

    Upper:
        cmp eax, 41h
        jb Digit
        sub eax, 41h
        jmp done

    Digit:
        sub eax,30h

    done:
        ret
AsciiToDigit ENDP

;-----------------------------------------------------
DigitToAscii PROC
;
; This procedure receives digit and returns Ascii value
; Receives: EAX register
; Returns:  EAX
;-----------------------------------------------------
    add eax, 30h

    ret
DigitToAscii ENDP
END main

1 个答案:

答案 0 :(得分:0)

到目前为止,我可以看到一些事情。

在第一部分中,当您尝试获取基数时,如果输入10,则代码退出。我认为您需要查看从用户输入的每个字符。

接下来,您正在挂起,因为当您正在读取该特定基数的整数时,程序似乎仍在继续获取数字。在这一点上代码:

;-----------------------------------------------------
; AsciiToDigit PROC
;
; This procedure receives the ASCII code of a digit and returns 
; the numerical digit value.
; Receives: EAX register
; Returns:  EAX
;-----------------------------------------------------
cmp eax, 61h
jb Upper
sub eax,61h
jmp done

当您从EAX中减去61h时,如果此人点击了“Enter”,则该值消失。意味着13h将用61h减去,AL保持在ACh。所有这一切都意味着只要代码连续减去61h,你就永远不会看到13h的输入。

我认为你可能想要更多的cmp,并且在这方面真的考虑更多的代码。另外,要小心你推动和弹出堆栈。观察寄存器,看看它们在执行这些功能时的作用。

只需要看一些事情并希望它有所帮助。