## Subroutines problem

## Subroutines problem

**Guest**(visitor)

(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)

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

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

Guest(visitor)or at least give me the code for 8-bit integer input?

## RE: Subroutines problem

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."