puts() using asm

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator:Moderators

Post Reply
Blasterman
Posts:4
Joined:Sat Nov 24, 2007 6:23 pm
Location:In front of my computer, duh.
Contact:
puts() using asm

Post by Blasterman » Sun Dec 16, 2007 2:51 am

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....
Image
Image

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Post by Andyhhp » Fri Dec 21, 2007 4:18 pm

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

Post Reply