-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hex2Bin.c
146 lines (90 loc) · 2.85 KB
/
Hex2Bin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Name: D.Sai Nandan
Regd.No: 19006
*/
#include<stdio.h>
//Defining some constants...
//1. Lengths of the instructions in different formats
#define HEX_LEN 6
#define BIN_LEN 24
//2. Lengts of the various components of an instrucion in binary format
#define OP_LEN 8
#define ADDRESS_LEN 15
//function to convert a decimal to binary...(details are given below)
void Dec2Bin(int,int[],int);
//the 'main' program
void main(){
//Declaring the variables...
int i;
unsigned int hex[HEX_LEN],HexOp;
int BinOp[BIN_LEN];
int Opcode[OP_LEN];
int Mode;
int Address[ADDRESS_LEN];
//Initializing the BinOp and hex arrays...
for(i=0;i<BIN_LEN;i++)
BinOp[i]=0;
for(i=0;i<HEX_LEN;i++)
hex[i]=0;
//Taking input...
printf("Enter the hexadecimal instruction: ");
scanf("%x",&HexOp);
//the 'main' part of the main() function
//loop to obtain the digits of the hexadecimal instruction.
i=HEX_LEN;
while(HexOp!=0){
hex[--i]=HexOp%16;
HexOp/=16;
}
//loop to convert the hexadecimal digits to their binary form
for(i=0;i<HEX_LEN;i++)
Dec2Bin((int)hex[i],BinOp,(i+1)*4);
//Display the output...
//1. Binary form of the instruction.
printf("Binary representation: ");
for(i=0;i<BIN_LEN;i++){
if(i&&i%4==0)
printf(" ");
printf("%d",BinOp[i]);
}
printf("\n");
//2. Opcode.
printf("Opcode: ");
for(i=0;i<OP_LEN;i++){
Opcode[i]=BinOp[i];
printf("%d",Opcode[i]);
}
printf("\n");
//3. Mode of addressing.
printf("Mode: ");
Mode=BinOp[OP_LEN];
if(Mode)
printf("Indexed\n");
else
printf("Not Indexed\n");
//4. The address.
printf("Address: ");
for(i=OP_LEN+1;i<BIN_LEN;i++){
Address[i-(OP_LEN+1)]=BinOp[i];
printf("%d",Address[i-(OP_LEN+1)]);
}
printf("\n");
} //end of main()
//////////////////////////////////////////////////// FUNCTIONS ////////////////////////////////////////////////////////////////
/*
Description:
This function, essentially, converts a decimal number to its binary form.
But, here, it's written in the context of converting a hexadecimal number to its binary form.
So each hexadecimal digit is converted to its decimal form and the decimal is then converted to its binary form,
and the binary forms of successive hexadecimal deigits are appended to the BinOp (or, here, bin) array.
Pre: dec=decimal number
bin[]=array to store binary forms
index=an integer used for appending the resultant binary form (to bin[])
Post: the function returns void
bin[] array is updated with the binary form a decimal number
*/
void Dec2Bin(int dec, int bin[], int index){
while(dec!=0){
bin[--index]=dec%2;
dec/=2;
}
}