OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 5:12 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Converting screen position into byte
PostPosted: Sat May 04, 2019 4:45 pm 
Offline

Joined: Sat May 04, 2019 4:31 pm
Posts: 12
Hello,

So, the goal of the operation was to convert screen position(1st row, 2nd line) to an offset so we can use it to write a character into the screen. I can print on the screen without using the screen position(using directly the video address memory), but when I use screen position, nothing is written, but the cursor position updates just fine. My guess would be that the address to where I write the char is wrong, but I can't find where is the mistake.

Code:
[bits 32]
VIDEO_MEMORY equ 0xb8000
WHITE_ON_BLACK equ 0x0f
COLS equ 80
LINES equ 25

; screen coordinate offsets
POS_X : db 0
POS_Y : db 0

%include "screen/cursor.asm"

print_string_pm:
    pusha
    mov edx, VIDEO_MEMORY
   call get_current_position

print_string_pm_loop:
    mov al, [ebx]
    mov ah, WHITE_ON_BLACK

    cmp al, 0
    je print_string_pm_done

    mov [edx], ax

    inc ebx ; next char
   ;add edx, 2
   call next_char
   call get_current_position

    jmp print_string_pm_loop

print_string_pm_done:
   mov bh, byte [POS_Y]
   mov bl, byte [POS_X]
   call move_cursor

    popa
    ret

next_char:
   inc byte [POS_X]
   cmp byte [POS_X], COLS
   je next_line
   ret

next_line:
   mov byte [POS_X], 0
   inc byte [POS_Y]
   ret

get_current_position:
   ; compute the column byte
   xor eax, eax
   mov ecx, COLS * 2 ; number of bytes for columns
   mov al, byte [POS_Y] ; POS_Y in eax
   mul ecx ; multiply eax * ecx
   push eax ; save byte in eax

   ;compute the lign byte
   mov al, byte [POS_X] ; put POS_X in eax
   mov cl, 2 ; multiply eax by 2(char is 2 bytes)
   mul cl

   pop ecx ; get back result from eax put it in ecx
   add eax, ecx ; add the computed X pos and computed Y pos(stor in eax)

   xor ecx, ecx ; clear ecx
   add edx, eax ; add final byte offsets to video address

   ret


Thanks in advance for the help


Top
 Profile  
 
 Post subject: Re: Converting screen position into byte
PostPosted: Sat May 04, 2019 5:03 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
Seems to me the video address in EDX that you set at the start is getting clobbered by the MUL ECX you are doing since the result of the multiplication of EAX and ECX is being placed in EDX:EAX wiping out your video pointer in EDX. Side note: multiplying something by 2 can be done by shifting a register left 1 bit.


Top
 Profile  
 
 Post subject: Re: Converting screen position into byte
PostPosted: Sun May 05, 2019 5:36 am 
Offline

Joined: Sat May 04, 2019 4:31 pm
Posts: 12
Thanks! That was it. About the bit shifting, what is the advantage of using it instead of mul ?


Top
 Profile  
 
 Post subject: Re: Converting screen position into byte
PostPosted: Sun May 05, 2019 8:46 am 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
Shifting bits to the left is significantly better performing than the MUL instruction. MUL is an expensive instruction to execute. Of course this only works if you are multiplying by powers of 2 (2,4,8,16,32,64 etc)


Top
 Profile  
 
 Post subject: Re: Converting screen position into byte
PostPosted: Tue May 07, 2019 7:43 am 
Offline

Joined: Sat May 04, 2019 4:31 pm
Posts: 12
Alright, I will change it. Thanks for the help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], deblokc and 79 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group