Download presentation
Presentation is loading. Please wait.
Published byGerald Pinkerman Modified over 10 years ago
1
BINARY & HEX I/O
2
Binary input : read in a binary number from keyboard, followed by a carriage return. character strings of 1’s & 0’ we need to convert each character to a bit value& collects the bits in a register
3
Algorithm Clear BX….. To hold the binary value Input a character …….. ‘1’ or ‘0’ WHILE character <> CR then Convert character to binary value Left shift BX Insert value into LSB of BX Input a character END_WHILE
4
The algorithm assumes Input characters are either “0”,”1”or CR At most 16 bit are input BX is shifted left to make room and the OR operation is used to insert the new bit into BX
5
Code XORBX,BX; clear BX MOVAH,1 ; input character function INT21H; read a character WHILE_: CMPAL,0DH ; CR ? JEEND_WHILE ; yes, done ANDAL,0FH ; no, convert to binary value SHLBX,1; make room for new value ORBL,AL; put value in BX INT21H ; read a character JMPWHILE_; loop back END_WHILE :
6
Binary output: Outputting contents of BX in binary … Shift Operation
7
Algorithm FOR 16 times DO Rotate left BX /* BX … output CF … MSB */ IF CF = 1 THEN output ‘1’ ELSE output ‘0’ END_IF END_FOR
8
Binary output MOV AH, 2 MOV CX, 16 TOP: ROL BL,1 JC ONE JNC ZERO ONE: MOV DL,'1‘ ; or ( 31h) JMP DISPLAY ZERO: MOV DL,'0‘ ; or (30h ) DISPLAY: INT 21H LOOP TOP
9
Hex Input Digits “0” to “9” & letters “A” to “F” Followed by a carriage return
10
Assume Only upper case letters are used. The user inputs no more than 4 hex characters. BX must be shifted 4 times to make room for a hex value.
11
Algorithm for hex input Clear BX Input a hex character WHILE character <> CR DO Convert character to binary value Left shift BX 4 times Insert value into lower 4 bits of BX Input a character END_WHILE
12
Code XORBX,BX ; clear BX MOV CL,4 MOVAH,1 ; input character function INT21H ; read a character WHILE_: CMPAL,0DH; CR ? JEEND_WHILE; yes, done ; convert character to binary digit CMPAL,39H ; a digit ? JGLETTER ; no, a letter ; input is a digit ANDAL,0FH ; convert digit to binary value JMPSHIFT ; go to insert in BX
13
Code LETTER: SUBAL, 37H ; convert letter to binary value SHIFT: SHLBX,CL ;make room for new value ; insert value into BX ORBL,AL ; put value into low 4 bits of BX INT21H ; input a character JMPWHILE_ ; loop until CR END_WHILE:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.