Академический Документы
Профессиональный Документы
Культура Документы
#include <stdio.h>
#include <math.h>
#include <string.h>
void binary_hex(int n, char hex[]);
int hex_binary(char hex[]);
int main()
{
char hex[20],c; int n;
printf("Instructions:\n");
printf("Enter h to convert binary to hexadecimal:\n");
printf("Enter b to hexadecimal number to binary:\n");
printf("Enter a character: "
scanf("%c",&c); if (c 'h'
{
printf("Enter binary number: "
scanf("%d",&n);
binary_hex(n hex
printf("Hexadecimal number: %s"
}
if (c=='b' || c==
{
printf("Enter hexadecimal number: ");
scanf("%s",hex);
printf("Binary number: %d",hex_binary(hex));
}
return 0;
}
void binary_hex(int n, char hex[]) /* Function to convert binary to hexadecimal. */
{ int i=0,decimal=0, rem; while (n!=0)
{
decimal += (n%10)*pow(2,i); n/=10; ++i; } /* At this point, variable decimal contains binary
number in decimal format. */
i=0; while (decimal!=0)
{
rem=decimal%16; switch(rem)
{
case 10: hex[i]='A';
break;
case 11:hex[i]='B';
break;
case 12: hex[i]='C';
break;
case 13: hex[i]='D';
break;
case 14: hex[i]='E';
break;
case 15: hex[i]='F';
break;
default: hex[i]=rem+'0';
break;
}
++i;
decimal/=16;
} hex[i]='\0';
strrev(hex);/* Function to reverse string. */
}
int hex_binary(char hex[]) /* Function to convert hexadecimal to binary. */
{
int i, length, decimal=0, binary
for(length=0;
hex[length]!='\0';
++length);
for(i=0; hex[i]!='\0';
++i, --length)
{
if(hex[i]>='0' && hex[i]<='9')
decimal+=(hex[i]-'0')*pow(16,length-1);
if(hex[i]>='A' && hex[i]<='F')
decimal+=(hex[i]-55)*pow(16,length-1);
if(hex[i]>='a' && hex[i]<='f')
decimal+=(hex[i]-87)*pow(16,length-1);
} /* At this point, variable decimal contains the hexadecimal number in decimal format. */ i=1;
while (decimal!=0)
{
binary+=(decimal%2)*i;
decimal/=2;
i*=10;
}
return binary;
}
(b)Write an assembly language program to accept a decimal number and display its
two's complement representation in binary and hexadeimal formats.
Ans :- [BITS 16]
[SECTION .text]
[ORG 100h]
mov ax,cs ;get code segment
mov ds,ax ;use it now
mov si,0080h ;DOS command line page 0
lodsb ;load size of command line
cmp al,0 ;anything on command line ?
jbe usage ;noo, show usage
cbw ;extend AL to AX
.code
main proc
mov ax,@data
mov ds,ax
start:
lea dx,msg
;user prompt
mov ah,9
int 21h
cmp al,0Dh
;checking if the enter is pressed or not
JE end_input
;if left bracket,then push on stack
cmp al, "["
JE push_data
cmp al, "{"
JE push_data
cmp al, "("
JE push_data
parentheses:
dec cx
cmp cx,0
JL many_right
pop
cmp
JNE
JMP
dx
dl, "("
mismatch
input
curly_braces:
dec cx
cmp cx,0
JL many_right
pop dx
line_bracket:
dec cx
cmp cx, 0
JL many_right
pop dx
cmp dl, "["
JNE mismatch
JMP input
end_input:
cmp cx, 0
JNE many_left
mov ah, 9
lea dx, msg_correct
int 21h
lea dx, msg_continue
int 21h
mov ah,1
int 21h
cmp al, "Y"
JNE exit
JMP start
mismatch:
lea dx, msg_mismatch
mov ah,9
int 21h
JMP start
many_left:
lea dx, msg_left_bracket
mov ah,9
int 2
JMP start
many_right:
lea dx, msg_right_bracket
mov ah,9
int 21h
JMP start
exit:
mov ah,4ch
int 21h
MAIN ENDP
END MAIN
push ax
push bx
push cx
push dx
push si
mov cx,00h
mov bx,0Ah
rpt1: mov dx,00
div bx
add dl,'0'
push dx
inc cx
cmp ax,0Ah
jge rpt1
add al,'0'
mov [si],al
rpt2: pop ax
inc si
mov [si],al
loop rpt2
inc si
mov al,'$'
mov [si],al
pop si
pop dx
pop cx
pop bx
pop ax
ret
hex2asc endp
end
Prg(strrev.asm)
Title reversing a string
dosseg
.model small
.stack
.data
msg1 db 13,10,"Enter a string with dolar symbol as a break:$"
msg2 db 13,10,"Reverse of a string is:$"
strg db 20 DUP(0)
restr db 20 DUP(0)
.code
main proc
mov ax,@data
mov ds,ax
mov es,ax
mov di,00
lea dx,msg1
mov ah,09h
int 21h
read:mov ah,01h
int 21h
cmp al,24h
je next
inc di
mov strg[di],al
jmp read
next: mov si,00
start:cmp di,0
je dmsg2
mov al,strg[di]
mov restr[si],al
inc si
dec di
jmp start
dmsg2:lea dx,msg2
mov ah,09h
int 21h
dis:mov al,restr[di]
cmp al,0
je ou
mov dl,al
mov ah,02h
int 21h
inc di
jmp dis
ou: mov ax,4c00h
int 21h
main endp