Search code examples
assemblyx86uart

Programming a UART directly in assembler


I'm attempting to write a assembler function to read and echo characters for the following C callable function

void echo(int comport, unsigned char esc_char) 

Basically it prompts you for an ESC character then after the user types one it allows you to keep typing and terminates the program if that character is typed. My implementation allows the user to assign the ESC character but stops after typing the first letter for anything after that, which I believe may be some sort of infinite loop. How do I go about resolving this?

Ideally the program would run like this

Type escape character and enter
d
abc(d)
Escape character typed - exiting.

But what's happening so far is

Type escape character and enter
p
a
(Program Freezes)
.text
.globl _echo
_echo:
    pushl %ebp
    movl %esp, %ebp
    subl $8, %esp
    movb 12(%ebp), %bl  
    movl 8(%ebp), %edx  

    xorl %ecx, %ecx
    movb $0xfc, %dl
    inb (%dx), %al
    orb $0x03, %al
    outb %al, (%dx)
    movb $0xfe, %dl  
loop1:
    inb (%dx), %al  
    andb $0xb0, %al  
    xorb $0xb0, %al  
    jnz loop1
loop2:
    movl $0xfd, %dl 
    inb (%dx), %al  
    andb $0x01, %al 
    jz loop2        
    movl $0xf8, %dl 
    inb (%dx), %al  
    movb %al, %al   
    cmpb %cl, %bl
    je return
    movb $0xfd, %dl 
type:
    inb (%dx), %al  
    andb $0x20, %al
    jz loop2        
    movzbl %dl, %eax    
    movl $0xf8, %edx    
    outb %al, (%dx) 
    jmp loop2
return:
    mov %ebp, %esp
    popl %ebp
    ret
    .end

Solution

  • After reading the character from the port, you have

        movb %al, %al
    

    It should be

        movb %al, %cl
    

    Then, after checking THR_EMPTY, you have

        movzbl %dl, %eax
    

    It should be

        movb %cl, %al