-2

I use TASM, I am trying to input 2 number greater than 9 and calculate sum of this numbers and after print the numbersand the sum simply ;

like that :

Number1=109 Number2=90 sum=199

I want how to converted this number and how to calculate sum. I use that code for input an number if greater than 9. Thanks

MOV Numbre1,0
lecture: int 21h ; 
Cmp AL, "0"
JB fin
Cmp AL,"9"
JA fin
Sub AL,"0" 
SHL Numbre1,1 
Mov BL,Numbre1
SHL BL,1
Add Numbre1,BL
Add Numbre1,AL
jmp lecture
Sachin Prasad
  • 5,365
  • 12
  • 54
  • 101

1 Answers1

0

This has gotta be the most frequently asked question of all time. If you can't find an example, upgrade your search skills.

From what you've got... you're going to need a subfunction in al(edit: ah!) for that int 21h. You check for a valid decimal digit correctly. The shl isn't going to help you with a decimal number (but possibly useful for hex).

To convert from text to a number, which is what your code appears to be doing, zero out a register for "result so far". Each time you find a valid decimal digit (working left to right), subtract '0' (30h or 48 decimal, but '0' is clearer to read), multiply "result so far" by ten, and add the new digit. Until done...

To convert from a number to text so you can print it, which your question actually asks, divide by ten repeatedly - pay attention to what div divides by what! The remainders (in ah, dx, or edx, depending) are the decimal digits. Add '0' to each. You'll get the digits from right-to-left, and you want to print 'em left-to-right - there are several ways to deal with this...

Find an example, or fire up a debugger and work it out. The most likely reason your program will crash is that you haven't paid attention to what div divides by what! Ask if you have a specific problem...

Edit: Sigh...

mov ax, the_number
xor cx, cx ; digit counter
mov bx, 10
pushem:
xor dx, dx ; important!
div bx ; quotient in ax, remainder in dx
add dl, '0' ; convert to character
push dx  ; save 'em to get back in right order
inc cx ; count 'em
test ax, ax  ; done?
jnz pushem ; no, do more
popem:
pop ax ; we're only interested in al
int 29h ; print al
loop popem  ; until done

That's about the simplest way I know (not the "best" perhaps). Your current set of numbers will fit in a byte, but it's just as easy to do a full word.

Frank Kotler
  • 1,462
  • 1
  • 8
  • 4