×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Subroutines problem

 Forum Search FAQs Links MVPs

## Subroutines problem

(OP)
how do i read an 8-bit integer n from the keyboard and than
read 16-bits integer X from the keyboard. then calculate the sum s = x/1 + x/2 + x/3 + x/4 ....+ x/n; (using quotient only)

### RE: Subroutines problem

Use C

Just kidding.

First, all inputs are in strings

Second, learn how to convert strings to integers.

Third, learn how to loop.

Fourth, have fun.

Fifth, good luck!

"Information has a tendency to be free.  Which means someone will always tell you something you don't want to know."

### RE: Subroutines problem

(OP)
that wasn't any help! i need code ...
or at least give me the code for 8-bit integer input?

### RE: Subroutines problem

Like I said:

All inputs are in strings of characters

ALWAYS

SO: First, learn to convert a string representation of a number to an integer

When a user enters the number 123, your program will ALWAYS get the following numbers:

31h, 32h, 33h

Which are the ASCII representations of the string of numbers that he input.

The quick and dirty way to convert a string to a number is to AND the ASCII inputs with 0fh.

But you would still have three numbers: 1h, 2h, and 3h.

So next, learn to multiply by 10

So here's the loop:

number=0
loop1:
Get a character
check if carriage return or termination, if so get out
AND char with 0fh
number=number*10
add char to number
loop again

That's pseudo code.  If you can't convert pseudo code to actual code, I suggest you try something easier.

That pseudo code will work with n-bits.

Also:  If you use the OS's get a character function, then you won't be able to use a backspace feature.  If you want, use your OS's string input function, then replace the Get a Character line with something like:

mov char,StringBuff[index]
index++

And of course clear the index before looping.

As for the second part where you have to divide: Division is very bad for the processor.  I can't understand why I get division errors so often.  I try to avoid division because I crash my computer too much with it.  'Course you can do multiplication by reciprocal.

n/d = ( n * (10000h / d) ) /10000h = ( n * (10000h / d) ) >> 16

For example:  to divide by 2, multiply a 16-bit number by 8000h (That is 10000h / 2).  This results in a 32-bit number.  Get the high 16-bits, which is the result you would get  if you divided.  Multiplication is faster after all.

BUT how would you get the value 10000h/d?  You precalculate it!  Since the divisor goes only up to 0ffh, you can just bring out your calculator and divide 10000h by all values of d up to 255, and store them in a table.  It's just a couple of hour's work...

But that's rather complicated.  You can just try to use idiv which I think is more stable than div.

"Information has a tendency to be free.  Which means someone will always tell you something you don't want to know."

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

#### Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!