Assembly 我试图用汇编语言编写有限状态机,但我被卡住了
有很多关于有限状态机的问题,但都与我的问题无关 我需要5种方法Assembly 我试图用汇编语言编写有限状态机,但我被卡住了,assembly,x86,masm,state-machine,irvine32,Assembly,X86,Masm,State Machine,Irvine32,有很多关于有限状态机的问题,但都与我的问题无关 我需要5种方法 S0 S1 S2 S3 and read the input 我们从 S0 我们想打印州政府→ 0和输出0→ 读取输入 第一个在ebx中,第二个在eax中 . If (ebx ==0&&eax==0) Call S0 .elseif (ebx==1)&&(eax==1) Call S1 .else Call S2 .endif 完成整个程序 这是我的密码:
S0 S1 S2 S3 and read the input
我们从
S0
我们想打印州政府→ 0和输出0→
读取输入
第一个在ebx中,第二个在eax中
. If (ebx ==0&&eax==0)
Call S0
.elseif (ebx==1)&&(eax==1)
Call S1
.else
Call S2
.endif
完成整个程序
这是我的密码:
这里的问题是输入不起作用。如果我输入00,01,11->它都会给我相同的输出,这是不正确的。我想输入00并调用S0,输入11调用S1。我不知道为什么。
谁能猜出来
TITLE finite state machine
INCLUDE Irvine32.inc
E = 13
.data
invalidMsg BYTE 'Ivalid input',0
a DWORD ?
b DWORD ?
count dword ?
prompt1 byte 'Enter 0 or 1: ',0
prompt2 byte 'Enter 0 or 1: ',0
num1 byte 'The output is now 1 ',0
num2 byte 'The ouput is now 0',0
num3 byte 'The state is now 0 ',0
num4 byte 'The state is now 1 ',0
num5 byte 'The state is now 2 ',0
num6 byte 'The state is now 3 ',0
.code
main PROC
call clrscr
mov edx,offset prompt1
call writestring
call readint
mov a,ebx
mov edx,offset prompt2
call writestring
call readint
mov b,eax
.if(ebx ==0 && eax == 0)
call S0
.elseif(ebx == 1 && eax == 1)
call S1
.elseif(ebx == 0 && eax == 1)
call S2
.else
call S3
.endif
exit
main ENDP
S0 proc
mov edx,offset num3
call writestring
call crlf
mov edx,offset num2
call writestring
call readint
ret
S0 endp
S1 proc
mov edx,offset num4
call writestring
call crlf
mov edx,offset num2
call writestring
ret
S1 endp
S2 proc
mov edx,offset num5
call writestring
call crlf
mov edx,offset num1
call writestring
call crlf
ret
S2 endp
S3 proc
mov edx,offset num6
call writestring
call crlf
mov edx,offset num1
call writestring
ret
S3 endp
END main
我假设a和b是你们的州?因此,您将状态存储在那里,但在两者之间调用函数,因此在检查之前,我假设ebx已损坏
call writestring
call readint
mov a,ebx
mov edx,offset prompt2
call writestring
call readint
mov b,eax
因此,在这里,您需要至少将ebx恢复到,然后才能检查eax是否已经包含该值
mov a, ebx
但不确定a是否应该在eax中,因此您可能还需要交换它们
xchg eax, ebx
另外,我有点惊讶,您调用readint并将ebx移动到a,然后再次调用readint,但这次将eax移动到b。我认为readint在eax中返回值,对吗?您没有提供代码?那么,在第一次调用时,ebx中的值是多少?也许也应该如此
mov b, eax
更新
我尝试了所有它直接指向的部分,其他部分只打印S3,其他部分被忽略。如果我改变代码,它只读取一个寄存器。我不知道如何将用户的两个输入读入两个不同的寄存器。您尝试了什么?我已经告诉过你出了什么问题,所以修复它应该不难。我用完全相同的方法来做,但无论我使用什么输入,它都会跳到下一步。否则称为S3。它会忽略之前的输入,那么你的问题一定是在代码中没有显示出来。我想是的。尝试创建一个简单的示例,读取一个值,看看是否得到了预期的结果。如果这个方法有效的话,扩展到第二个值应该不难。我只能使用一个寄存器,但不能使用两个。readint不是我的过程,它是MASM定义的指令,类似于call WriteString call readint->读取用户输入。我认为readint只读取eax而不读取ebx。
mov edx,offset prompt1
call writestring
call readint
mov a,eax
mov edx,offset prompt2
call writestring
call readint
mov b,eax
mov eax, a
mov ebx, b
TITLE Finite State Machine (Finite.asm)
; This program implements a finite state machine that
; accepts an integer with an optional leading sign.
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 WaitMsg
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