Mcsl 017 Solved Asignment
-
Upload
nitin-nilesh -
Category
Documents
-
view
18 -
download
0
description
Transcript of Mcsl 017 Solved Asignment
-
Institute of core education
Section 1: C Programming Lab Qustion 1: Write a program that takes a decimal number and converts it into binary, octal and
hexadecimal equivalents. Your program should have functions for each type of
conversion. These functions should implement algorithms to perform these conversions.
Predefined functions OR %x and %o codes are not allowed. The output of the
programme should be stored in a text file named ourput.txt. Note: You must execute the
program and submit the program logic, sample input and output along with the
necessary documentation for this question Assumptions can be made wherever
necessary.
Ans :-
#include
#include
#include
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' || c=='H')
{
printf("Enter binary number: ");
scanf("%d",&n);
binary_hex(n,hex);
printf("Hexadecimal number: %s",hex);
}
if (c=='b' || c=='B')
{
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)
-
Institute of core education
{
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=0;
for(length=0;
hex[length]!='\0';
++length);
for(i=0; hex[i]!='\0';
++i, --length)
{
if(hex[i]>='0' && hex[i]='A' && hex[i]='a' && hex[i]
-
Institute of core education
decimal/=2;
i*=10;
}
return binary;
}
Section 2: Assembly Language Programming Lab
Question 1: (a) Write a program in assembly language to calculate the average of numbers in an array.
Ans :- DATA SEGMENT A DB 1,2,3,4,5,6,7,8,9,10
SUM DB ?
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA,CS:CODE
START:
MOV AX,DATA
MOV DS,AX
LEA BX,A
MOV CL,10
MOV AX,0000
L1:ADD AL,BYTE PTR[BX]
INC BX
DEC CL
CMP CL,00
JNZ L1
MOV SUM,AL
MOV BH,10
DIV BH
MOV AH,4CH
INT 21H
CODE ENDS
END START
(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
-
Institute of core education
xchg bx,ax ;swap size to bx for indexing mov byte [bx+si],0 ;null terminate command line call parse ;parse command line jmp main ;go on with main usage: mov bx,utext ;pointer usage text jmp errout ;skip this main: xor eax,eax ;wanna clean registers xor ebx,ebx xor ecx,ecx xor edx,edx ;calculate length of 1:st arg mov si,inbuff ;get input buffer get0: lodsb ;load AL cmp al,0 ;end of arg ? je got0 ;yeahh inc cx ;noo, increase length count jmp get0 ;check next got0: ;first arg len in CX now mov si,inbuff ;get command line dump: lodsb cmp al,0 ;second command ? jne dump ;noo lodsb and al,0dfh ;force upper case cmp al,'H' ;hex input ? je dohex cmp al,'D' ;decimal ? je dodec cmp al,'B' ;binary ? jne usage ;noo, let us know we're wrong ;check for valid BIN input mov si,inbuff ;command line buffer mov bx,errt1 ;error text isbin: lodsb ;load AL cmp al,'0' ;a zero ? je bindo ;yeahh cmp al,'1' ;a 1 ? je bindo ;yeahh jmp errout ;noo, not binary input, skip this bindo loop isbin call binascbin ;make ascii bin jmp output ;do the output job dohex: mov si,inbuff ;check for valid HEX input mov bx,errt2 ;proper text
-
Institute of core education
ishex: lodsb ;get the char cmp al,'0' jb errout and al,0dfh ;force UPPERCASE cmp al,'F' ;>F ? ja errout ;yeahh, dump this loop ishex call hexbin ;make hex bin jmp output dodec: mov si,inbuff ;check for valid DEC input mov bx,errt3 isdec: lodsb cmp al,'9' ;>9 ? ja errout ;yeahh, skip loop isdec lodsb call decbin ;make dec bin output: call showit ;show the stuff jmp quit errout: call write ;land here if error quit: mov ax,exit int msdos
(c) Write an assembly language program to implement a stack.
Ans :- .model small .stack 100h
.data
cr equ 0DH ; cr represents carriage return
lf equ 0AH ; lf represents line feed
msg DB cr,lf,'ENTER AN ALGEBRIC EXPRESSION : ',cr,lf,'$'
msg_correct DB cr,lf,'EXPRESSION IS CORRECT.$'
msg_left_bracket DB cr,lf,'TOO MANY LEFT BRACKETS. BEGIN
AGAIN!',cr,lf,'$'
msg_right_bracket DB cr,lf,'TOO MANY RIGHT BRACKETS. BEGIN
AGAIN!',cr,lf,'$'
msg_mismatch DB cr,lf,'BRACKET MISMATCH. BEGIN AGAIN!',cr,lf,'$'
msg_continue DB cr,lf,'Type Y if you want to Continue : ',cr,lf,'$'
.code
main proc
-
Institute of core education
mov ax,@data ;get data segment
mov ds,ax ;initialising
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
;if right bracket,then pop stack
cmp al, ")" JE parentheses cmp al, "}" JE curly_braces cmp al, "]" JE line_bracket jmp input
push_data: push ax inc cx jmp input
parentheses: dec cx cmp cx,0 JL many_right
pop dx cmp dl, "(" JNE mismatch JMP input
curly_braces: dec cx cmp cx,0 JL many_right pop dx
-
Institute of core education
cmp dl, "{" JNE mismatch JMP input
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 21h JMP start
many_right: lea dx, msg_right_bracket mov ah,9 int 21h
-
Institute of core education
JMP start exit: mov ah,4ch int 21h
MAIN ENDP END MAIN
(d) Write a Program in assembly language that has two subroutines: One for encrypting alphabets of a string and second for decrypting the encoded string. In Encryption, simply convert a character /number into its predefined numerical/character value. Decryption is a reverse process of encryption. Write suitable Main program in C that calls these function. Test your program suitably.
Ans :- int 21h
read1:mov si,offset chr mov ah,01h int 21h mov [si],al inc si mov al,24h mov [si],al mov bx,00 mov si,offset buf mov ax,00 mov di,offset chr
check:mov al,[si] cmp al,[di] je count cmp al,'$' je dis inc si jmp check
count:inc bl inc si jmp check dis:mov strlen,bl lea si,res mov ax,00 mov al,strlen call hex2asc lea dx,msg3 mov ah,09h int 21h lea dx,res mov ah,09h int 21h ou:mov ax,4c00h int 21h main endp hex2asc proc near
-
Institute of core education
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
-
Institute of core education
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