flat assembler
Message board for the users of flat assembler.

Index > OS Construction > UART Problem

Author
Thread Post new topic Reply to topic
chain_x



Joined: 19 Apr 2014
Posts: 3
chain_x 19 Apr 2014, 19:39
hello im having trouble on my code,
i want to send output to uart COM1
but it fails,
can anyone help me with my proplem?
here is the code:
Code:
define com1 0x3f8

bootentry: cli
           mov ax,0x07c0
           mov ds,ax
           mov ss,ax
           mov es,ax
           mov sp,0x0800
           sti
           call setup_uart
           cld
           mov ax,0xb800
           mov es,ax
           xor di,di
           mov si,fo
           mov cx,10
           rep movsw
           mov dx,com1
hang:      call tx_empty
           cmp al,1
           je x12
           jmp hang
x12:       mov al,"H"
           mov dx,com1
           out dx,al
           jmp hang

tx_empty:  mov dx,com1+5
           in al,dx
           and al,0x20
           test al,al
           jz azero
           mov al,1
           ret
azero:     mov al,0
           ret

setup_uart: mov dx,com1+1
            mov al,0
            out dx,al

            mov dx,com1+3
            mov al,0x80
            out dx,al

            mov dx,com1
            mov al,0x0c
            out dx,al

            mov dx,com1+1
            mov al,0x00
            out dx,al

            mov dx,com1+3
            mov al,0x03
            out dx,al

            mov dx,com1+2
            mov al,0xC7
            out dx,al

            mov dx,com1+4
            mov al,0x0B
            out dx,al
            xor ax,ax
            xor dx,dx
            ret
fo: db "H",0x0f,"e",0x0f,"y",0x0f," ",0x0f,"W",0x0f,"o",0x0f,"r",0x0f,"l",0x0f,"d",0x0f,"!",0x0f
buffer: times 510-($-$$) db 0
signature: dd 0xAA55    


thx in advance!
Post 19 Apr 2014, 19:39
View user's profile Send private message Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 20 Apr 2014, 01:06
I'm pretty sue you need an ORG to begin with. is there a specific UART your targetting? I recall there being more than one.
Post 20 Apr 2014, 01:06
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 20 Apr 2014, 04:33
BAiC wrote:
I'm pretty sue you need an ORG to begin with.
No, he doesn't. Implicit org 0 and proper segment registers' setup eliminates that need.

----8<----
chain_x,

Why do you think it fails? Modem's RD LED doesn't blink or what? UART has internal loopback mode, it might be useful for testing purposes.
Post 20 Apr 2014, 04:33
View user's profile Send private message Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 20 Apr 2014, 19:24
baldr: if you use far calls or the "int" instruction then the value of CS prior to entry will fuck with the code. you also need an ORG for absolute addressing of memory inside the code. using an ORG is the first step to initializing CS to a deterministic value.
Code:
ORG 0x7C00
jmp 0:$+5;initialize CS to zero, IP to the address of the next instruction.    
Post 20 Apr 2014, 19:24
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 20 Apr 2014, 20:13
BAiC,

Well, next instruction will be there if and only if cs==0 on entry (because your jump is essentially jmp 0:0x7C05, i.e. 5-bytes nop in this case).
Post 20 Apr 2014, 20:13
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 20 Apr 2014, 23:10
baldr
Quote:
Well, next instruction will be there if and only if cs==0 on entry

Not necessarily. I heard of some BIOSes initializing cs:ip to $7c0:0 instead of 0:$7c00. Thus it was generally a good practice to start the boot code with the suggested far jump.

_________________
Faith is a superposition of knowledge and fallacy
Post 20 Apr 2014, 23:10
View user's profile Send private message Reply with quote
chain_x



Joined: 19 Apr 2014
Posts: 3
chain_x 21 Apr 2014, 06:25
it loads from the SeaBIOS and works fine
the only problem is that the UART not do anything
Post 21 Apr 2014, 06:25
View user's profile Send private message Reply with quote
chain_x



Joined: 19 Apr 2014
Posts: 3
chain_x 21 Apr 2014, 13:08
yes it works now
i disabled DLAB

Code:
%define com1 0x3f8

bootentry: cli
           mov ax,0x07c0
           mov ds,ax
           mov ss,ax
           mov es,ax
           mov sp,0x0800
           sti
           call setup_uart
           cld
           mov ax,0xb800
           mov es,ax
           xor di,di
           mov si,fo
           mov ax,10
           push ax
           pop cx
           rep movsw
hang:      mov dx,com1+3
           in  al,dx
           push ax
           and al,127
           out dx,al ; disable DLAB
           mov dx,com1
           mov al,'K'
           out dx,al
           pop ax
           add dx,3
           out dx,al ; set LCR back
           jmp hang


setup_uart: mov dx,com1+1
            mov al,0
            out dx,al

            mov dx,com1+3
            mov al,0x80
            out dx,al

            mov dx,com1
            mov al,0x01
            out dx,al

            mov dx,com1+1
            mov al,0x00
            out dx,al

            mov dx,com1+3
            mov al,0x03
            out dx,al

            mov dx,com1+2
            mov al,0xC7
            out dx,al

            mov dx,com1+4
            mov al,0x0B
            out dx,al
            xor ax,ax
            xor dx,dx
            ret
fo: db "H",0x0f,"e",0x0f,"y",0x0f," ",0x0f,"W",0x0f,"o",0x0f,"r",0x0f,"l",0x0f,"d",0x0f,"!",0x0f
buffer: times 510-($-$$) db 0
signature: dd 0xAA55    


Very Happy
Post 21 Apr 2014, 13:08
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.