Monday, October 04, 2010

Playing With Assembly

As I usually do, I was playing with languages, this time was Assembly, then I started to rebuild my OS, just doing simple things. Doing a simple printf and scanf using the BIOS. I will put the code and explain it to make better.

Printing Characters
printf:
mov ah, 0eh
mov bl, 07h

.nextchar
lodsb
or al, al
jz .return
int 10h
jmp .nextchar

.return
ret

First of all, you define the 0x0e value to the higher part of the general register AX to have the output function of the interrupt 0x10, the second line that moves the value 0x07 to the lower part of the general register BX is optional, because it say what will be the colors of the output text, where the first one(in this case 0, that is black) is the background, and the second one(white that is7, in this case) is the foreground one.

Then you need to do the lodsb, that will load what is inside the register SI and move it to the lower part of the general register AX, that is AL, used for the 0x10 interrupt as the output value, then it do a simple loop then prints it.

Getting Input
scanf:       
mov ah, 00h
int 16h

mov ah, 0eh
int 10h

loop getinput

This one is a lot more simple than the printf one, then you will get it better than the other. :)

First of all you need to move the value 0x00 to the higher part of the general register AX, that is the value to make the 0x16 interrupt wait for a keystroke and as it does by default, the result(character of the key that was pressed) is moved to the lower part of the general register AX, that is the same used by the 0x10 interrupt to output, then the rest is very simple to understand(as you already saw at the printf one), then it does a infinite loop into the get character function.

Using They
To use them is very simple, for the printf, you just need to move the values that you want to the register SI and call the correct function, like this:
mov si, msg
call printf

msg db "Hello, World!"

Very simple, but for the scanf one is much more simple than the other one. You just need to call the function and it will do the rest for you. Like this:
call scanf


And here is the result running on my Bochs:


I hope you've enjoyed. I was thinking first to make this a tutorial, on the tutorial are of our community, but I think that a blog entry is better to launch my blog. :)

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home