Page 1 of 1

puts() using asm

Posted: Sun Dec 16, 2007 2:51 am
by Blasterman
Hi guys,
I've been studying Demo 5 a lot, and I was thinking we could implement <tt>puts()</tt> using inline assembly.

At the beginning of <tt>kmain()</tt>, there would be asm that defines the <tt>Print</tt> macro for printing the string.
Then there would be the <tt>puts()</tt> function itself. It would go something like this:

Code: Select all

inline unsigned char _cdecl puts (unsigned char str)
{
  _asm {
        msg db str,13,10,0
        mov si, msg
        call Print
       }
Anyone else like the idea? Anyone have a better idea? Anyone have some bugfixes?

EDIT: Wait, that'd only work for real mode....

Posted: Fri Dec 21, 2007 4:18 pm
by Andyhhp
Firstly, to print strings, the function should be declared as 'unsigned char *' else it will only work for a single character.

Also, there are a few problems with your ASM code:
1)you need a jump at the beginning of the block to prevent the program trying to execute data (A good compiler should catch this)
2)the variable 'str' is a pointer to a string so cant be used in a data declaration.
3)as this function is declared inline, it will write 3 bytes of data into the code section of the program every time you call puts() which is a) a large waste of space and b) makes debugging by dissassembly almost impossible
4)this would require you to write a ASM Print function in the kernal itself else you will be trying to call a memory location thats in the wrong code segment.

Sorry to be so negative,

Andrew