# BINARY & HEX I/O. Binary input : read in a binary number from keyboard, followed by a carriage return. character strings of 1’s & 0’ we need to convert.

## Presentation on theme: "BINARY & HEX I/O. Binary input : read in a binary number from keyboard, followed by a carriage return. character strings of 1’s & 0’ we need to convert."— Presentation transcript:

BINARY & HEX I/O

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

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

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

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 :

Binary output: Outputting contents of BX in binary … Shift Operation

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

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

Hex Input Digits “0” to “9” & letters “A” to “F” Followed by a carriage return

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.

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

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

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