Speed Question - ASM vs C

Started by
48 comments, last by mittens 18 years, 8 months ago
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,'$'

Advertisement
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)
printf() is for formatted output so there is of course some overhead if you're only printing a simple line of text

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

"It's such a useful tool for living in the city!"
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
indeed, puts("FF is now a string"); should be faster due to no formatting..
puts took 30 seconds..
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.
1. Go inside of printf and remove all of the of the formatting code, etc., and do a test run with that.

2. Ask yourself why this all matters.

This topic is closed to new replies.

Advertisement