Sign in to follow this  
cNoob

Speeding up ASM loop

Recommended Posts

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.

Share this post


Link to post
Share on other sites
#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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 mode
mov es, bx
xor di, di

;You may need to swap around if you get a
;weird color with a strange character
mov ah, 0Eh ;This should give Yellow on black
mov al, 'X' ;Puts The Letter X Into al

mov cx, 5 ;How Many repeats
rep stosw ;Fill the memory at ES:DI

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





Try that and let me know how you get on :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Ok thanks for the help guys ive now relized that my loop was running slow because i am running my boot loader in bochs which is an emulator and is slower than the real bios. Ive tried my code on my tet PC and its all running as fast as i need it to. Thanks guys.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this