Archive for February 2013

An Introduction To Hard Disk Geometry


Introduction
This page describes the typical layout of a modern hard drive. You may have heard of file systems such as NTFS, FAT32 or EXT3, which are used by your operating system. Concepts like files and directories are contained in these file systems, so obviously they are very important. But a single physical hard disk can contain multiple filesystems – each on a separate partition. And a hard disk that contains a operating system must contain some elements that play a role in the boot sequence.

Obviously a lot more is going on under the hood. Let’s take a look.

Anatomy of Hard Disk



Hard disks have been around since the 1950′s, but the design has not changed much. The general hard disk design is quite simple, consisting of only a few moving parts. In the picture above you can see:
  • Platters: Solid disks with a magnetic coating that contains the data. The platters spin at a constant rate when the hard disk is in operation,  typically at 3600, 5200 or 7200 rounds per minute (rpm).
  • Arms: The head stack assembly holds the arms that hold the read/write heads. The stack is rotated by an actuator which is not displayed in the image, causing the arms to position the heads between the hub and the edge of the platter. To achieve great speed and accuracy, the arm and its movement mechanism need to be extremely light and fast. The arm on a typical hard-disk drive can move from hub to edge and back up to 50 times per second


Every platter contains many concentric circles – called tracks – that are used to store the data. This radically differs from a CD or DVD, where a single track of data is used, laid out as a spiral. A modern hard disk has tens of thousands of tracks on a platter. The tracks on a hard disk are divided up into smaller segments called sectors. Each sector usually holds 512 bytes of user data, plus as many as a few dozen additional bytes used for internal drive control and for error detection and correction.

The heads that access the platters are locked together on an assembly of head arms. This means that all the heads move in and out together, so each head is always physically located at the same track number. It is not possible to have one head at track 0 and another at track 1,000. Because of this arrangement, often the track location of the heads is not referred to as a track number but rather as a cylinder number. What you should take away from this is that they are essentially the same.


Logical Geometry
The logical geometry of a hard disk is the logical structure that programs see when communicating with the hard disk’s controller – which is located on a logical board inside the hard disk. In all but the earliest hard disks the physical geometry is a lot more complicated than the logical geometry. Luckily, only the hard disk’s engineers have to deal with the complicated nature of the physical geometry; they are hidden from the operating system and the user.
Addressing Modes

CHS Adressing Modes

In the case of early IDE/ATA hard disks the BIOS provides access to the hard disk through an addressing mode called CHS, where “CHS” stands for “cylinder, head, sector”. CHS addressing starts at (0, 0, 1). In old computer system the maximum amount of addressable data was very limited – due to limitations in both the BIOS and the hard disk interface. Some well-known resulting limits are the 502 MB and the 8.4 GB barriers. The CHS addressing mode was declared obsolete in the ATA-5 standard, replacing it with LBA addressing.

LBA Adressing Modes

Modern hard disks use a recent version of the ATA standard, such as ATA-7. These disks are accessed using a different addressing mode called: logical block addressing or LBA involves a totally new way of addressing sectors. Instead of referring to a cylinder, head and sector number, each sector is instead assigned a unique “sector number”. In essence, the sectors are numbered 0, 1, 2, etc. up to (N-1), where N is the number of sectors on the disk. In order for LBA to work, it must be supported by the disk, the BIOS and operating system. The current 48-bit LBA scheme, introduced in 2003 with the ATA-6 standard, allows addressing up to 144 petabytes (144,000,000 gigabytes)..

The BIOS, working with the system chipset on the motherboard and the system I/O bus, controls which types of modes can be used with the hard disk to actually transfer the data. Originally, systems used the BIOS as an intermediary for every byte of transferred information. Modern operating systems implement direct disk access (Direct Memory Access), and do not use the BIOS subsystems, except at boot load time. A detailed description of the modes of transfer between the hard disk and volatile memory is out of the scope of this page, but the prevalent mode in 2011, UDMA, is a good place to start reading.

Master Boot Record
When an x86 PC is powered on, the BIOS will select a storage device from which to boot. From this device, the BIOS reads a boot sector -  called the Master Boot Record (MBR) – which contains the primary boot loader. The MBR is a 512-byte sector, located in the first sector on the disk (sector 1 of cylinder 0, head 0). This is equal to LBA 0. After the entire MBR is loaded into RAM, the BIOS yields control to it.



MBR Elements
  • The first 446 bytes of the MBR contain executable code – the primary boot loader.
  • The next 64 bytes hold a Partition Table which describes the partitions – or volumes – of a storage device. In this context the boot sector may also be known as a partition sector.
  • The last two bytes of the MBR contain a magic number. If the value is equal to 0xAA55 then the BIOS will assume it is dealing with a valid MBR. If it is not 0xAA55 the BIOS will produce an error message.
Partition Table Entry
The Partition Table contains a maximum of four entries. Each entry specifies a primary partition, but one of the entries can specify an extended patition. Extended partitions are discussed later. The entries have the following format:



The contents of a typical master boot record are displayed below. Note that the code section also contains some human-readable strings – these strings are used by the machine instructions to display messages depending on the code flow at runtime.


EXTENDED PARTITION
Due to the size of the MBR’s partiton table, there is a limit of 4 primary partitions. To get around this issue, engineers came up with a special partition type: the extended partition. A hard disk may contain a maximum of one extended partition. The extended partition can be subdivided into multiple logical partitions. In DOS/Windows systems, each logical partition may then be assigned an additional drive letter. In the MBR’s Partition Table, extended partition entries usually have a Partition Type of either 0x05 or 0x0F; depending upon the size of the disk.

When the operating system encounters an extended partition type in the Partition Table entry, it will use the Starting Sector field to locate the first sector of the extended partition. In that sector it will look for a structure called the Extended Boot Record (EBR). This is a descriptor for a logical partition.

EBRs have exactly the same structure as the MBR; the only difference is that a some fields in the EBR go unused. The only used fields are the first and second Partion Table entries, along with the mandatory boot record signature (or magic number) of 0xAA55 at the end of the sector. This results in the following layout:


So each logical partition in the extended partition is preceded by an EBR, and the EBR’s are chained together. The operating system will follow the chain until it reaches the end. The complete hard disk layout might look something like this:

Note that every filesystem contains a Boot Sector of its own. This is called the Volume Boot Record (VBR). In the boot sequence the MBR is processed first, and eventually the VBR of the partition that contains the OS is processed.
Sources

The DNS Protocol

The DNS PROTOCOL
The Domain Name System protocol translates domain names into IP addresses (Wikibooks page). When a client wants to open a webpage at www.google.com, a query is sent to a DNS server (a.k.a. name server) to fetch the corresponding IP address. The IP returned by the name server is used to contact the Google web server – the server that hosts the actual website contents. In this post we explain the DNS protocol and the packets involved.



Usually, a client will know the IP address of one or more DNS servers after the DHCP boot process is completed. In order to resolve a hostname, a DNS query packet is sent. All DNS traffic between clients and name servers is encapsulated in UDP, and name servers always run on UDP port 53.


The DNS PACKET STRUCTURE

The structure of DNS packets looks like this:


The flags field (16 bits) has the following structure:

a)  The first (0th)bit indicates query(0) or response(1)
    b) Next three bits (1-4) indicates ‘Standard Query (0)’,
       ‘Inverse Query (1)’ and ‘Server Status Request (2)’.
    c) The 5th bit field indicates Authoritative answer. The
       name server is authoritative for the domain in the
       question section.
    d) The 6th bit field is set if message was truncated. With
       UDP this means that the total size of the reply exceeded
       512 bytes and only the first 512 bytes of reply were
       returned.
    e) The 7th bit field indicates Recursion Desired. This bit
       can be set in a query and is returned in the response.
    f) The 8th bit field indicates Recursion Available or not.
    g) The next 3 bits (9-11) have to be 0.
    h) The next 4 bits (12-15) give a return code where
       0 signifies No Error and 3 signifies Name Error.


QUERY PACKET


When a client sends a query to a name server, the Resource Record fields are empty. The Number of Questions field contains the amount of entries in the Questions field, usually 1. Each question consists of:

Name: A string containing the Internet address being queried
Class: The query class, normally the value 1 for Internet (“IN”).
Type: The record type to be queried, the most common types are:






RESPONSE PACKET

A response sent by a name server to a client will contain the original questions in the Questions fields. Additionally the Resource Record fields contain one or more entries. A resource record entry has the following structure:


  • Name: Contains the object, domain or zone name that is the subject of the resource record.
  • Type: One of the available record types.
  • Class: Again, this is normally 1 .
  • TTL: Specifies the number of seconds that the record should be retained in the cache of the device reading the record.
  • RData Length: Indicates the size of the RData field, in bytes.
  • Resource Data: The data portion of the resource record.

The Name Server

 A client that wants to translate a name to an IP address sends a question to the name server(s) on its network. The name server provides an answer to the question, but it might have to contact several other servers to obtain the answer. A lot goes on behind the scenes that the client is blissfully unaware of. A typical scenario where a client looks up abc.company.com looks like this:





  1. The client formulates a question and sends it to the name server, expecting an unambiguous answer. The name server will first search its cache (5), and immediately answer the client if the answer is found.
  2. If the name server does not know the answer itself, it contacts the root name server. The root name server finds out that it does not know the IP for abc.company.com, but it contains an NS type resource record delegating the .com domain to a subordinate name server. So it returns the IP’s for the authoritative name servers for the .com Top Level Domain (TLD).
  3. The name server contacts the authoritative server for the .com domain, which contains an NS type resource record for the company.com domain. It will receive the IP addresses for the authoritative servers for the company.com domain.
  4. Then, the authoritative name server for the company.com domain is contacted, again asking for the IP of abc.company.com. This server then solves the query (or not), and transmits the result back to the name server. Finally, the name server sends the answer to the client.
  5. The name server also caches the information it obtains during the lookup process for future reference.
  6. This process is called iterative lookup, and it the most common approach for name servers that serve clients.






Editing/Compiling Programs on Windows Using MinGW

Editing/Compiling Programs on Windows Using MinGW

So, you are new to C or C++ and you want to figure out how to get started? How to run your code? How to create executables? Well, here is all you need to know.

EDITORS: 
There are quite a few editors for C and C++, and some which even support multiple other languages. It's up to you to figure out which one works best for you. Some of the better ones that I am aware of are:

Notepad++: Fully featured editor which supports a wide variety of languages out of the box.
http://notepad-plus-plus.org/ 

SciTE: Fast and lightweight cross platform editor. Supports many languages and has a nice syntax highlighting scheme.
http://www.scintilla.org/SciTE.html 

Emerald Editor: Another free and easy to use, small editor. This one also supports a wide variety of programming languages and has macro capabilities. It is based on Crimson Editor, which is no longer in development.
http://www.emeraldeditor.com/ 

TextPad: Quite nice, but not completely free. If you want to keep this editor, you should eventually pay for it.
http://www.textpad.com 
You can get the proper syntax highlighting plugin from:
http://www.textpad.com/add-ons/syna2g.html 

Dev-C++: Included only for the reason that it is extremely popular, Dev C++ is not a stand alone editor, but an all out Integrated Development Environment which uses MinGW/GCC. For the purposes of this tutorial, we'll assume you're not using Dev-C++.
http://www.bloodshed.net/devcpp.html 

COMPILING: 

If you want to compile your source code, here is the "best", most straight-forward way. We will be working with GCC, through the Windows MinGW port, probably the most popular cross platform collection of compilers. It include compilers for C, C++, Objective C, Java as well as Ada.

First, what you need to do is download it from here:
http://sourceforge.net/projects/mingw/files/latest/download 

Next, just install it, no differently than you would install any other program. You can keep all the configurations and settings as their default; that should be good enough.

Alright, this is the part that you need to pay attention to. To use the compiler through command prompt, you will need to edit the Windows PATH environmental variable. If you are using Windows 2000 or Windows XP then you have to:

  1. Right click on My Computer from the desktop. 
  2. Select properties. 
  3. Go to advanced. 
  4. Click environmental variables. 
  5. Under system variables select Path 
  6. Click edit. 
  7. Add ; and then where you installed MinGW to. If it was C:/Program Files/MinGW then you would add ;C/Program Files/MinGW/bin to the end of what is already there, do not overwrite what is already there!
Now you're done! To test to see if it worked, and compile programs, open command prompt. If you don't already have a shortcut then it should be somewhere in the accessories folder in the start menu.

When you are already there, you will see the directory which you are already in. Now what you need to do is browse to where the source file is. You will be using DOS style commands, if you are not already familiar with them then:

  • type cd to open a folder and browse it. 
  • type cd .. to go up, 'back' into a more general directory.
  • type dir to see what folders and files are already in the directory


Now, once you are at the location of your source file, to compile it you have to type:


1.    For C++: 
g++ .cpp -o .exe 

2.    For C: 
gcc .c -o .exe 

3.    For Java: 
gcj .java -o .exe


This just specifies which compiler you are using, the source file, and what the resulting executable file will be called.

Straight Line and Accelerated Depreciation Methods


Straight Line Depreciation Method

The simplest and most commonly used method of depreciation is the straight line method.

The straight line depreciation method takes the purchase or acquisition price, subtracts the salvage value and then divides it by the total estimated life in years.

For example, an equipment worth $1m with an estimated life of five years and salvage value of $100,000 would have the following depreciation schedule and asset value after each year as shown below.

Depreciation Expense = (Total Acquisition Cost – Salvage Value) / Useful Life

The characteristics of the straight line method is that the depreciation expense is constant so the valuation of the company is easier as you know how to adjust it if necessary. Plus, it is easy to predict.

FIFO LIFO Inventory Valuation Methods


Inventory Valuation Methods in Accounting

Inventory can make up a large amount of the assets on the balance sheet and so knowing how to analyze the inventory, and the method used by management is crucial.

A large part of stock valuation comes from being able to understand how inventory is valued and built.

To put it in the most basic form, inventory is what you have in stock. If you expand on this definition to look at what is involved on the other side of the scale to get the ending inventory amount, the equation for inventory is

Beginning Inventory + Net Purchases – Cost of Goods Sold = Ending Inventory

In words, your beginning inventory along with your purchases and then subtracting what you have sold, results in ending inventory.

But this is where it gets tricky with GAAP rules. Depending on the inventory valuation  method used by the company, the COGS can vary considerably which ultimately affects the ending inventory.

Sadly, it is not as easy as counting what is left on the shelf at the end of the day to get the ending inventory value.

Three inventory valuation methods are used in the US.

1. Average cost method

2. First In First Out (FIFO) method

3. Last in First Out (LIFO) method




1.    Average Cost Method

To put it real bluntly, the average cost method is rarely used. This method does not offer any real convenience or added accuracy.

The equation for average cost method is as follows.

Average Cost = (Total Quantity of Inventory Units) / (Total Quantity of Units)

where

Cost of Goods Sold = (Average Unit Cost) x (Number of Units Sold)

For example if 1,000 toys are produced on Monday at a cost of $1 and then on Tuesday another 1,000 toys are manufactured at a price of $1.05, the average cost method would value the inventory at $1.025 a piece.



2.    FIFO Method

As mentioned previously on aggressive and conservative accounting policies, the FIFO method of valuing inventory is considered to be the aggressive method.

FIFO works like how you maintain your fridge at home. After you have bought some groceries, you tend to place what you just bought at the back of the fridge in order to finish off the older food before it spoils.

In other words, under FIFO, the oldest goods are sold first and the newest goods are sold last.

As a formula it would look like this

Unit Cost per batch = (Cost/Quantity) for each batch

where

Cost of Goods Sold = (Unit Cost x Quantity) for each batch

Using the toy example above, if 1,000 toys were then sold on Wednesday, the COGS would be $1 per unit. The remaining inventory on the balance sheet would then be worth $1.05 each.



3.    LIFO Method
LIFO is the opposite of FIFO. Instead of the oldest inventory being considered as sold first, the newest product is sold first. While the factory analogy works for the FIFO, consider a bakery. By lunch or evening, the bread baked from the morning will not sell as well as the fresh ones from the afternoon batch.

This means that cost of the latest inventory now becomes the COGS with the cost of the oldest inventory being assigned to the inventory value on the balance sheet.

The equation is essentially the same as FIFO since both are calculated based on batches of unit sold.

Unit Cost per batch = (Cost/Quantity) for each batch

where

Cost of Goods Sold = (Unit Cost x Quantity) for each batch

Using the toy example, the 1,000 units sold on Wednesday would have a COGS of $1.05 per unit, with the remaining 1,000 toys being valued at $1 each.



5.    How Inventory Valuation Affects Profits and Assets

As you can see from above, despite ending with the same 1,000 toys, FIFO assigns the inventory value to be $1,050 compared to the LIFO $1,000.

But another point is that the method of inventory valuation does not just affect the balance sheet. Gross profit also varies considerably. How?

Gross Profit = Sales – COGS

COGS differ under FIFO and LIFO, and if your COGS is low, then that means gross profit will increase.

The table below sums up how each of the three inventory valuations vary.




Financial Statement Analysis

Financial Statement Analysis

 Helping You with Financial Statement Analysis

Accounting is the language of business and to become a successful investor, you also have to think like a businessman.

Warren Buffett said that

“I am a better investor because I am a businessman and a better businessman because I am an investor.”

In other words, business and investing is so closely intertwined that you can not separate it. In order to be a great investor, understanding accounting and performing financial statement analysis is a top priority in your growth as an investor.

I personally struggled a lot with financial statement analysis. Coming from a background with no financial degree or any education in finance, it was difficult to self learn how to analyze financial statements.

But because I went through the hardship of reading many books, magazines, articles and tutorials to improve my knowledge of accounting and the analysis of financial statements, the following articles will make your journey somewhat easier.



1.    How to Master Analyzing the Income Statement

The financial statement is divided into three sections. The income statement is the first part of the financial statement and gets the most attention because it is where the numbers for revenue, net income and earnings per share lives.

This tutorial of the income statement analysis will provide you with insight on how to not just read the income statement, but provide insights on what other points you have to consider as you analyze the income statement in your financial statement analysis.




2.   How to Master Analyzing the Balance Sheet

By understanding the balance sheet, you will be able to gain valuable insight into the financial health of the company. This article uses Circuit City as an example to see why the company went bankrupt.




3.    How to Master Analyzing the Cash Flow Statement

The cash flow statement is a little different than the income statement and balance sheet, because while you can easily compare income statements by quarter, the cash flow statement is more “accrual”. This means that numbers in the cash flow statement add up or accrue throughout the fiscal year.

Best to go read the final section of the financial statement analysis.



4 Timeless Articles on Financial Statement Analysis

1.    Introduction to Analysis of Financial Statements
  • Financial statements enable investors to analyze a company’s fi nancial strength and performance.
  • The income statement measures a period of time, whereas the balance sheet is a snapshot of a single day.
  • All three statements are interlinked, with changes to one ultimately affecting the other two.

2.    The Income Statement: From Net Revenue to Net Income
  • The income statement shows the revenues, expenses and income recognized over a period of time.
  • Companies use accrual accounting to show when revenues and expenses occured, as opposed to when is transferred.
  • Gross and operating margins can help reveal important trends about how a company is performing.

3.    The Balance Sheet: Assets, Debts and Equity
  • The balance sheet lists all of a company’s assets and liabilities at a certain point in time.
  • The proportion of cash, receivables, fixed assets and debt varies by industry.
  • Changes in accounts receivables and accounts payables can provide warning signs about a company or its customers

- Copyright © 2013 Taqi Shah Blogspot -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -