Click to See Complete Forum and Search --> : How to disply Fractions


Wh1t3gh0st
November 7th, 2011, 11:20 PM
Can someone show me how to display a Fraction entered by the user?
My problem is at prompt6.
This is my code so far...

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.

Eri523
November 8th, 2011, 12:14 PM
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.

Wh1t3gh0st
November 8th, 2011, 02:14 PM
Thanks, this is my resolved diplaying fraction after the last line, but for some reason it outputs a
+(number entered)/+(number entered).

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

Wh1t3gh0st
November 8th, 2011, 03:17 PM
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.

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

Eri523
November 8th, 2011, 09:48 PM
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...

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/Fraction_(mathematics)#Multiplication