Re: Need Help.
- From: "MQ" <spamtrap@xxxxxxxxxx>
- Date: 11 Jul 2006 05:14:39 -0700
Frank Kotler wrote:
spamtrap@xxxxxxxxxx wrote:
....
Am wondering if any can help me on making up a simple multiplication
program
Mmmm... as you might suspect, you'll be using the "mul" instruction. But
wait a second! What answer does your addition program give you for 5 +
5? What about 2 - 3? You've got the right idea - subtract 30h (or 48 or
'0') from a character to change it to a number, do your calculation, and
add 30h to the number to make it a character before displaying it. Works
great for a single digit.
When your answer runs into multiple digits... Perhaps your course
materials provide a macro or subroutine for "print_number" or
"print_int" or something? If not, you'll need to write one. (or find
one) For that, you'll want to skip past the "mul" instruction to the
"div" instruction. "div" doesn't provide an answer with a decimal point
in it, of course (use fdiv for that... you think *these* numbers are
hard to display?), but gives a quotient and remainder. The remainders
are of interest to us.
Suppose we have 123, and divide by ten - quotient 12, remainder 3.
Divide the quotient, 12, by ten again - quotient 1, remainder 2, Do it
again - quotient 0, remainder 1. When the quotient is zero, we're done.
The remainders are the digits we want to print - after adding 30h/48/'0'
- but we want to print the last one first! One solution would be to push
'em on the stack as we get 'em, and pop 'em back off to print 'em.
Another way would be to store 'em in a string, starting at the "end" and
working back toward the beginning.
I think we've seen this assignment before, and single-digit inputs are
okay. If you want to accept multiple digit numbers as input... zero a
register for the result, get a character and subtract 30h. Multiply the
result so far by ten, and add in this digit. Get another character and
repeat until the user hits "enter". Better check to make sure they're
giving you real digits - those users will enter any old thing, y'know!
And don't let 'em overflow a byte, or word, or whatever size you're
using. I don't think you need to worry about that now.
Once you can display a correct answer all the way up to 9 + 9, I think
you'll find it easy to extend it to multiplication. Just "mul ch1" when
you've got the second number in al. The answer will be in ax, but 9 * 9
fits in a byte, so you'll only need to display al.
Dealing with a negative result from a subtraction...
sub al, ch1
jnc positive ; carry (borrow) flag set, if negative
print a '-' - don't trash your number!
neg al
positive:
display as usual
I think you move the cursor to the same place twice, and the "int 20h"
is never reached. That's just "lint" - does no harm - better than not
doing it at all - but you might want to clean it up before you
cut-and-paste it around too much more. :)
If you have trouble with the multiplication part, get back to us, but I
think you can get it. You're off to a good start.
Best,
Frank
Seriously, do you really believe that they even wrote the addition and
subtraction code. I also have seen this before. The assignment
usually *gives* you the code for addition and subtraction, and the
student has to adapt this code to handle multiplication or division,
etc.
And even if we assume that they did write the code, wouldn't you think,
having gotten this far, they would have the nouse to work out
multiplication?
Helping people who don't help themselves are flooding the industry with
incompetents, and it hurts our profession...
.
- References:
- Need Help.
- From: spamtrap
- Re: Need Help.
- From: Frank Kotler
- Need Help.
- Prev by Date: Re: Need Help.
- Next by Date: MASM dwords as character strings
- Previous by thread: Re: Need Help.
- Next by thread: security and red zone
- Index(es):
Relevant Pages
|
|