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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subroutines problem

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
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)
 
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."
 
that wasn't any help! i need code ...
or at least give me the code for 8-bit integer input?
 
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."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top