0
votes

Good day. My project is about computing BMI using inches and pounds. So the user will input his height in inches and weight in pounds. I have already learned dividing and multiplying. I just don't know how to include the decimal points after dividing, because I have to multiply it again by 703, and only the whole number is multiplied by 703.

n1 dw 0
n2 dw 0

;gets input from user

xor dx, dx
mov ax, n1    ;n1 is the weight and n2 is the height so i'm dividing them
mov bx, n2
div bx
mov bx, ax    ;answer is stored in bx

Any help would be greatly appreciated thank you!

1
Assuming x86 architecture, you're doing integer division (and probably multiplication as well). Floating point arithmetic in assembler uses special instructions (and special registers).Some programmer dude
I can't find any good reference programs using Floating point arithmetic. I found the instructions but can't find a program using them.Keith Snyder
If you multiply before you divide,you can do it all with integers.prl
You can do the computations with scaled integers (10 times or 100 times the real value) and then just insert a decimal separator when displaying.Bo Persson

1 Answers

1
votes

Using fixed-point notation, to obtain a result of division with fractional part, you can write:

 Mov   AX,n1
 XOr   CX,CX
{AX:CX contains the integer number to divide by n2}
 Mov   BX,n2
{BX contains the integer number that divides}
 XOr   DX,DX
 Div   BX
 XChg  AX,CX
 Div   BX
{CX:AX contains the result of division (AX is the fractional part)}

But, as said by prl, is better first multiply and then divide. The division, however, is maded always with two "div" instruction fore this reason: when the result of a DIV instruction can't be contained into AX's register, a "division by zero" interrupt's call is invoked. After MUL you have a 32 bit number DX:AX, that can be divided by n2 as follows:

 Mov   CX,AX
 MOV   AX,DX
{AX:CX contains the result of multiplication}
 Mov   BX,n2
{BX contains the integer number that divides}
 XOr   DX,DX
 Div   BX
 XChg  AX,CX
 Div   BX
{CX:AX contains the result of division}

Also, to do a inch to cm conversion (and similar), you can multiply for a fixed point number that represent the scale factor:

{cm=inch*028A3DH (2.54)}

 MOV AX,08A3DH
 MOV BX,Inch
 XOR CX,CX
 MUL BX
 ADD DX,BX
 ADC CX,CX
 ADD DX,BX
 ADC CX,0

{CX:DX:AX= cm (AX is the fractional part)}