【问题标题】:I am trying to program finite state machine in assembly language but i am stuck我正在尝试用汇编语言编写有限状态机,但我被卡住了
【发布时间】:2013-12-24 16:27:51
【问题描述】:

有很多有限状态机问的问题,但都与我的问题无关。

我需要 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

做完整的程序

这是我的代码: 这里的问题是输入不起作用。如果我输入 00,01,11 -> 它都给我相同的输出,这是不正确的。我想输入00调用S0,输入11调用S1。它没有这样做,我不知道为什么。 有谁能看出来。

TITLE 有限状态机 包括 Irvine32.inc E = 13 。数据 invalidMsg BYTE '无效输入',0 一个双字? b 双字? 数双字? prompt1 byte '输入0或1:',0 prompt2 byte '输入0或1:',0 num1 byte '现在的输出是 1 ',0 num2 byte '输出现在是 0',0 num3 byte '状态现在是 0 ',0 num4 byte '状态现在是 1 ',0 num5 byte '状态现在是 2 ',0 num6 byte '状态现在是 3 ',0 。代码 主进程 调用 clrscr mov edx,offset prompt1 调用 writestring 调用 readint 移动 a,ebx mov edx,offset prompt2 调用 writestring 调用 readint mov b,eax .if(ebx ==0 && eax == 0) 调用 S0 .elseif(ebx == 1 && eax == 1) 调用 S1 .elseif(ebx == 0 && eax == 1) 呼叫 S2 。别的 调用 S3 。万一 出口 主要ENDP S0 过程 mov edx,offset num3 调用 writestring 调用 crlf mov edx,offset num2 调用 writestring 调用 readint ret S0 端 S1 进程 mov edx,offset num4 调用 writestring 调用 crlf mov edx,offset num2 调用 writestring ret S1 端 S2 进程 mov edx,offset num5 调用 writestring 调用 crlf mov edx,偏移量 num1 调用 writestring 调用 crlf ret S2 端 S3 进程 mov edx,偏移量 num6 调用 writestring 调用 crlf mov edx,偏移量 num1 调用 writestring ret S3 终端 结束主

【问题讨论】:

    标签: assembly x86 masm state-machine irvine32


    【解决方案1】:

    我假设ab 是你的州?因此,您将状态存储在那里,但您在两者之间调用函数,所以我假设 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
    

    更新

    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
    

    【讨论】:

    • 我尝试了所有这些,它直接进入 .else 部分,它只打印 S3 而所有其他部分都被忽略。如果我更改代码,它只会读取一个寄存器。我不知道如何将用户的两个输入读入两个不同的寄存器。
    • 你尝试了什么?我已经告诉你出了什么问题,所以应该不难修复它。
    • 我以完全相同的方式进行操作,但无论我使用什么输入,它都会一直跳到调用 S3 的 .else。它忽略它之前的输入。
    • 那你的问题一定出在你没有显示的代码上。 readint 我假设。尝试创建一个读取单个值的简短示例,看看是否获得了预期的结果。如果这可行,将其扩展到第二个值应该不难。
    • 我只能使用一个寄存器,但不能使用两个。 readint 不是我的程序,它是 MASM 定义的 DIRECTIVE,类似于 call writetstring call readint -> 从用户读取输入。我认为 readint 只读取 eax 而不是 ebx。
    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 2022-11-14
      • 2013-12-23
      • 1970-01-01
      相关资源
      最近更新 更多