# Author: Steven Carr
# Program Created: April 2023
# Assembly program that echos a given string input
echo.s:
    .text
    .globl echo
    # Start the code of the program
echo:
    # Push %ebx onto the stack and reset all registers
    push %ebx
    movl $0, %eax
    movl $0, %ebx
    movl $0, %ecx
    movl $0, %edx

    # Get the arguments of the function
    movw 8(%esp), %dx       # Pointer to base port in %dx
    movb 12(%esp), %cl      # The escape character to %cl
    addw $5, %dx            # Set %dx to the line status

    # Check to see if there's input
checkInp:
    inb (%dx), %al          # Get the data ready
    andb $0x01, %al
    jz checkInp             # If the data is not ready, then loop back to the start of loop

    # Read the input
input:
    # %edx set to the data address for i/o
    subw $5, %dx
    # Move rx to %al
    inb (%dx), %al
    # Save the input char into %bl
    movb %al, %bl
    # Set %dx to the line status
    addw $5, %dx

    # Test to see if we're going to escape
escape:
    # Test the input character against the escape character
    cmpb %al, %cl
    # If they are equal, jump to the end to escape the program
    je end


output:
    # Get input 
    inb (%dx), %al
    # Look at the input
    andb $0x20, %al
    # If its empty, go back to check it again
    jz checkInp

    # Put the input character into %al
    movb %bl, %al
    # Set %dx to the data address for i/o
    subw $5, %dx
    # Send the character to output
    outb %al, (%dx)

    # Set the %dx to line status
    addw $5, %dx
    # Reset %al and %bl
    movb $0, %al
    movb $0, %bl

    # Jump back to check the next input character
    jmp checkInp

    # End of the program
end:
    # Restore %ebx and return
    popl %ebx
    ret
    .end