|
-
November 8th, 2011, 12:20 AM
#1
How to disply Fractions
Can someone show me how to display a Fraction entered by the user?
My problem is at prompt6.
This is my code so far...
Code:
TITLE MASM Template (main.asm)
; Description:
; Author:
; Assignment:
; Revision date:
INCLUDE Irvine32.inc
.data
firstNUM SDWORD ?
firstDEN SDWORD ?
secNUM SDWORD ?
secDEN SDWORD ?
prompt BYTE "Please enter a two numerators and two denominators for two fractions.", 0
prompt2 BYTE "Please enter the first numerator: ", 0
prompt3 BYTE "Please enter the first denominator: ", 0
prompt4 BYTE "Please enter the second numerator: ", 0
prompt5 BYTE "Please enter the second denominator: ", 0
prompt6 BYTE "The first fraction you entered is: ", 0
SDWORD firstNum, "/", firstDEN, 0
prompt7 BYTE "The second fraction you entered is: ", 0
.code
main PROC
mov edx, OFFSET prompt ;display prompt.
call WriteString
call CRLF
mov edx, OFFSET prompt2 ;display prompt2.
call WriteString ;allows user to enter an unsigned integer from the keyboard.
call ReadDec mov firstNUM, eax
mov edx, OFFSET prompt3
call WriteString
call ReadDec
mov firstDEN, eax
mov edx, OFFSET prompt4
call WriteString
call ReadDec
mov secNUM, eax
mov edx, OFFSET prompt5
call WriteString
call ReadDec
mov secDEN, eax
mov edx, OFFSET prompt6
exit
main ENDP
END main
These all use the Irvine library, Thanks.
-
November 8th, 2011, 01:14 PM
#2
Re: How to disply Fractions
To output the fraction, of course you need to convert both the numerator and denominator to decimal ASCII strings and then output those. Doesn't your library have a function for that? What needs to be done is the reverse of what ReadDec obviously does internally with a character string read in from the console and stored into an internal buffer. (It actually may do that on the fly without buffering as well, but that wouldn't change anything about the principle.)
In case your library doesn't have a binary-to-decimal conversion function, you can't find documentation on that process (which I consider to be unlikely) and/or just to quench your curiosity: Here's a thread that discusses exactly that topic: http://www.codeguru.com/forum/showthread.php?t=503267.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
November 8th, 2011, 03:14 PM
#3
Re: How to disply Fractions
Thanks, this is my resolved diplaying fraction after the last line, but for some reason it outputs a
+(number entered)/+(number entered).
Code:
mov eax, firstNUM
call writeInt
mov al, 47
call writeChar
mov eax, firstDEN
call writeInt
call CRLF
Another question how would you reduce that fraction after multiplying two fractions? More code coming...
-
November 8th, 2011, 04:17 PM
#4
Re: How to disply Fractions
This is my code after multiplying. Now I have a problem when I enter a negative number. The number results in a +0. Why and how to make it say -(Number). I am using the signed and imul, so I don't know what the problem is. Also I know that I need to use the cmp instruction when reducing just don't know how to completly do it.
Code:
TITLE MASM Template (main.asm)
; Description:
; Author:
; Assignment:
; Revision date:
INCLUDE Irvine32.inc
.data
firstNUM SDWORD ?
firstDEN SDWORD ?
secNUM SDWORD ?
secDEN SDWORD ?
newNUM SDWORD ?
newDEN SDWORD ?
prompt BYTE "Please enter a two numerators and two denominators for two fractions.", 0
prompt2 BYTE "Please enter the first numerator: ", 0
prompt3 BYTE "Please enter the first denominator: ", 0
prompt4 BYTE "Please enter the second numerator: ", 0
prompt5 BYTE "Please enter the second denominator: ", 0
prompt6 BYTE "The first fraction you entered is: ", 0
prompt7 BYTE "The second fraction you entered is: ", 0
prompt8 BYTE "The result of multiplying the two fractions together is: ", 0
.code
main PROC
mov edx, OFFSET prompt ;display prompt.
call WriteString
call CRLF
mov edx, OFFSET prompt2 ;display prompt2.
call WriteString ;allows user to enter an unsigned integer from the keyboard.
call ReadDec ;allows the user's to get input and store it as an unsigned integer.
mov firstNUM, eax
mov edx, OFFSET prompt3
call WriteString
call ReadDec
mov firstDEN, eax
mov edx, OFFSET prompt4
call WriteString
call ReadDec
mov secNUM, eax
mov edx, OFFSET prompt5
call WriteString
call ReadDec
mov secDEN, eax
mov edx, OFFSET prompt6
call WriteString
mov eax, firstNUM
call writeInt
mov al, 47
call writeChar
mov eax, firstDEN
call writeInt
call CRLF
mov edx, OFFSET prompt7
call WriteString
mov eax, secNUM
call writeInt
mov al, 47
call writeChar
mov eax, secDEN
call writeInt
call CRLF
mov eax, 0
mov eax, firstNUM
imul eax, secNUM
mov newNUM, eax
mov eax, 0
mov eax, firstDEN
imul eax, secDEN
mov newDEN, eax
mov edx, OFFSET prompt8
call WriteString
mov eax, newNUM
call writeInt
mov al, 47
call writechar
mov eax, newDEN
call writeInt
call CRLF
exit
main ENDP
END main
-
November 8th, 2011, 10:48 PM
#5
Re: How to disply Fractions
Naturally, I have no idea of what's going on inside that library you use. Hopefully you have proper documentation on that library or its source code to aid you in finding out. But even without the library source code you may find out some things by stepping into the library code with your debugger. After all, in assembly language source code and object code aren't too far away from each other anyway. Perhaps all that you're observing actually is somehow by design of the library. And, of course, first thing you should do is make sure that you actually are passing correct data to the library functions - by debuging your own code.
One thing I can tell you, thogh: Yes, IMUL is the correct instruction for signed integer multiplication. But you probably already knew that anyway...
 Originally Posted by Wh1t3gh0st
Another question how would you reduce that fraction after multiplying two fractions?
In assembly language that doesn't work differently than in any other language (except perhaps in some language specifically geared towards arithmetic/math that provides readily available instructions for that) - just like you would do it with an ordinary calculator - or with pencil and paper... Perhaps this is helpful in fortifying your background on fractional multiplication: http://en.wikipedia.org/wiki/Fractio...Multiplication
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|