Difference between revisions of "ASCII2Binary"

From MKWiki
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
<pre>
+
'''<pre>
'''; Program to convert ASCII character to Binary equivalent'''
+
; Program to convert ASCII character to Binary equivalent
</pre>
+
</pre>'''
 
1. Define the model and architecture for the assembly code
 
1. Define the model and architecture for the assembly code
 
<pre>
 
<pre>
Line 17: Line 17:
 
3. Define your instruction in the code section (here you write the assembly instructions that perform computations, control program flow, and interact with data variables and memory)
 
3. Define your instruction in the code section (here you write the assembly instructions that perform computations, control program flow, and interact with data variables and memory)
 
<pre>
 
<pre>
; print the input string
+
.code
 +
.startup
 
 
 +
        ; print the input string
 
MOV DX, OFFSET inputStr   
 
MOV DX, OFFSET inputStr   
 
MOV AH, 09H
 
MOV AH, 09H
 
INT 21H
 
INT 21H
  
;read a character from the standard input
+
;read a character from the standard input
 
 
MOV AH, 01H
 
MOV AH, 01H
 
INT 21H
 
INT 21H
Line 31: Line 32:
  
 
 
MOV BL,AL    ; binary equivalent of ASCII character is moved to BL  
+
MOV BL, AL    ; binary equivalent of ASCII character is moved to BL  
 
 
 
 
 
; print the output string
 
; print the output string
MOV DX, OFFSET inputStr    
+
MOV DX, OFFSET outputStr    
 
MOV AH, 09H
 
MOV AH, 09H
 
INT 21H
 
INT 21H
Line 63: Line 64:
  
 
END
 
END
 
 
</pre>
 
</pre>

Latest revision as of 23:52, 16 October 2023

; Program to convert ASCII character to Binary equivalent

1. Define the model and architecture for the assembly code

.model SMALL
.stack 100H

2. Define your data in the data section ( here you can define various data items: variables, constants, strings, arrays)

.data
	inputStr db 10,13, 'Enter an ASCII Character: $'
	outputStr db 10,13, 'Binary equivalent is : $'

3. Define your instruction in the code section (here you write the assembly instructions that perform computations, control program flow, and interact with data variables and memory)

.code
.startup
	
        ; print the input string	
	MOV DX, OFFSET inputStr   
	MOV AH, 09H
	INT 21H

	;read a character from the standard input	
	MOV AH, 01H
	INT 21H

	;at this point whatever character is entered by the user, its binary equivalent(8-bits) of the ASCII code is stored in AL

	
	MOV BL, AL    ; binary equivalent of ASCII character is moved to BL 
	
	
	; print the output string
	MOV DX, OFFSET outputStr   
	MOV AH, 09H
	INT 21H

	; Now left shift the bits, to extract the MSB and print it (repeat the shifting and extracting until every bit is printed)

	MOV CX, 8	
		
	repeat8Times:
		SHL BL, 1
		JC printOne	; Jump if carry generated
	
		MOV DL, 30H 	; ASCCI code for '0' is 30H, which is 48 in decimal	
		JMP print
	
		printOne:
			MOV DL, 31H
			
		print:
			MOV AH, 02H
			INT 21H
		LOOP repeat8Times		
	
	; exit from the program	
	MOV AH, 4CH
        INT 21H

END