Write a C program that will translate a CHS address into a LBA ad.docx
1. Write a C program that will translate a CHS address into a LBA
address and display it. Take the CHS information from user and
use interrupt 13H/48H to get drive parameters.
Solution
#include <bios.h>
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
//Structure of result buffer used by extended 13H services.
struct RESULTBUFFER
{
unsigned int size;
unsigned int infoflags;
unsigned long int cylinders;
unsigned long int heads;
unsigned long int sectors;
unsigned long int locount;
unsigned long int hicount;
unsigned int bytespersector;
2. unsigned long int configptr;
}rb; //If Int 13 returns some value then it will be stored in rb.
//Function to get drive parameters
void getDriveParameters(unsigned int drive, struct
RESULTBUFFER * rbptr)
{
_AH = 0x48; //Service #
_DL = drive ; //Drive number
rbptr->size = 30; //Size of result buffer
_SI = (unsigned int) rbptr; //Offset address of result buffer
geninterrupt (0x13); //Generate Interrupt 13
}
void main()
{
char input[15];
//Will be used to store current cylender, head and sector
numbers
unsigned long int lba, cylinder, head , sector;
clrscr();
//Taking input from user
puts (" Enter the current Cylinder # : ");
gets (input);
cylinder = atol(input);
3. puts (" Enter the current Head # : ");
gets (input);
head = atol(input);
puts (" Enter the current Sector # : ");
gets (input);
sector = atol(input);
//Getting drive parameters of Result buffer using Int
13H/48H
getDriveParameters(0x80,&rb);
//Now we have No. of heads per cylinder in rb.heads and No.
of Sectors per track for the disk in rb.sectors
//Now from the following formula we can calculate the LBA
index for the selected CHS index
lba=( ( cylinder * rb.heads + head ) * rb.sectors ) + sector -
1;
printf(" The LBA Index for current CHS address :
%ld",lba);
printf(" Press any key to exit...");
getch();
}