Page 1 of 1

Following the baby steps tutorial, why does this code work?

Posted: Tue Oct 23, 2012 2:40 pm
by handuel
Hi, another noob here, so sorry if I'm not to quick to grasp some stuff, (don't worry I know some basic OS theory though, I'm not coming here from nothing). Anyway, I've been following the babystep tutorials on the wiki (which are very good by the way, thankyou whoever wrote them), and have reached number 4. The code inside it is pretty basic, and I understand most of it, however there are a few lines in the printreg16 "function" that I don't understand. Here is the code for people to look at: http://wiki.osdev.org/Babystep4
I could just copy and paste the lines in, but I somehow doubt that will help me learn. The lines are these

rol ax, 4
mov bx, ax
and bx, 0x0f
mov bl, [si + bx]
mov [di], bl

this is part of a loop that loops for 4 times (1 for each character, as 16 bit registers have 4 digits when written in hex) and the register we are attempting to print's higher byte is the color attribute, and it's lower byte is the character.

My question is, too get the hex value of the character, why does the lines that I have pasted work. I know what each individual instruction does, but I can't seem to figure why that would find the hex value of the character. Can anyone tell me why.

Re: Following the baby steps tutorial, why does this code wo

Posted: Tue Oct 23, 2012 6:20 pm
by Brendan
Hi,
handuel wrote:My question is, too get the hex value of the character, why does the lines that I have pasted work.
I'd assume that DS:SI points to an array of 16 characters (e.g. "0123456789ABCDEF"); and the line "mov bl, [si + bx]" fetches a character from this array.

The only other slightly tricky part is "rol ax, 4", which rotates the value so that the highest 4 bits become the new lowest 4 bits. The equivalent in C would be "value = (value << 4) | (value >> 28);".


Cheers,

Brendan

Re: Following the baby steps tutorial, why does this code wo

Posted: Wed Oct 24, 2012 12:33 am
by handuel
Thankyou very much, I've worked it out know, and you explaining about the array helped me a lot. Thanks for the quick reply :D