看板 ASM 關於我們 聯絡資訊
TITLE Finite State Machine (Finite.asm) ; This program implements a finite state machine that ; accepts an integer with an optional leading sign. ; Last update: 06/01/2006 INCLUDE Irvine32.inc ENTER_KEY = 13 .data InvalidInputMsg BYTE "Invalid input",13,10,0 .code main PROC call Clrscr StateA: call Getnext ; read next char into AL cmp al,'+' ; leading + sign? je StateB ; go to State B cmp al,'-' ; leading - sign? je StateB ; go to State B call IsDigit ; ZF = 1 if AL contains a digit jz StateC ; go to State C call DisplayErrorMsg ; invalid input found jmp Quit StateB: call Getnext ; read next char into AL call IsDigit ; ZF = 1 if AL contains a digit jz StateC call DisplayErrorMsg ; invalid input found jmp Quit StateC: call Getnext ; read next char into AL call IsDigit ; ZF = 1 if AL contains a digit jz StateC cmp al,ENTER_KEY ; Enter key pressed? je Quit ; yes: quit call DisplayErrorMsg ; no: invalid input found jmp Quit Quit: call Crlf exit main ENDP ;----------------------------------------------- Getnext PROC ; ; Reads a character from standard input. ; Receives: nothing ; Returns: AL contains the character ;----------------------------------------------- call ReadChar ; input from keyboard call WriteChar ; echo on screen ret Getnext ENDP ;----------------------------------------------- DisplayErrorMsg PROC ; ; Displays an error message indicating that ; the input stream contains illegal input. ; Receives: nothing. ; Returns: nothing ;----------------------------------------------- push edx mov edx,OFFSET InvalidInputMsg call WriteString pop edx ret DisplayErrorMsg ENDP END main 以上的程式碼執行結果: 輸入+,-,或數字 然後再輸入數字最後按Enter鍵會正常結束 如果輸入其他的字原則會顯示"Invalid input"錯誤訊息 如果現在希望輸入小數點也不會產生錯誤訊息,可以正常結束 那需要在StateC的 cmp al,ENTER_KEY 之前加入什麼指令呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.143.27.20
loveflames:在STATEA加上「cmp al,'.'」 05/17 23:23
eentut:如果你的小數點是有意義的 如+1234和+12.34是不同的 05/19 07:26
eentut:那就不是只有增加L大的那行code可以解決的 05/19 07:28
eentut:還有你的程式可以寫的更減略點 call太多 jmp也太多 05/19 07:30