Converting screen position into byte

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
R3DC0DE
Posts: 12
Joined: Sat May 04, 2019 4:31 pm

Converting screen position into byte

Post by R3DC0DE »

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: Select all

[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
MichaelPetch
Member
Member
Posts: 798
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Converting screen position into byte

Post by MichaelPetch »

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.
R3DC0DE
Posts: 12
Joined: Sat May 04, 2019 4:31 pm

Re: Converting screen position into byte

Post by R3DC0DE »

Thanks! That was it. About the bit shifting, what is the advantage of using it instead of mul ?
MichaelPetch
Member
Member
Posts: 798
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Converting screen position into byte

Post by MichaelPetch »

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)
R3DC0DE
Posts: 12
Joined: Sat May 04, 2019 4:31 pm

Re: Converting screen position into byte

Post by R3DC0DE »

Alright, I will change it. Thanks for the help.
Post Reply