Text Mode Cursor: Difference between revisions

added assembly code for moving cursor
[unchecked revision][unchecked revision]
(added assembly code for moving cursor)
Line 12:
 
Note, this quick example assumes 80x25 screen mode. Also note that the base port (here assumed to be 0x3D4) should be read from the [[BIOS]] [[Memory Map (x86)#BIOS Data Area .28BDA.29|data area]].
 
===Source in C===
<source lang="c">
/* void update_cursor(int row, int col)
Line 29 ⟶ 31:
</source>
Note that the 2 parameters 'row' & 'col' passed to the function above start from zero, not from 1. And keep in mind that in/out to [[VGA Hardware]] is a slow operation. So using the hardware registers to remember of the current character location (row, col) is bad practice -- and updating position after each displayed character is poor practice (updating it only when a line/string is complete is wiser and hiding it until a user prompt is required is wisest)
 
===Source in assembly===
Since BIOS services can't be accessed in 64bit long mode, the following routine shows how to move cursor without BIOS in VGA text 80x25 (can be altered a bit to fit protected mode):
<source lang="asm">
; Set cursor position (text mode 80x25)
; @param BL The row on screen, starts from 0
; @param BH The column on screen, starts from 0
;=============================================================================
set_cursor: pushfq
push rax
push rbx
push rcx
push rdx
 
;unsigned short position = (row*80) + col;
;AX will contain 'position'
mov ax,bx
and ax,0ffh ;set AX to 'row'
mov cl,80
mul cl ;row*80
mov cx,bx
shr cx,8 ;set CX to 'col'
add ax,cx ;+ col
mov cx,ax ;store 'position' in CX
;cursor LOW port to vga INDEX register
mov al,0fh
mov dx,3d4h ;VGA port 3D4h
out dx,al
mov ax,cx ;restore 'postion' back to AX
mov dx,3d5h ;VGA port 3D5h
out dx,al ;send to VGA hardware
;cursor HIGH port to vga INDEX register
mov al,0eh
mov dx,3d4h ;VGA port 3D4h
out dx,al
mov ax,cx ;restore 'position' back to AX
shr ax,8 ;get high byte in 'position'
mov dx,3d5h ;VGA port 3D5h
out dx,al ;send to VGA hardware
 
pop rdx
pop rcx
pop rbx
pop rax
popfq
ret
</source>
 
==See Also==
25

edits