Why bother putting in obscure bit shifts if the compiler will do it for you? Any decent compiler will optimize ((y * 80) + x) * 2 to what you have above.Curufir wrote: ((y<<6 + y<<4) + x) << 1
Why bother with a mul if you don't have to![]()
PutChar & GetChar
-
Tim
Re:PutChar & GetChar!
-
Curufir
Re:PutChar & GetChar!
Guess it's personal preference to assume as little as possible about the compiler's optimisation routines. If they're there then that's helpful, but I prefer to be explicit about exactly what I want the compiler to produce. It's a good point though Tim.
Hmm, df's problem.
With the result ending in ax. If things are that general MUL will always win out on cycles though (MUL is 13-26 cycles on 486 according to HelpPC).
**
That code seems right, but I wouldn't go using it. Anyhow the general principle should be plain to see.
Hmm, df's problem.
Code: Select all
mov bx, [y]
mov dx, [width]
mov ax, 0
mov cx, [x]
.1
shr bx, 1
jnc .2
add ax, dx
nop
.2
cmp bx, 0
je .3
shl dx, 1
jmp short .1
.3
add ax, cx
shl ax, 1
**
That code seems right, but I wouldn't go using it. Anyhow the general principle should be plain to see.
- Pype.Clicker
- Member

- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:PutChar & GetChar!
and why asking the compiler to optimize your "y*160" while you could remember it... Think about it: for the cost of only 2 bytes, you just store the start of the current line, and you perform a "next line" operation with "console.line_offset+=console.line_width". Now when you want to write a new char you just push it at
Try to remember that most of the time, you're adding chars one after another: a GOTOXY is rather an infrequent operation, so its optimization isn't that important.
Also, if you manage to do it, avoid to have a "putchar(c)" function but rather keep your "textram[...]" stuff in the main loop (repeating call/ret will decrease the throughput).
ALT: if you really want a "putchar" function, declare it "static __inline__" so that the compiler can do the better job with it.
Code: Select all
*textram[line_offset+column_offset]=the_char;
*textram[line_offset+column_offset+1]=the_attributes;
column_offset+=char_size;
Also, if you manage to do it, avoid to have a "putchar(c)" function but rather keep your "textram[...]" stuff in the main loop (repeating call/ret will decrease the throughput).
ALT: if you really want a "putchar" function, declare it "static __inline__" so that the compiler can do the better job with it.
-
Whatever5k
Re:PutChar & GetChar!
This putchar() function isn't really useful since you have to know x and y. You should replace this function which only takes the character (char c). A way to do this is to have a static int x and static int y which are both 0 at startup. On each putchar(), x is incremented. If the line is full, x is set to 0 and y is incremented...
Based on this more useful putchar(), you could easily implement printf() or printk() with:
Based on this more useful putchar(), you could easily implement printf() or printk() with:
Code: Select all
int printf(const char *format, ...)
{
char buf[MAX];
va_list arg; /* you need to have the va_x functions
may copy these from linux sources
*/
int i = 0;
va_start(arg, format);
vsprintf(buffer, format, arg); /* need vsprintf */
va_end(arg);
while(buffer[i] != '\0')
putchar(buffer[i++]);
return i; /* return amount of printed characters */
}
-
Whatever5k
Re:PutChar & GetChar!
But i purposly put in the x and y Co-ordiantes, i think that having the x and y Co-ordinates there makes it even more handyWhat? That should be handy? Sorry if I say this so directly, but the way you do it is crap! Consider your kernel is setting up paging or a file system for example and wants to print "done". How does it know where to print it? Why don't you make life easier?
-
jrfritz
Re:PutChar & GetChar!
I know how it could make it easy...if he did this:
void putch( char c, int x = curx, int y = cury )
He could use C++ and make a powerfull putch so you could use it two ways:
putch(c)
or putch( c, x, y ).
instead of making a putch that needs gotoxy.
void putch( char c, int x = curx, int y = cury )
He could use C++ and make a powerfull putch so you could use it two ways:
putch(c)
or putch( c, x, y ).
instead of making a putch that needs gotoxy.
- Pype.Clicker
- Member

- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:PutChar & GetChar!
I don't find it *that* powerful ...Tom wrote: I know how it could make it easy...if he did this:
void putch( char c, int x = curx, int y = cury )
He could use C++ and make a powerfull putch so you could use it two ways:
Code: Select all
#define Putch(c) putch(c,curx,cury)
