0% found this document useful (0 votes)
3 views9 pages

Convert Hex, Decimal to Binary Program

The document contains multiple programming experiments in C and 8086 Assembly language, focusing on number conversions, arithmetic operations, and algorithms like Booth's and Restoring Division. Each experiment includes code snippets for tasks such as converting hexadecimal and decimal to binary, calculating 2's complement, performing arithmetic operations, and drawing shapes. The document serves as a practical guide for implementing these concepts in programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Convert Hex, Decimal to Binary Program

The document contains multiple programming experiments in C and 8086 Assembly language, focusing on number conversions, arithmetic operations, and algorithms like Booth's and Restoring Division. Each experiment includes code snippets for tasks such as converting hexadecimal and decimal to binary, calculating 2's complement, performing arithmetic operations, and drawing shapes. The document serves as a practical guide for implementing these concepts in programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXP 1: To Implement a program to convert hexadecimal, decimal number to binary

number
Program Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int choice;
char hex[100];
int dec;
clrscr();
printf("[Link] hexadecimal to binary\n");
printf("[Link] decimal to binary\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter a hexadecimal number:");
scanf("%s",&hex);
hexatobin(hex);
break;
case 2:
printf("Enter a decimal number:\n");
scanf("%d",&dec);
dectobin(dec);
break;
default:
printf("Invalid choice!!!");
}
getch();
return 0;
}
int hexatobin(char hex[])
{
int i;
printf("Binary:");
for(i=0;hex[i]!='\0';i++)
{
switch(hex[i])
{
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'A':
printf("1010");
break;
case 'B':
printf("1011");
break;
case 'C':
printf("1100");
break;
case 'D':
printf("1101");
break;
case 'E':
printf("1110");
break;
case 'F':
printf("1111");
break;
default:
printf("Invalid input");
break;
}}
return 0;
}
int dectobin(int dec)
{
int bin[32],i=0,j;
printf("Enter decimal number:");
scanf("%d",&dec);
if(dec==0)
{
printf("binary number:0");
return 0;
}
while(dec>0)
{
bin[i]=dec%2;
dec=dec/2;
i++;
}
printf("Binary: ");
for(j=i-1;j>=0;j--)
{
printf("%d",bin[j]);
}
return 0;
}

EXP 2:To implement a program to obtain 2’s complement of a number


Program Code:
#include<stdio.h>
#include<conio.h>
int main()
{
char bin[50],i,len;
clrscr();
printf("\nEnter a binary number:");
scanf("%s",&bin);
len=strlen(bin);
for(i=0;i<len;i++)
{
if(bin[i]=='0')
bin[i]='1';
else
bin[i]='0';
}
for(i=len-1;i>=0;i++)
{
if(bin[i]=='0')
{
bin[i]='1';
break;
}
else
bin[i]='0';
}printf("2's complement: %s",bin);
getch();
return 0;
}

EXP 4:To implement 8086 assembly language program for performing various
Arithmetic operations.
Program for Addition:
org 100h
MOV AL,02H
MOV BL,06H
ADD AL,BL
ret
Program for Subtraction:
org 100h
MOV AL,0AH
MOV BL,06H
SUB AL,BL
ret
Program for Multiplication:
org 100h
MOV AL,03H
MOV BL,04H
MUL BL
ret
Program for Division:
org 100h
MOV AL,0AH
MOV BL,02H
DIV BL
Ret

EXP 5:To implement Booth’s Algorithm using C for the multiplication of two signed
binary numbers.
Program Code:
#include <stdio.h>
#include <conio.h>
void printBinary(int n, int bits) {
int i;
for (i = bits- 1; i >= 0; i--)
printf("%d", (n >> i) & 1);
}
int main() {
int M, Q;
int A = 0;
int Q_1 = 0;
int count = 4;
int origM, origQ, Q0, sign, combined, result;
int decimalResult;
clrscr();
printf("Enter multiplicand (M): ");
scanf("%d", &M);
printf("Enter multiplier (Q): ");
scanf("%d", &Q);
origM = M;
origQ = Q;
while (count > 0) {
Q0 =Q&1;
if (Q0 == 1 && Q_1==0)
A=A-M;
else if (Q0 == 0 && Q_1 == 1)
A=A+M;
A=A&0xF;
sign = (A >> 3) & 1;
combined = (A << 4) | (Q & 0xF);
combined = (combined >> 1) | (sign << 7);
A=(combined >> 4) & 0xF;
Q=combined &0xF;
Q_1 =Q0;
count--;
}
result = ((A & 0xF) << 4) | (Q & 0xF);
decimalResult = result;
if (decimalResult & 0x80)
decimalResult-= 256;
printf("\nMultiplicand (M): ");
printBinary(origM & 0xF, 4);
printf(" (%d)", origM);
printf("\nMultiplier (Q): ");
printBinary(origQ & 0xF, 4);
printf(" (%d)", origQ);
printf("\n\nFinal Result (Binary): ");
printBinary(result, 8);
printf("\nFinal Result (Decimal): %d\n", decimalResult);
getch();
return 0;
}
EXP 6:To implement the Restoring Division Algorithm in C language and calculate
the quotient
and remainder for two unsigned integers
Program Code:
#include <stdio.h>
void displayBinary(int n, int bits) {
int i;
for (i = bits- 1; i >= 0; i--)
printf("%d", (n >> i) & 1);
}
int main() {
int Q = 13;
int M = 3;
int A = 0;
int n = 4, i;
printf("Initial values:\n");
printf("A = "); displayBinary(A, n); printf("\n");
printf("Q = "); displayBinary(Q, n); printf("\n");
printf("M = "); displayBinary(M, n); printf("\n\n");
for (i = 0; i < n; i++) {
A=(A<<1)|((Q >> (n- 1)) &1);
Q=(Q<<1)&((1 <<n)- 1);
printf("Step %d:\nAfter left shift: A=", i + 1);
displayBinary(A, n);
printf(" Q="); displayBinary(Q, n); printf("\n");
A=A-M;
printf("After A = A- M: A=");
displayBinary(A & ((1 << n)- 1), n);
printf("\n");
if (A < 0) {
A=A+M;
} else {
Q=Q|1;
}
printf("After check: A=");
displayBinary(A & ((1 << n)- 1), n);
printf(" Q=");
displayBinary(Q, n);
printf("\n\n");
}
printf("Final Result:\n");
printf("Quotient (Q) = "); displayBinary(Q, n); printf(" (Decimal %d)\n", Q);
printf("Remainder (A) = "); displayBinary(A & ((1 << n)- 1), n); printf("
(Decimal %d)\n",
A);
getch();
return 0;
}

EXP 7:To implement an 8086 Assembly Language program to perform various


logical operations
such as AND, OR, XOR, and NOT.
Program for AND Operation:
org 100h
MOV AL,0AH
MOV BL,06H
AND AL,BL
ret
Program for OR Operation:
org 100h
MOV AL,0AH
MOV BL,06H
OR AL,BL
ret
Program for XOR Operation:
org 100h
MOV AL,0AH
MOV BL,06H
XOR AL,BL
ret

Program for NOT Operation:


org 100h
MOV AL,0AH
NOT AL
Ret

EXP 8:To Implement 8086 assembly language program for performing Drawing
Square using 10H
Program Code:
org 100h
MOV AX,0012H
INT 10H
MOV BX,60H
MOV CX,120H
MOV DX,60H
MOV AL,02H
L1:MOV AH,0CH
INC CX
DEC BX
INT 10H
JNZ L1
MOV BX,60H
MOV AL,02H
L2:MOV AH,0CH
INC DX
DEC BX
INT 10H
JNZ L2
MOV BX,60H
MOV AL,02H
L3:MOV AH,0CH
DEC CX
DEC BX
INT 10H
JNZ L3
MOV BX,60H
MOV AL,02H
L4:MOV AH,0CH
DEC DX
DEC BX
INT 10H
JNZ L4
Ret

EXP 9:Implement a program to Display Character A to Z in Upper case and Lower


case using INT 21H
Program:
org 100h
MOV AH,02H
MOV DL,'A'
MOV CX,26
L1: INT 21H
INC DL
DEC CX
JNZ L1
MOVDL,0AH
INT 21H
MOV DL,0AH
INT 21H
MOV DL,0DH
INT 21H
MOV DL,'a'
MOV CX,26
L2:INT 21H
INC DL
DEC CX
JNZ L2
Ret

You might also like