can't get my putc function to put a c

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
zerorax
Posts: 3
Joined: Wed Sep 09, 2015 3:41 pm
Libera.chat IRC: zerorax

can't get my putc function to put a c

Post 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
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

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

Post 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)
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
zerorax
Posts: 3
Joined: Wed Sep 09, 2015 3:41 pm
Libera.chat IRC: zerorax

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

Post 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
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

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

Post 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.
glauxosdever
Member
Member
Posts: 501
Joined: Wed Jun 17, 2015 9:40 am
Libera.chat IRC: glauxosdever
Location: Athens, Greece

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

Post 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
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

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

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Post Reply