Speeding up ASM loop

Started by
9 comments, last by cNoob 18 years, 1 month ago
Hey everyone Ive been making my own BootLoaders for a simple OS im creating and Im trying to loop a piece of code:

[BITS 16]
[ORG 0x7C00]

mov ah, 0Eh
mov bh, 0
mov bl, 07h
mov cx, 5             ;How Many Times To Loop

main:                 ;Start Main Loop
mov al, 'X'           ;Puts The Letter X Into al
int 10h               ;Present
loop main             ;Loop Back To Beginning

times 510 - ($-$$) db 0
dw 0AA55h

So as you can see from my code the loop loops 5 times and then stops. This is exactly what i want but it seems to be very slow at doing so. Any help on speeding up my simple loop would be great. Thanks.
Advertisement
#1 "main" is not a good choice for a lable as it's too easily confused with the generic C program launch function, ie. int main(int argc, char *argv[])

#2 try "unrolling" the loop - that is, simply repeat the contents once for each loop - in this case 5 times

mov al, 'X' ;Puts The Letter X Into al
int 10h ;Present
mov al, 'X' ;Puts The Letter X Into al
int 10h ;Present
mov al, 'X' ;Puts The Letter X Into al
int 10h ;Present
mov al, 'X' ;Puts The Letter X Into al
int 10h ;Present
mov al, 'X' ;Puts The Letter X Into al
int 10h ;Present

The slowness is more likely due to the interupt invocation rather than the loop.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
The BIOS interrupt routines are quite slow, if you need more speed you could look into I/O port programming. That will give you direct access to the hardware. It's been a long time since I've done any ASM, but basically you use the 'in' and 'out' instructions to address the hardware ports and write values to them sequentially. I found this pdf which contains a nice listing of all hardware ports to play with. Good luck.
OK thanx for pointing out the main label thing lessBread ill change that. Also thanks for that link prototype ill check it out. thanks guys
Well, it's been a long time since I did ASM programming however I do know that to write to the screen it starts at B800:0000

so

[BITS 16][ORG 0x7C00]mov bx, 0B800h        ;This will be start of video location                      ;Note that graphics mode use A000:0000                      ;When in graphics modemov es, bxxor di, di;You may need to swap around if you get a;weird color with a strange charactermov ah, 0Eh           ;This should give Yellow on blackmov al, 'X'           ;Puts The Letter X Into almov cx, 5             ;How Many repeatsrep stosw             ;Fill the memory at ES:DItimes 510 - ($-$$) db 0dw 0AA55h


Try that and let me know how you get on :)
Thanks for that sniplet Adam that code does work but it still takes a while to run like my old code. Also does anyone now anywhere i can get tutorials about OS Development.
I have created a simple kernal in c and a piece of asm which starts my kernal. Ive compiled my asm file into an object file called ks.o and my c file into kernal.o and I now need a way of linking these to files into one called kernal.bin. Ive seen that people are using ld to link files but when i try using it I need a script. Does anyone know where i can get a script for ld to link my object files into one *.bin file. Thanks.
Quote:Original post by cNoob
I have created a simple kernal in c and a piece of asm which starts my kernal. Ive compiled my asm file into an object file called ks.o and my c file into kernal.o and I now need a way of linking these to files into one called kernal.bin. Ive seen that people are using ld to link files but when i try using it I need a script. Does anyone know where i can get a script for ld to link my object files into one *.bin file. Thanks.


All I remember is that you link using ld only with an extension, can't remember which just read the man file (or help doc) to find what to add. You link using a format and make sure you can load them, some format has headers that you must read or skip. The easiest way would be to link to a "raw" format.

You could use a config file but its not necessary and if you decide to make one you should make it yourself or tweak one that you find in the build scripts you can find on other free and open source [simple] OS.
How do you mean it runs very slowly?

You should have those X's displayed immediately. Try setting CX to 2000 so the whole screen gets filled with X's. They should fill the screen in a blink of an eye.

Quote:Original post by cNoob
... Also does anyone now anywhere i can get tutorials about OS Development.


Try this site: Bona Fide OS development

And this one too: OS-FAQ Wiki
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement