I'm trying to create a program where the user enters a digit(more than one digit) and am planning to print the ascii version afterwards. Here's my source code with comments on the INPUT area. The input is a LOOP where it keeps asking the user to enter a number until the USER hits enter then it displays the binary format.. No problems with the displaying of binary format. The code runs fine, using the formula TOTAL = TOTAL * 10 + VALUE I would be able to get all the digits which the user entered. Although When i'm about to display the binary code format, it's all ZEROOOoooooo.. THough i entered is 123 where output should be something like "0000000001111011"

mov cx, 10	; using this as our multiplier we will be able to read decimal input from the program
	xor bx, bx	; bx will hold the total value of the input, initialize it to zero
	
	INPUT_LOOP:
		
		mov ah, 1	; set the input character function
		int 21h	; prompt the user to enter a number from 0 - 9 assuming it is a number
		
		cmp al, 13			; check if the user has pressed the enter key
		je INPUT_LOOP_EXIT	; get out of the loop if the user pressed the enter key
		
		and al, 0fh	; here, we convert the input of the user(hex) to it's binary format
		
		; using the formula TOTAL = 10 * TOTAL + VALUE, we will be able to read an unlimited any number of digit entered
		; In the formula, total is the whole digit itself, 10 is CX, VALUE is the entered input of the user(al)
		mov dl, al	 ; we temporarily move the input of the user to dl
		mov ax, bx ; we move the TOTAL VALUE into ax for multiplication
		
		imul cx	; we multiply 10 * Total value. This is the purpose we have to move BX to ax because ax will be the multiplier		
		add ax, dx	; Expected that after imul has been executed, the product value will automatically be stored in ax, then we add the TOTAL with the VALUE that was entered by the user
		mov bx, ax	; then we move the TOTAL VALUE into BX
		
	jmp INPUT_LOOP
	INPUT_LOOP_EXIT:

But if you're inputting ASCII characters, then you need to subtract 48 (or '0') from each one.

yes i know, I think you mean is that I should convert each of the hex input character into a decimal? yes i did that already using the code and "al, 0fh"

By using code and "al,0fh", u r not subtracting but u r masking the bits this doesn't work fine always because.. when u enter the values 0 to 9 through keyboard it gives 30H to 39H, and 41H to 46H and so on for letters A to F... Z, for small letters a to f .... the value is 61H to 66H so on... So if u r reading from keyboard say letter A u have to first perform the comparison whether the entered value is betn 0 to 9 or A to Z or a to z, then subtract 30H (0 to 9), 37H(A to Z),57H (a-f), the result after this subtraction wil be the character entered by user... while displayin them again if its a 2 or 3 digit number u have to display the digits individually using the reverse process....

This article has been dead for over six months. Start a new discussion instead.