Page 1 of 1
Cursor just Dissapears
Posted: Wed Jul 31, 2002 5:12 am
by srg
Hi, this is my first post in this forum
I am having a problem with moving the cursor in text mode in asm (please note, I am still new to asm)
I have tried to port the C based example in the OS FAQ that is on this site but rather than moving the cursor, it just disapears!!
Here is the code that I'm using, it is part of a small program to teach me text mode programming with out using BIOS services, I have also included the relevant defines and variables (I am using NASM):
%define crtc_reg_select 0x03D4
%define crtc_addr 0x03D5
mov ax, [row]
mov dx, 80 ; 80 characters per line
mul dx
add ax, [col]
mov [position], ax
mov ax, 0x0F
mov dx, crtc_reg_select
out dx, al
mov ax, [position]
and ax, 0xFF
mov dx, crtc_addr
out dx, al
mov ax, 0x0E
mov dx, crtc_reg_select
out dx, al
mov ax, [position]
shr ax, 8
and ax, 0xFF
mov dx, crtc_addr
out dx, al
position dw 0000
row dw 0010
col dw 0010
Why is this just making the cursor disapear, it should be on the 10th row and the 10th column, what am I doing wrong?
Thanks
Steven Graham
Re:Cursor just Dissapears
Posted: Wed Jul 31, 2002 8:06 am
by crazybuddha
See if this helps. I took the liberty of capitalizing your constants. If this *does* work, then I'd say there's just an error in your code (I didn't bother checking). If it doesn't, then come back and maybe we'll take a closer look at the shifting and anding.
xor bx, bx
mov bl, [col] ; get x offset
xor ax, ax
mov al, [row]
mov dx, 80
mul dx ; calculate y offset
add bx, ax ; calculate index
; select to write low byte of index
mov al, 0xf
mov dx, CRTC_REG_SELECT
out dx, al
; write it
mov al, bl
mov dx, CRTC_ADDR
out dx, al
; select to write high byte of index
mov al, 0xe
mov dx, CRTC_REG_SELECT
out dx, al
; write it
mov al, bh
mov dx, CRTC_ADDR
out dx, al
Re:Cursor just Dissapears
Posted: Wed Jul 31, 2002 9:29 am
by srg
No change i'm afraid, the cursor still just disapears.
Re:Cursor just Dissapears
Posted: Wed Jul 31, 2002 10:04 am
by crazybuddha
Well, that livens things up a bit. I just tried this, and it works as expected.
You might want to provide some of the other code in your test and we'll have a look.
Re:Cursor just Dissapears
Posted: Wed Jul 31, 2002 10:46 am
by srg
Right, here is my whole program (simple Hello World stuff)
One thing I must say though, this program is loaded by a floppydisk bootsector program, it is not a DOS program although would be easy to make it one.
; Program to display a letter on the screen
; Eventually building up to displying a sentance
; This all writing directly to screen memory
; Defines##############################################
%define CodeSeg 0x7C00
%define VGATextModeSeg 0xB800
%define crtc_reg_select 0x03D4 ; 03B4h for monochrome
%define crtc_addr 0x03D5
; Code Starts Here###########################################
;Setup Segments
mov ax, VGATextModeSeg
mov es, ax
;Write Hello World to the Screen
mov byte [es:0000], 'H'
mov byte [es:0002], 'E'
mov byte [es:0004], 'L'
mov byte [es:0006], 'L'
mov byte [es:0008], 'O'
mov byte [es:0010], ' '
mov byte [es:0012], 'W'
mov byte [es:0014], 'O'
mov byte [es:0016], 'R'
mov byte [es:0018], 'L'
mov byte [es:0020], 'D'
;Blank the rest of the screen
mov si, 0022
mov cx, 1989
ToHere: mov byte [es:si], 0x0000
add si, 2
loop ToHere
;Move the cursor
xor bx, bx
mov bl, [col]
xor ax, ax
mov ax, [row]
mov dx, 80 ; 80 characters per line
mul dx
add bx, ax
mov ax, 0x0F
mov dx, crtc_reg_select
out dx, al
mov al, bl
mov dx, crtc_addr
out dx, al
mov al, 0x0E
mov dx, crtc_reg_select
out dx, al
mov al, bh
mov dx, crtc_addr
out dx, al
; Hang Computer
Jmp $
; Variables###############################################
position dw 0000
row dw 0010
col dw 0010
Re:Cursor just Dissapears
Posted: Wed Jul 31, 2002 12:43 pm
by crazybuddha
Try this:
[ORG 0x7c00]
; Defines##############################################
%define VGATextModeSeg 0xB800
%define CRTC_REG_SELECT 0x03D4 ; 03B4h for monochrome
%define CRTC_ADDR 0x03D5
; Code Starts Here###########################################
xor ax, ax
mov ds, ax
mov ss, ax
;Setup Segments
mov ax, VGATextModeSeg
mov es, ax
;Write Hello World to the Screen
mov byte [es:0000], 'H'
mov byte [es:0002], 'E'
mov byte [es:0004], 'L'
mov byte [es:0006], 'L'
mov byte [es:0008], 'O'
mov byte [es:0010], ' '
mov byte [es:0012], 'W'
mov byte [es:0014], 'O'
mov byte [es:0016], 'R'
mov byte [es:0018], 'L'
mov byte [es:0020], 'D'
;Blank the rest of the screen
mov si, 0022
mov cx, 1989
ToHere: mov byte [es:si], 0x0000
add si, 2
loop ToHere
;Move the cursor
xor bx, bx
mov bl, [col] ; get x offset
xor ax, ax
mov al, [row]
mov dx, 80
mul dx ; calculate y offset
add bx, ax ; calculate index
; select to write low byte of index
mov al, 0xf
mov dx, CRTC_REG_SELECT
out dx, al
; write it
mov al, bl
mov dx, CRTC_ADDR
out dx, al
; select to write high byte of index
mov al, 0xe
mov dx, CRTC_REG_SELECT
out dx, al
; write it
mov al, bh
mov dx, CRTC_ADDR
out dx, al
; Hang Computer
Jmp $
; Variables###############################################
row db 0
col db 10
times 510-($-$$) db 0 ; fill sector w/ 0's
dw 0xAA55 ; req'd by some BIOSes
Re:Cursor just Dissapears
Posted: Wed Jul 31, 2002 1:01 pm
by srg
err....I think you misunderstood me a bit! :-[
This program is not a boot sector program but it is loaded into RAM and then run by a boot sector program.
Sorry!
Steven Graham
Re:Cursor just Dissapears
Posted: Wed Jul 31, 2002 1:21 pm
by crazybuddha
Even so, make sure that your offsets (ie, the variables 'row' and 'col'), relative to DS, are correct. Otherwise, you are feeding garbage to the ports.
If you run the code I gave as a bootsector, the cursor works correctly, so that at least tells you that the problem isn't in there. Right?
Re:Cursor just Dissapears
Posted: Wed Jul 31, 2002 2:25 pm
by srg
;D Thank you, thank you, thank you
It works! I should have seen that!
Do not worry, I think this was just my inexperience showing!
Again thank you for your help. I definately think I will come to this forum again in the future, on comp.lang.asm.x86 you have to wait 4 hours just to get your message on the board!
BTW, Where can I find out exactly about best practices when porting C/C++ code to asm?
Thanks Again
Steven Graham