Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Integer Input between a desired range 1

Status
Not open for further replies.

NinJJa73

Technical User
Jan 1, 2006
4
GB
Hi,

Let me first say that i am very new to assembly language, and finding it to be a bit out of my depth but anyway heres the problem:
I'm currently in the process of writing a program in 8086 assembly language but i'm really stuck.
I need to be able to get an integer input from the keyboard(user) within the range of 0-255.

I've tried all kinds of ways and wasted many hours getting more confused.
Basically it would be great if someone could show me some example code or the simplest steps to go through to help solve the problem.

Any help or advice would be greatly appreciated
thanks
 
Not tested (and crude):

errormessage db "Number is too large$"

xor bx, bx
getkey:
mov ah, 1
int 21h
cmp al, 13
je done
cmp al, 30h
jb getkey
cmp al, 39h
ja getkey
and al, 30h
xor ah, ah
add bx, ax
jmp short getkey
done:
or bh, bh
jz numberok
lea dx, errormessage
mov ah, 9
int 21h
jmp exit
numberok:
;bl contains the number for you to do what you want with
exit:
;exit program or return from proc or whatever

Hope this helps.

[vampire][bat]
 
thank you so much [thumbsup2], i'm now starting to see how it works. I can understand how almost all the code works but if its not too much of a bother how would i go about checking if the inputted number is between 0-255 as the current code works between a larger range of numbers. Am i right in thinking that it would involve storing the number using a 8 bit variable and checking whether overflow has occurred?

thanks for any advice given
 
First, thanks for the star.

Second I didn't deserve it - having looked at my code again, I see that it was wrong.

Hope this is a better attempt:

Code:
DigitCount	db	?
Result		db	?
BadCharacter	db	"Invalid character$"
TooLarge	db	"Number too large$"

	mov	[DigitCount], 0
	mov	[Result], 0
	
	xor	bx, bx			;to hold interim result
	mov	cl, 10			;we need to multiply interim result by 10
GetDigit:
	or	bh, bh
	jnz	ShowTooLargeError
	mov	ah, 1
	int	21h
	cmp	al, 13
	je	Done
	cmp	al, 30h
	jb	ShowDigitError
	cmp	al, 39h
	ja	ShowDigitError
	sub	al, 30
	xor	ah, ah
	cmp	[DigitCount], 0
	jz	AddDigit
	push	ax
	mov	ax, bx
	mul	cl
	mov	bx, ax
	pop	ax
AddDigit:
	add	bx, ax
	inc	[DigitCount]
	jmp	short GetDigit
ShowDigitError:
	lea	dx, BadCharacter
	mov	ah, 9
	int	21h
	;xor	bx, bx			;possibly reset interim number to start again
	jmp	short GetDigit
ShowTooLargeError
	lea	dx, TooLarge
	mov	ah, 9
	int	21h
	;xor	bx, bx			;possibly reset interim number to start again
	jmp	short GetDigit
Done:
	mov	[result], bl
	;do what you want with the number

This also gives you better checking than before and more acurately restricts the range.

Hope this helps.



[vampire][bat]
 
Thanks again, 1 last question if the user inputs say a 3 digit number such as 213 does this mean I have to change cl to 100 to get the correct value for the hundreds column e.g.
2 x 100
1 x 10
3 x 1
then add the 3 values (200,10,3)?

reason I ask is at the moment i know mostly the basic Assembly language commands and not sure whether there are quicker ways to go about it.
 
No, because you are indirectly multiplying the accumulated value in BL by 10.

User inputs:
2, BL = 2
1, BL = (BL * 10) = 20, + 1 = 21
3, BL = (BL * 10) = (21 * 10) = 210, + 3 = 213

The value in BL is temporarily move to AL to do the multiplication.


Hope this helps.




[vampire][bat]
 
There probably is a quicker way of doing it. I used to program extensively in Assembler, but haven't done so for many years and am rather rusty.

[vampire][bat]
 
Ok, everything is sorted. I've been through all the code and can understand what each line does now [2thumbsup]. I think i've learnt more in the last couple of days then the last couple months beforehand.

I can't thank you enough for the help it is much appreciated. :)
 
Glad to have been of help, and once again sorry if my first reply led you in the wrong direction

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top