Search code examples
assemblyx86

Sending 16 bit data in assembly x86 using serial ports


I am making a multiplayer game that works on 2 pcs and he communication is via two com ports, The ideas is I want to send and receive 16 bit data but the highest value is 8, so I neeed to divide them but it is causing me errors

recieve proc
        mov data_in, 0 ; variable to store data
        mov dx , 03F8H 
        in al , dx 
        mov bl, al  
        mov byte ptr data_in, bl
        mov cx,200 ; just delay
        kk:loop kk
        mov dx , 03F8H
        in al , dx 
        mov bh, al  
        mov byte ptr data_in + 1, bh
        ret
recieve endp
transmit proc
        mov bl , byte ptr data_out
        
        mov dx , 03F8H
        mov al, bl
        out dx , al
        mov cx, 200
        kk:loop kk
        mov bl , byte ptr data_out + 1
        mov dx , 03F8H
        mov al, bl
        out dx , al
        
        ret
transmit endp

Solution

  • When receive is called, how does it know that data is available? It should check the receive data available flag before reading the receive holding register.

    For the delay loop, instead of looping for a fixed number of iterations, it should read the receive data available flag to wait for the next byte to become available. 200 iterations is not nearly enough.

    receive proc
            call receive_byte
            mov byte ptr data_in, al
            call receive_byte
            mov byte ptr data_in + 1, al
            ret
    receive endp
    
    receive_byte proc
        receive_wait:   
            mov dx, 03F8H + 5
            in al, dx
            test al, 1
            jz receive_wait
    
            mov dx, 03F8H
            in al, dx 
            ret
    receive_byte endp
    

    Transmit is similar, but it checks the transmit holding register empty flag.

    transmit proc
            mov cl, byte ptr data_out
            call transmit_byte
            mov cl, byte ptr data_out + 1
            call transmit_byte
            ret
    transmit endp
    
    transmit_byte proc
        transmit_wait:
            mov dx, 03F8H + 5
            in al, dx
            test al, 1 << 5
            jz transmit_wait
    
            mov dx, 03f8H
            mov al, cl
            out dx, al
            ret
    transmit_byte endp