<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>James Ball &#187; OS Development</title>
	<atom:link href="http://jwball.co.uk/category/programming/os-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://jwball.co.uk</link>
	<description></description>
	<lastBuildDate>Sat, 14 Jan 2012 11:57:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Real and Protected Segmentation</title>
		<link>http://jwball.co.uk/2011/04/real-and-protected-segmentation/</link>
		<comments>http://jwball.co.uk/2011/04/real-and-protected-segmentation/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 11:00:54 +0000</pubDate>
		<dc:creator>James Ball</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[OS Development]]></category>

		<guid isPermaLink="false">http://jwball.co.uk/?p=153</guid>
		<description><![CDATA[One of things that lead to the downfall of my first attempt to write an operating system last summer was not fully understanding how segmentation works and that there are two different types depending on the CPUs mode. In real mode, segmentation is quite simple. A logical address consists of a segment and an offset. [...]]]></description>
			<content:encoded><![CDATA[<p>One of things that lead to the downfall of my first attempt to write an operating system last summer was not fully understanding how segmentation works and that there are two different types depending on the CPUs mode.</p>
<p>In real mode, segmentation is quite simple. A <em>logical</em> address consists of a segment and an offset. This logical address is then converted to a physical address internally by the CPU. To convert the logical address to a physical one, the CPU shifts the segment value 4 bits to the left (equivalent to multiplication by 16) and then adds the value of the offset.</p>
<p>Let&#8217;s look at an example:</p>
<pre class="brush: asm; title: ; notranslate">
mov word ax, [0x07c0:0x0200]
</pre>
<p>So, the segment is 0x07c0 (which refers to the 0x07c0th 64KiB block of memory). First, the segment is shifted 4 bits to the right, leaving us with 0x7c00. Then the offset (0&#215;0200) is added, leaving us with a physical address of 0x7e00. </p>
<p>Pretty simple huh? </p>
<p>One problem you may notice with this is that there isn&#8217;t a unique logical address for each physical address, for example:</p>
<pre class="brush: asm; title: ; notranslate">
mov word ax, [0x07c0:0x0200]
mov word ax, [0x07e0:0x0000]
</pre>
<p>Both instructions refer to exactly the same physical address (0x7e00)!</p>
<p>The story in protected land becomes a little more complex. Segments no longer refer to a 64KiB block of memory, instead they have the following form:</p>
<p>The lowest 2 bits of the segment describe the privilege level that the segment expects to be accessed in (i.e. 0 is ring 0, 1 is ring 1, etc)</p>
<p>The next bit describes which table the CPU should look the segment index up in. A clear bit indicates the segment will be found in the GDT, whilst a set bit indicates it&#8217;ll be found in the LDT.</p>
<p>The remaining 13 bits are the segment index, the index points to an entry in either the LDT or GDT.</p>
<p>In protected mode, a logical address is converted first to a linear address by the CPU. The linear address is then sent through the paging unit to be converted to a physical address.</p>
<p>The conversion to a linear address is performed by taking the upper 13 bits of the segment selector, multiplying it by 8 (the size of an entry in either the GDT and LDT) and using the resulting value as a pointer to the segment descriptor in either the GDT or LDT. The correct segment descriptor contains the linear base address of the segment, which is taken by the CPU and added to the offset.</p>
<p>So for example,</p>
<pre class="brush: asm; title: ; notranslate">
jmp 0x08:0x0200</pre>
<p>The segment is 0&#215;08, or 00001000 in binary. The lower two bits (bit 0 and 1) with a value of 0 show that the segment should only be accessed in ring 0 and the next bit (bit 2) shows that the segment descriptor can be found in the GDT. The remaining 13 bits simply equal 1, which tells the CPU that the corresponding segment descriptor is at position 1 in the GDT.</p>
<p>If the segment descriptor at position 1 in the GDT had a base address of 0xcafe then the CPU adds the base to the offset (0&#215;0200) to give the linear address of 0xccfe. Other checks are made such as checking the privilege level, read and write access, etc. and if they fail, the CPU will raise an exception.</p>
<p>I will be discussing segment descriptors and the GDT in a later post.</p>
<p>Exercise: Look up segment descriptors, the GDT and the LDT in the <a href="http://www.intel.com/products/processor/manuals/">Intel x86 Manual, Volume 3</a>.</p>

				<!-- Social Sharing Toolkit v2.0.4 | http://www.marijnrongen.com/wordpress-plugins/social_sharing_toolkit/ -->
				<div class="mr_social_sharing_wrapper"><span class="mr_social_sharing"><iframe src="https://www.facebook.com/plugins/like.php?locale=en_US&amp;href=http%3A%2F%2Fjwball.co.uk%2F2011%2F04%2Freal-and-protected-segmentation%2F&amp;layout=standard&amp;show_faces=false&amp;width=51px&amp;height=24px" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:51px; height:24px;" allowTransparency="true"></iframe></span><span class="mr_social_sharing"><a href="http://twitter.com/share?url=http%3A%2F%2Fjwball.co.uk%2F2011%2F04%2Freal-and-protected-segmentation%2F&amp;text=Real+and+Protected+Segmentation" target="_blank" class="mr_social_sharing_popup_link"><img src="http://jwball.co.uk/wp-content/plugins/social-sharing-toolkit/images/buttons/twitter.png" alt="Share on Twitter" title="Share on Twitter"/></a></span><span class="mr_social_sharing"><g:plusone size="medium" count="false" href="http://jwball.co.uk/2011/04/real-and-protected-segmentation/"></g:plusone></span><span class="mr_social_sharing"><script type="IN/Share" data-url="http://jwball.co.uk/2011/04/real-and-protected-segmentation/"></script></span><span class="mr_social_sharing"><a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjwball.co.uk%2F2011%2F04%2Freal-and-protected-segmentation%2F&amp;title=Real+and+Protected+Segmentation" target="_blank" class="mr_social_sharing_popup_link"><img src="http://jwball.co.uk/wp-content/plugins/social-sharing-toolkit/images/buttons/stumbleupon.png" alt="Submit to StumbleUpon" title="Submit to StumbleUpon"/></a></span><span class="mr_social_sharing"><a href="http://digg.com/submit?url=http%3A%2F%2Fjwball.co.uk%2F2011%2F04%2Freal-and-protected-segmentation%2F&amp;title=Real+and+Protected+Segmentation" target="_blank" class="mr_social_sharing_popup_link"><img src="http://jwball.co.uk/wp-content/plugins/social-sharing-toolkit/images/buttons/digg.png" alt="Digg This" title="Digg This"/></a></span></div>]]></content:encoded>
			<wfw:commentRss>http://jwball.co.uk/2011/04/real-and-protected-segmentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing Text to the Screen in Real Mode</title>
		<link>http://jwball.co.uk/2011/04/writing-text-to-the-screen-in-real-mode/</link>
		<comments>http://jwball.co.uk/2011/04/writing-text-to-the-screen-in-real-mode/#comments</comments>
		<pubDate>Thu, 14 Apr 2011 01:35:34 +0000</pubDate>
		<dc:creator>James Ball</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[OS Development]]></category>
		<category><![CDATA[vga]]></category>

		<guid isPermaLink="false">http://jwball.co.uk/?p=29</guid>
		<description><![CDATA[There are two methods for writing text to the screen in real mode, one involves the use of BIOS interrupts whilst the other involves writing directly to video memory. In my opinion, using the BIOS routine is a little overkill; it&#8217;s slow and not as &#8220;clean&#8221; as writing directly to the video memory. To start [...]]]></description>
			<content:encoded><![CDATA[<p>There are two methods for writing text to the screen in real mode, one involves the use of BIOS interrupts whilst the other involves writing directly to video memory. In my opinion, using the BIOS routine is a little overkill; it&#8217;s slow and not as &#8220;clean&#8221; as writing directly to the video memory.</p>
<p>To start with, we must understand that there are various different video modes and standards, each which may have video memory mapped to a different region. For example, if the video mode is set to monochrome text mode, then the video memory starts at <strong>b000:0000</strong>, whereas if the video mode is set to 256 bit colour graphics then the video memory is located at <strong>a000:0000</strong>. This article will focus on the standard colour text mode, 80 columns wide and 25 lines deep, whose memory is located at <strong>b800:0000</strong>.</p>
<p>This article also assumes that you understand real mode segmented addressing notation (segment:offset), that you have a basic understanding of x86 assembly language and that you&#8217;re trying to write your own boot loader or operating system.</p>
<p>Let&#8217;s look a little more into the video memory used by colour text mode, as mentioned before, the memory is mapped to a region of ram which starts at <strong>b800:0000</strong>. Because there are 80 columns of characters and 25 lines, this gives the total number of displayable characters as 2000. Each character takes up a byte of space in memory (its ASCII code) and also requires a single attribute byte.</p>
<p>This means that to write the letter &#8216;a&#8217; on the screen in the top left-hand corner, we would need to write the byte <strong>0&#215;61</strong> to <strong>[b800:0000]</strong> and an attribute byte to <strong>[b800:0001]</strong>. Writing a &#8216;b&#8217; next to the &#8216;a&#8217; would require us to write the byte <strong>0&#215;62</strong> to <strong>[b800:0002] </strong>along with an attribute byte into <strong>[b800:0003]</strong>, and so on.</p>
<p>But what is this attribute byte? The lower 4 bits of the attribute byte represent the foreground colour of the character and the high 4 bits represent the background colour of the character. An attribute byte of <strong>0x0f</strong>, for example, gives a white character on a black background.</p>
<p>So, let&#8217;s see an actual example of writing a character to the screen.</p>
<pre class="brush: asm; title: ; notranslate">
mov ax, 0xb800
mov gs, ax			        ; Set GS to point to video memory
xor bx, bx			        ; Set BX to point to the first character
mov byte [gs:bx], 'a'		; Write 'a'
inc bx                                ; Set BX to point to the first attribute byte
mov byte [gs:bx], 0x0f		; Write the attribute byte
inc bx                                ; Set BX to point to the second character
</pre>
<p>This can be represented rather more eloquently in C (and when the CPU is in protected mode) as:</p>
<pre class="brush: cpp; title: ; notranslate">
unsigned char * p = (unsigned char *) 0xb8000;
*p++ = 'a';
*p++ = 0xf;
</pre>
<p>Exercise: Write a function in assembly which prints the string &#8220;Hello, World&#8221; to the screen. Hint: <strong>LODSB</strong> is your friend.</p>
<p>In a future post I&#8217;ll look into scrolling and making use of different text pages.</p>

				<!-- Social Sharing Toolkit v2.0.4 | http://www.marijnrongen.com/wordpress-plugins/social_sharing_toolkit/ -->
				<div class="mr_social_sharing_wrapper"><span class="mr_social_sharing"><iframe src="https://www.facebook.com/plugins/like.php?locale=en_US&amp;href=http%3A%2F%2Fjwball.co.uk%2F2011%2F04%2Fwriting-text-to-the-screen-in-real-mode%2F&amp;layout=standard&amp;show_faces=false&amp;width=51px&amp;height=24px" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:51px; height:24px;" allowTransparency="true"></iframe></span><span class="mr_social_sharing"><a href="http://twitter.com/share?url=http%3A%2F%2Fjwball.co.uk%2F2011%2F04%2Fwriting-text-to-the-screen-in-real-mode%2F&amp;text=Writing+Text+to+the+Screen+in+Real+Mode" target="_blank" class="mr_social_sharing_popup_link"><img src="http://jwball.co.uk/wp-content/plugins/social-sharing-toolkit/images/buttons/twitter.png" alt="Share on Twitter" title="Share on Twitter"/></a></span><span class="mr_social_sharing"><g:plusone size="medium" count="false" href="http://jwball.co.uk/2011/04/writing-text-to-the-screen-in-real-mode/"></g:plusone></span><span class="mr_social_sharing"><script type="IN/Share" data-url="http://jwball.co.uk/2011/04/writing-text-to-the-screen-in-real-mode/"></script></span><span class="mr_social_sharing"><a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjwball.co.uk%2F2011%2F04%2Fwriting-text-to-the-screen-in-real-mode%2F&amp;title=Writing+Text+to+the+Screen+in+Real+Mode" target="_blank" class="mr_social_sharing_popup_link"><img src="http://jwball.co.uk/wp-content/plugins/social-sharing-toolkit/images/buttons/stumbleupon.png" alt="Submit to StumbleUpon" title="Submit to StumbleUpon"/></a></span><span class="mr_social_sharing"><a href="http://digg.com/submit?url=http%3A%2F%2Fjwball.co.uk%2F2011%2F04%2Fwriting-text-to-the-screen-in-real-mode%2F&amp;title=Writing+Text+to+the+Screen+in+Real+Mode" target="_blank" class="mr_social_sharing_popup_link"><img src="http://jwball.co.uk/wp-content/plugins/social-sharing-toolkit/images/buttons/digg.png" alt="Digg This" title="Digg This"/></a></span></div>]]></content:encoded>
			<wfw:commentRss>http://jwball.co.uk/2011/04/writing-text-to-the-screen-in-real-mode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

