[問題] 有限狀態機FSM的問題

看板ASM (組合語言)作者 (56好棒)時間15年前 (2010/05/17 22:18), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/1
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

05/17 23:23, , 1F
在STATEA加上「cmp al,'.'」
05/17 23:23, 1F

05/19 07:26, , 2F
如果你的小數點是有意義的 如+1234和+12.34是不同的
05/19 07:26, 2F

05/19 07:28, , 3F
那就不是只有增加L大的那行code可以解決的
05/19 07:28, 3F

05/19 07:30, , 4F
還有你的程式可以寫的更減略點 call太多 jmp也太多
05/19 07:30, 4F
文章代碼(AID): #1ByL0yuI (ASM)
文章代碼(AID): #1ByL0yuI (ASM)