Page 1 of 1

can't get my putc function to put a c

Posted: Wed Sep 09, 2015 3:53 pm
by zerorax
this is the relevant code. it SHOULD put a character in edx to the screen and increment the column variable by 1 but it does nothing. can anyone see what I'm don't wrong?

terminal_row db 0
terminal_column db 0

putc:
mov eax, terminal_row
mov ebx, 80
mul ebx
add eax, terminal_column
mov ebx, 2
mul ebx
add eax, 0xB8000
mov byte[eax], dl
mov bx, terminal_column
inc bx
mov terminal_column, bx
ret

Re: can't get my putc function to put a c

Posted: Wed Sep 09, 2015 6:07 pm
by BASICFreak
Your code seem right and should place the character with no problem other than:
The color attribute seems to be 0x00 (black on black)
Mul overwrites both eax and edx (so your dl is probably not what you think it is [dl=0x00?])


Also to optimize it a little do a bit shift, do not multiply (unless you multiply by a variable and not a constant)

Re: can't get my putc function to put a c

Posted: Wed Sep 09, 2015 7:07 pm
by zerorax
i have already set the color of all the characters in the buffer to 7. and yes i realized what I was doing so I push edx at the begninning now and pop it at the end right before I use it. still no good

Re: can't get my putc function to put a c

Posted: Wed Sep 09, 2015 9:20 pm
by alexfru
If this is written for NASM/YASM or FASM, you need square brackets around memory operands that you read or write. You only have them in one out of five places.

Re: can't get my putc function to put a c

Posted: Thu Sep 10, 2015 4:50 am
by glauxosdever
Hi,

Here is the code (untested). Let us know if it works.

Code: Select all

terminal_row db 0
terminal_column db 0

putc:
; in: dl=char, cl=color
    movzx eax, byte [terminal_row]    ; For terminal_row you have defined 1 byte, but eax is 4 bytes wide
    movzx ebx, byte [terminal_column] ; We are going to use it later
    imul eax, 80*2                    ; There are 80 chars per row but each char takes 2 bytes
    add eax, ebx                      ; Add columns of last row two times
    add eax, ebx                      ; because each char takes 2 bytes
    mov byte [0x000B8000 + eax], dl   ; Write char
    mov byte [0x000B8001 + eax], cl   ; Write color
    inc byte [terminal_column]        ; Go one column right
    ret
You should check if terminal_column is above or equal to 80. If so, reset terminal_column and increment terminal_row (don't let it go to next row automatically, because terminal_column will overflow). You should also check if terminal_row is above or equal to 25. If so, you should scroll.

Regards,
glauxosdever

Re: can't get my putc function to put a c

Posted: Thu Sep 10, 2015 8:14 am
by xenos
glauxosdever wrote:Here is the code (untested).
Note that your code trashes EBX, so if you call it from C code, you should save and restore it.