Jump to content
  • Advertisement
Sign in to follow this  
Gink

Speed Question - ASM vs C

This topic is 4735 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As a simple test, I wanted to see how much faster ASM was than C, so I decided to compare printf to Display String function 9. I was surprised to see that ASM displayed 0xFFFF strings in 3 seconds compared to the 21 seconds it took in C. Is this kind of speed difference normal? Also can C ever be as fast as ASM when it comes to displaying strings? Its written as real mode flat model. Is my ASM code wrong or is there a huge speed difference between C and ASM? edit : Also tried creating the string before printf - same result Compiled with GCC Here is the code

#include <stdio.h>

int main(){
     for(register int x=0; x < 0xFFFF; x++){    
           printf("FF is now a string\n");
     }
}


[BITS 16]
[ORG 0x100]

%include "Byte2Str.MAC"

[SECTION .text]

mov AL,0xFF  ;load number into AL (ByteToString uses AL)
mov SI,msg   ;Load address of string
ByteToString ;converts a 1 byte number into a 2 byte string

mov DX,msg
mov AX,0x0900  ;Function 9 - printstring

mov CX,0xFFFF ;Loop 0xFFFF Times
looptop:
dec CX
INT 0x21       ;DOS Dispatcher
jnz looptop

mov AX,0x4C00  ;0x4C00 - Terminate Program
int 0x21       ;Dos Dispatcher


[SECTION .data]
Digits db '0123456789ABCDEF'
msg db "   is now a string!",13,10,'$'

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
The speed of implementation in C outweights the difference in runtime comparison. The reason for your loss of speed in C is probably because you have to call into the standard library, which might be dynamically linked on top if it all.
Real speed differences occur when you compare a good solution optimised for a specific problem to a lousy solution optimised for nothing or a general case of the problem.
It is only worth using assembly code in kernel/driver development and perhaps as a last line of defence in a rendering pipeline. (Don't bother flaming me for that opinion)

Share this post


Link to post
Share on other sites
printf() is for formatted output so there is of course some overhead if you're only printing a simple line of text

Share this post


Link to post
Share on other sites
Quote:
Original post by Gink
Is this kind of speed difference normal?
Is this kind of speed difference meaningful? Does printf do the exact same thing as the ASM display routine, or is there a greater overhead because printf provides greater functionality?

You're not asking informed - and, therefore, useful - questions. Go take a look at the source to printf to determine what it does, then extract the portion that just displays a string - the part that does exactly the same thing as ASM - and only then can you begin to make useful benchmarks.

Share this post


Link to post
Share on other sites
Yeah sure it was oldhat in DOS days to do that. If you don't need portability and will only run on DOS do it and have fun, there are a million tricks you could do in DOS you can't do on modern OS anymore. But it's kind of useless these days...

Share this post


Link to post
Share on other sites
So is there any function in C that can write to output as fast as the one ASM has? Also where can i find the Source for printf

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
indeed, puts("FF is now a string"); should be faster due to no formatting..

Share this post


Link to post
Share on other sites
Visual C++ ships with Microsofts implementation of printf. GNU's C implementation is Open Source and thus by definition, the source is available.

Before you post any more meaningless results, I suggest you try to figure out what is the difference between the two code paths.

You can use DEBUG.COM to trace through both your assembly code and the interupt, (yea, I know you shouldn't trace through interupts but it is a software one) and most C dev environments allow you to single step through both your C code and the machine code it generated.

Share this post


Link to post
Share on other sites

This topic is 4735 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!