Search code examples
assemblyx86masmstate-machineirvine32

I am trying to program finite state machine in assembly language but i am stuck


There are many finite state machine asked questions but all are not related to my problem.

I need 5 methods

S0     S1   S2   S3   and read the input

We start in

S0

We want to print the state → 0 and the output 0→

Read input First is in ebx and the second will be in eax

  . If (ebx ==0&&eax==0)
  Call S0
  .elseif (ebx==1)&&(eax==1)
 Call S1
  .else
 Call S2
.endif

Do the complete program

here is my code: The problem here is input is not working. If i input 00,01,11 -> it all give me same output which is not right. I want to enter 00 and call S0, enter 11 call S1. It is not doing that i don't why. Can anyone figure out.

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


Solution

  • I assume that a and b are your states? So you store the state there but you call functions in between, so I would assume that ebx is trashed before you check it.

    call writestring
    call readint
    mov a,ebx
    
    mov edx,offset prompt2
    call writestring
    call readint
    mov b,eax
    

    So here you would need to restore at least ebx to before you can do the check (eax already contains the value).

    mov  a, ebx
    

    Not sure if a is supposed to be in eax though, so you may have to exchange them as well.

     xchg eax, ebx
    

    Also I'm a bit surprised that you call readint and move ebx to a and right after you call readint again, but this time move eax to b. I would think that readint returns the value in eax, right (you didn't provide the code)? So what value would be in ebx on the first call? It probably should be also

    mov b, eax
    

    update

    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