staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · web viewb c3 st c2 c1c0 ir pe...

32
CS344-321 Assembly Language Programming chapter 13 page ___________________________________________________________________ _____ Chapter 13 8087 Co-processor 8087 ใใใใใใใใใใ 8086/8088 ใใใใใใใใใใใใใใใใใใใใใใใใ ใใใใใใใใ 8087 ใใใใใใใ software ใใใใใใใใใใใใใใใใใใใใใใใใใใใ ใใใใใใใใใใใใใใใใ ใใใใใใใใใ 8087 ใใใใใใใ 80286 ใใ 80287 ใใใ 80386 ใใ 80387 ใใใใ C0-processor ใใใใใใใ CPU 8086 - 80386 ใใใใใใใใใใใใใใ Co-processor ใใใใใ ใใใใใใใ 80486 ใใใใใใใใใ ใใใใ Co- processor ใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใ ใใใใใใใใใใใใใใใใใใใ ใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใ ใใใใใใใ directive .8087 ใใใใใใใใใใใ assembler ใใใ ใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใใ 8087 ใใใใใใใใใใใใใใใใใใใ 8087 8087 Register Stack DATA FIELD TAG FIELD 79 78 64 63 0 1 0 sign exponent significand (mantissa) ST(0) ST(1) ST(2) ST(3) ST(4) ST(5) ST(6) ST(7) Computer Science, Prince of Songkhla University 1

Upload: others

Post on 20-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________

Chapter 13 8087 Co-processor

8087 ใชรวมกบ 8086/8088 เพอคำ�นวณเลขจำ�นวนจรง ถ�ไมม 8087 ตองใชsoftware เพอคำ�นวณเลขจำ�นวนจรงเอง ซงจะช�กว�ก�รคำ�นวณโดย 8087 สำ�หรบ 80286 ม80287 และ 80386 ม 80387 เปน C0-processor ดงนน CPU 8086 - 80386 ผใชตองซอ Co-processor เพม ตงแต 80486 เปนตนไป จะม Co-processor เพอคำ�นวณเลขจำ�นวนจรงม�ใหดวยเสมอ จงไมตองซอเพม

เมอเขยนโปรแกรมภ�ษ�แอสเซมบล ตองใส directive .8087

เพอบอกให assembler ร ว�ตองก�รเกบเลขจำ�นวนจรงต�มรปแบบของ 8087 และรจกคำ�สงของ8087

8087 Register StackDATA FIELD TAG FIELD

79 78 64 63 0 1 0 sign exponent significand (mantissa)ST(0)ST(1)ST(2)ST(3)ST(4)ST(5)ST(6)ST(7)

15 0 control register status register

Computer Science, Prince of Songkhla University

1

Page 2: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________

Control Register15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 IC RC PC IEM PMUM OM ZM DM IM

invalid operation denormalized operand zero divide overflow underflow precision

reserved interrupt enable mask

(1) precision control (2) rounding control (3) infinity control (4) reserved

(1) Interrupt-Enable Mask0 = Interrupts Enabled1 = Interrupt Disabled (masked)(2) Precision Control00 = 24 bits01 = (reserved)10 = 53 bits11 = 64 bits(3) Rounding Control00 = Round to Nearest or Even01 = Round Down (toward -)10 = Round Up (toward +)11 = Chop (Truncate Toward Zero)(4) Infinity Control0 = Projective ()1 = Affine (+,-)

Computer Science, Prince of Songkhla University

2

Page 3: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________Status Register15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 B C3 ST C2 C1C0 IR PE UE OE ZE DE IE

invalid operation denormalized operand zero divide overflow underflow precision

reserved interrupt request condition code (1) stack top pointer (2) busy

(1) See descriptions of compare, test, examine and remainder instruction

(2) ST values:000 = register 0 is stack top001 = register 1 is stack top…111 = register 7 is stack top

8087 Data Type

(S) sign bitword integer two’s complements

15 0

short integer two’s complement31 0

long integer two’s complement63 0

Computer Science, Prince of Songkhla University

3

Page 4: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________packed decimal S d17 d16 d15 d14 d13 d12 d11 d10 d9 d8 d7 d6 d5 d4 d3 d2 d1 d0 79 72 71 0

short real S biased exponent significand (mantissa) 31 23 1. 0

long real S biased exponent significand (mantissa) 63 52 1. 0

temporary real S biased exponent significand (mantissa) 79 64 63 0

สรป 8087 data typesdata type bits significant digits rangeword integer 16 4 -32768 to 32767short integer 32 9 -(231+1) to 231

long integer 64 18 -(263+1) to 263

packed Decimal 80 18 18 decimal digitsshort real 32 7 10-38 to 1038

long integer 64 15 10-308 to 10308

temporary real 80 19 10-4932 to 104932

FLOATING-POINT DATA REPRESENTATIONThere are three data formats for representing real

numbers with the 8087. Two of the floating-point

Computer Science, Prince of Songkhla University

4

Page 5: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________representations conform to the proposed Institute of Electrical and Electronic Engineers (IEEE) standard for such numbers. The short real format uses 32 bits; the long real format requires 64 bits. The third format specifies the number as an 80-bit field. This format does not conform to the IEEE standard. The 8087 uses this “temporary real” format internally to give very high precision to intermediate results.

Floating-point representation allows us to write very large numbers (e.g., 1.234x1085), or very small numbers (e.g., 1.234x10 –85), in a compact fashion. Binary floating-point representation is very similar to decimal floating point. The base of the number system is 2 rather than 10. The mantissa is a value between 1 mantissa < 2, and the exponent is a power of 2. A floating-point number is divided into two fields. One field contains the sign and value of the mantissa. The other field contains the sign and value of the exponent. The size of the mantissa field determines the precision or accuracy of the number. The more bits that the computer allots to the mantissa field, the higher the precision.

In order to maintain the maximum precision, computers almost always store the mantissa as a normalized number. This means that the mantissa is a number between 1 and 2. The computer adjusts the exponent portion of the number to the appropriate value to support the normalization. There are exceptions to the normalization of the mantissa. The most obvious exception is when the value is zero. In that case, the entire mantissa is zero.

Exponent values are not stored as two’s complement number. For computational ease, computers store the exponent as a biased number. This means that the actual exponent value has a bias value added to it before it is stored. The short real format in the 8087 uses 32 bits, of which 8 are for the exponent. Thus, the exponent can be in the range –128 to 127. However, the 8087 reserves the

Computer Science, Prince of Songkhla University

5

Page 6: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________value of –128 for a special, indefinite, number, so the exponent range is –127 to 127.

exponent value biased value (hex) biased value (dec)

-127 00 0-1 7e 1260 7f 1271 80 128127 fe 254

Note! bias short real +127 long real +1023, temporary real +16383exponent mantissa special meaningall 0 all 0 floating point zeroall 0 nonzero denormalizedall 1 all 0 infinityall 1 nonzero NaN (Not a Number)ตวอย�งshort floating point 41200000h or0100 0001 0010 0000 0000 0000 0000 0000 b s = 0 or + biased exponent = 10000010b or 130 therefore correct exponent = 130 – 127 = 3 or 11band mantissa = 01000000000000000000000b therefor correct mantissa = 1.01b

ดงนนค�ทแทนคอ 1.01b x 10b11b or 1010b ( หม�ยเหต เลอนจดไปท�งขว� 3 ตำ�แหนง) ซงมค�เท�กบ 10 เลขฐ�นสบนนเอง

8087 Instruction Set

- Data TransferFLD load realFST store realFSTPstore real and popFXCH exchange registers

Computer Science, Prince of Songkhla University

6

Page 7: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________FILD integer loadFIST integer storeFISTP integer store and popFBLD packed decimal (BCD) loadFBSTP packed decimal (BCD) store and pop

- ControlFINITInitialize the 8087, software resetFENI Enable the exception interruptsFDISIDisable the exception interruptsFLDCW Load the 8087 control register from memoryFSTCW Store the 8087 control register into memoryFSTSW Store the 8087 status register into memoryFCLEX Clear the exception indicatorsFSTENV Store the 8087 environment into memoryFLDENV Load the 8087 environment from memoryFSAVE Save the 8087 state into memoryFRSTOR Load the 8087 state from memoryFINCSTP Increment the stack pointerFDECSTP Decrement the stack pointerFFREE Free the stack registerFNOP Do nothingFWAIT Identical to WAIT

- Arithmetic destination source meaningFADD ST0 ST1 ST0ST0+ST1FADD STi ST0 Sti ST0ST0+STiFADD ST0,STi ST0 Sti ST0ST0+STiFADD STi,ST0 Sti ST0 STiSTi+ST0FADDP STi,ST0 Sti ST0 STiSTi+ST0, pop stackFADD REAL_memory ST0 REAL_memory

ST0ST0+REAL_memoryFIADD INTEGER _memory ST0 INTERGER_memory ST0ST0+INTEGER_memory

Computer Science, Prince of Songkhla University

7

Page 8: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________

สำ�หรบก�ร ลบ คณและห�ร แทนท ADD ดวย SUB, SUBR , MUL, DIV, และ DIVR ซงม คว�มหม�ย ดงน

SUB destination destination – sourceSUBR destination source – destinationMUL destination destination * sourceDIV destination destination / sourceDIVR destination source / destination

EXAMPLE; X = (7.5*Y)+(Z-3.7)/2.0;; POSTFIX FORM OF (7.5*Y)+(Z-3.7)/2.0; IS 7.5 Y * Z 3.7 - 2.0 / +.8087

ASSUME CS:MYCODE,DS:MYDATAMYCODE SEGMENTSTART: MOV AX,MYDATA

MOV DS,AXFINITFLD T1 ; 7.5FLD Y

FMULP ST(1),ST(0)FLD ZFLD T2 ; 3.7

FSUBP ST(1),ST(0)FLD T3 ; 2.0

FDIVP ST(1),ST(0) FADDP ST(1),ST(0)

FSTPXFWAITMOV AX,4C00HINT 21H

MYCODE ENDSMYDATA SEGMENTX DD ?Y DD 52.75Z DD 1.0E-10

Computer Science, Prince of Songkhla University

8

Page 9: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________T1 DD 7.5E0T2 DD 3.7T3 DD 2.0MYDATA ENDS

END START

- ComparisonFCOM หรอ FCOM STi หรอ FCOM REAL_memory หรอ FICOM INTEGER_memory C3 C00 0 ST > source0 1 ST < source1 0 ST = source1 1 ST and source cannot be comparedหม�ยเหต ส�ม�รถใส P ข�งหลงถ�ตองก�ร pop และส�ม�รถ pop สองครง คอ FCOMPP ในกรณน

ไมตองม operands จะหม�ยถงก�รเปรยบเทยบ ST0 กบ ST1 แลว pop ทงคทง FTSTcompares ST0 to zero, sets flags

FXAMC3 C2 C1 C0 Meaning0 0 0 0 + Unnormal0 0 0 1 + NAN0 0 1 0 - Unnormal0 0 1 1 - NAN0 1 0 0 + Normal0 1 0 1 + Infinity0 1 1 0 - Normal0 1 1 1 - Infinity1 0 0 0 +01 0 0 1 Empty1 0 1 0 - 01 0 1 1 Empty1 1 0 0 + Denormal1 1 0 1 Empty1 1 1 0 - Denormal

Computer Science, Prince of Songkhla University

9

Page 10: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________1 1 1 1 Empty

- Function and TranscendentalFSQRT square rootFSCALE scale one value by anotherFRNDINT round to integerFXTRACT extracts exponent and mantissa from valueFABS absolute valueFCHS change sign of valueFPTAN partial arctangent 0 x PI/4FPATAN partial arctangent 0 x y infinityFSIN sine | x | < 2 63

- Constantinstruction constantFLDZ 0FLD1 1FLDPI PIFLDL2T LOG2(10)FLD2E LOG2(e)FLDLG2 LOG10(2)FLDLN2 LOGe(2)

Coprocessor Data Movementตวอย�ง.א loads a 32-bit floating-point number from a variable onto

the coprocessor floating-point stack:.code…fld myvar….data…myvar dd ?…

Computer Science, Prince of Songkhla University

10

Page 11: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________

หรอ loads two 32-bit floating-point numbers from a variable onto the coprocessor floating-point stack:.code…mov bx,offset myvarfld dword ptr [bx]fld dword ptr [bx+4]….data…myvar dd 4 dup(?)…

.ב load a 32-bit integer from a memory variable.code…fild myvar….data…myvar dd ?

To Wait or Not to WaitOne of the subtler aspects of coprocessor programming

is enforcing proper synchronization between CPU and the coprocessor. The primary software means of synchronizing the CPU and the coprocessor is the FWAIT instruction. If an FWAIT is encountered in the instruction stream and the coprocessor is busy, the CPU goes into a wait state until the coprocessor finishes its current operation.

เพอไมใหผใชตองกงวลเกยวกบก�ร synchronize ระหว�งสอง processor ทกคำ�สง ของ 8087 จะมคำ�สง fwait ไวข�งหน�เสมอ เชน กรณทท�นเขยน ดงน

fld dword ptr [bp]fld dword ptr [bp+4]

คำ�สงภ�ษ�เครองทได จะมลกษณะ ดงนfwait

Computer Science, Prince of Songkhla University

11

Page 12: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________fld dword ptr [bp]fwaitfld dword ptr [bp+4]

อย�งไรกต�มในบ�งสถ�นะก�รณ ท�นตองระมดระวง เชน กรณตอไปนfld myvarfwaitmov word ptr myvar,0mov word ptr myvar+2,0

ถ�ไมมคำ�สง fwait หลง fld ถ� Coprocessor ไมพรอมทจะ load ค�ท myvar แลวไมมคำ� สง fwait กรณน CPU จะใส ค� 0 ไปท myvar ทำ�ใหค�ท Coprocessor เมอพรอมทจะ

load ไมถกตอง เปนตน อกตวอย�งหนง เชนfistp myvarfwaitmov ax,word ptr myvarmov dx,word ptr myvar+2

Comparing Numbers.datastatus dw ?….code…; compares the value in ST(0) to the number in ST(1), discards both

fcomppfstsw statusfwaitmov ax,statussahf ; Store AH into Flagsja label1 ; if st(0) > st(1)jmp label2 ; if st(0) <= st(1)

…หม�ยเหต 80287 ขนไป ส�ม�รถใชคำ�สง fstsw ax ไดโดยตรง

Computer Science, Prince of Songkhla University

12

Page 13: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________Transcendental FunctionsLOGS : The log to the base n of any number x, where x is greater than 0, can be found as shown here: log n(2)*log2(x)หม�ยเหต logb(a) = logc(a) / logc(b)

เชน log10(x) = log2(x) / log2(10) = log2(x) * log10(2).datax dd ?.code…

fldlg2fld x

fyl2x ; st(1) <- st(1)*log2(st(0)); pop where 0 < x < infinity and -infinity < Y < infinity

fstp xfwait

…ANTILOGS : This is the general approach to finding the antilog to the base n of some number x:

nx = 2x*log2(n)

Thus, to find common and natural antilogs, you need the instructions FLD1 (load constant 1), FLDL2E (load log2(e)), FLDL2T (load log2(10)), F2XM1 (2x – 1 where 0 <= x <= 0.5), and FSCALE (st0*2st1). Since F2XM1 can compute when 0 <= x <= 0.5, so the argument must be decomposed into its integer portions and fraction portions with FRNDINT and FSUB.

วธก�ร1. คำ�นวณ x*log2(n) กอน2. จ�กนน แยกเปนสองสวน คอ สวนจำ�นวนเตม กบ สวนทเปนเศษ 3. นำ�สวนทเปนเศษม�ห�ร 2 เพอใหมค�อยในชวง 0 <= x <= 0.54. ใชคำ�สง F2XM1 เพอห�ค� 2x – 1 เมอ x เปนค�ทได จ�กขอ 35. บวกค�ในขอ 4 ดวย 1 จะได 2x

6. นำ�ค�ในขอ 5 คณกน จะได 2 สวนทเปนเศษ ( หม�ยเหต 2 สวนทเปนเศษ/2 * 2 สวนทเปนเศษ/2 =2 สวนทเปนเศษ)7. ใชคำ�สง FSCALE เพอคำ�นวณ 2 สวนทเปนเศษ * 2 สวนจำ�นวนเตม จะไดค�ต�มทตองก�ร

Computer Science, Prince of Songkhla University

13

Page 14: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________

เขยนเปนโปรแกรมภ�ษ�แอสเซมบลได ดงน; FALOG.ASM --- Calculate Common Antilog on 80x87; Copyright (C) 1989 Ray Duncan; Call with: ST(0) = argument; Returns: ST(0) = antilog to base 10; Uses: CPU registers are preserved; uses 3 cells of coprocessor stack; Note: To obtain natural antilog, replace "fldl2t" instruction with "fldl2e".; Make sure coprocessor has been properly initialized; with a previous call to INIT87!.codeoldcw equ word ptr [bp-4] ; original control wordnewcw equ word ptr [bp-2] ; new control word

public falogfalogproc near

pushax ; save registers andpushbp ; make stack framemov bp,sp ; for local variablessub sp,4fldl2t ; load log2(10)fmulp st(1),st(0) ; log2(10) * argumentfld st(0) ; duplicate productfstcw oldcw ; get old control wordfwait ; wait till it arrivesmov ax,oldcw ; change rounding modeand ax,0f3ffh ; field to "round down"or ax,0400hmov newcw,axfldcw newcw ; force rounding modefrndint ; get int part of productfldcw oldcw ; restore old rounding

modefld st(0) ; duplicate integer partfxch st(2) ; get original productfsubrp st(1),st(0) ; find fractional part

Computer Science, Prince of Songkhla University

14

Page 15: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________

fld1fchsfxch st(1) ; scale fractional partfscalefstp st(1) ; discard coprocessor junkf2xm1 ; raise 2 to power-1fld1faddp st(1),st(0) ; correct for the -1fmul st(0),st(0) ; square resultfscale ; scale by int partfstp st(1) ; discard coprocessor junkadd sp,4 ; discard local variablespop bp ; restore registers andpop ax ; return result in ST(0)ret

falogendpend

SINE, COSINE, TANGENT สำ�หรบ 80387 เปนตนไป ส�ม�รถคำ�นวณไดโดยตรงดวยคำ�สง FSIN, FCOS, และ

FTAN

INPUT and OUTPUTATOF: ฟงกชน atof ทำ�หน�ทเปลยน string ซงมรปแบบ ดงน

[sign][digits][.[digits]][E|e[sign][exp]] ใหเปน real สำ�หรบ coprocessor

วธก�ร1. มองสวน mantissa เหมอนเลขจำ�นวนเตม โดยก�รคำ�นวณสวน mantissa แบบเดยวกบ

atoi แตคำ�นวณดวย real คอใช fimul ( ดวย 10) และ fiadd ( ดวยเลขทตำ�แหนงต�ง ๆ) ใหจำ�ตำ�แหนงของจดทศนยมดวย

2. คำ�นวณสวน exponent 3. ลบสวน exponent ดวย ตำ�แหนงจดทศนยม4. คำ�นวณ 10exponent โดยใช antilog10(exponent)5. นำ� mantissa ในขอ 1. คณกบ 10exponent ในขอ 4. จะไดค� real ต�มตองก�ร

Computer Science, Prince of Songkhla University

15

Page 16: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________

เชน 127.95E32 1. mantissa = 12795 ตำ�แหนงจดทศนยม = 22. exponent = 323. exponent = 32-2 = 304. ห� antilog10(30)5. ผลลพธ = 12795 * antilog10(30)

; ATOF.ASM --- Convert ASCII String to Binary; Floating Point Number on 80x87 Stack; (also requires FALOG); Copyright (C) 1989 Ray Duncan; Call with: DS:SI = address of string in the form; [sign][digits][.[digits]][E|e[sign][exp]]; leading blanks or tabs are ignored; Returns: ST(0) = binary floating point value; DS:SI = address+1 of terminator; Uses: Nothing; This routine gives no warning in the event of; overflow, and terminates on the first invalid character.; Make sure coprocessor has been properly initialized; with a previous call to INIT87!blank equ 20h ; ASCII blank charactertab equ 09h ; ASCII tab character.dataint10 dw 10 ; integer constant 10digit dw 0 ; current converted digitplaces dw 0 ; number of decimal places.code

extrn falog:near; we need FALOG routinepublic atof

atof proc near ; ASCII to floating pointpushax ; save registerspushcxpushdxcall convert ; convert mantissa

Computer Science, Prince of Songkhla University

16

Page 17: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________

neg dx ; save -1 * decimal placesmov places,dxor al,20h ; fold char to lower casecmp al,'e' ; is exponent present?je atof1 ; yes, jumpfldz ; assume zero exponentjmp atof2

atof1: call convert ; convert exponentatof2: fiaddplaces ; adjust exponent for

; dec. places in mantissacall falog ; raise 10 to powerfmul ; exponent * mantissapop dx ; restore registerspop cxpop axret ; return ST(0) = result

atof endp; CONVERT: Called by ATOF to convert ASCII number; with possible sign and/or decimal point; Call with: DS:SI = address of string; Returns: ST(0) = result; AL = first unconvertable character; DX = number of digits after decimal point; DS:SI = address+1 of character in AL; Uses: AH, CXconvert proc near ; convert numeric field

fldz ; initialize resultxor cx,cx ; initialize signmov dx,-1 ; initialize decimal count

conv1: lodsb ; scan off whitespacecmp al,blank ; ignore leading blanksje conv1cmp al,tab ; ignore leading tabsje conv1cmp al,'+' ; if + sign proceedje conv2

Computer Science, Prince of Songkhla University

17

Page 18: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________

cmp al,'-' ; is it - sign?jne conv3 ; no, test if numericdec cx ; yes, set flag

conv2: lodsb ; get next characterconv3: cmp al,'0' ; is character valid?

jb conv4 ; jump if not '0' to '9'cmp al,'9'ja conv4 ; jump if not '0' to '9'and ax,0fh ; isolate lower four bitsmov digit,ax ; and save digit valuefimulint10 ; previous value * 10fiadddigit ; accumulate new digitor dx,dx ; past decimal point?js conv2 ; no, convert next digitinc dx ; yes, count digitsjmp conv2 ; convert next digit

conv4: cmp al,'.' ; is it decimal point?jne conv5 ; no, proceedinc dx ; indicate decimal foundjmp conv2 ; convert more digits

conv5: jcxz conv6 ; jump if result pos.fchs ; make result negative

conv6: or dx,dx ; decimal point found?jns conv7 ; yes, jumpxor dx,dx ; no, return zero places

conv7: ret ; return ST(0) = resultconvert endp

end

FTOA : ฟงกชน ftoa ทำ�หน�ทเปลยน real ใหเปน string ซงมรปแบบ ดงน+0.000000000000000000E+000

วธก�ร1. ใช FXAM เพอตรวจสอบเลข real ถ�ไมปรกต แสดง string ทกำ�หนดไวแลว แลวเลกทำ� มฉะนน

ปรกตทำ�ขอตอไป2. ใช FXTRACT เพอแยก real ท st(0) ใหเปน mantissa และ exponent จะได

mantissa อยท st(0) และ exponent อยท st(1)

Computer Science, Prince of Songkhla University

18

Page 19: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________3. เปลยน exponent ในขอ 2. ซงเปน 2 ยกกำ�ลง ใหเปน 10 ยกกำ�ลง โดยใช antilog4. ปรบ mantissa ใหเปน integer ดวยก�รปรบสวน exponent 5. นำ�สวน mantissa ในขอ 4 เปลยนเปน 18-digit BCD โดยใชคำ�สง fbstp mant

เมอ mant เปนทเกบ 18-digit BCD จ�กนนเปลยนเปน ASCII 6. เปลยน exponent(y) ใหเปน ASCII จะได string ต�มตองก�ร

; FTOA.ASM --- Convert Binary Floating Point; Number on 80x87 Stack to ASCII; (also requires FALOG); Copyright (C) 1989 Ray Duncan; Call with: ST(0) = floating point number; DS:SI = buffer to receive string; Returns: DS:SI = address of converted string; AX = length of string; Coprocessor stack "popped"; Uses: Nothing; Make sure coprocessor has been properly initialized; with a previous call to INIT87!.datasign dw 0 ; receives FXAM statusoldcw dw 0 ; previous rounding modenewcw dw 0 ; new rounding modeexp dw 0 ; extracted power of tenstatus dw 0 ; receives FCOM statusmant dt 0 ; mantissa as BCD valuefp1e17 dq 1.0e17 ; constant for scalingfp1e18 dq 1.0e18 ; constant for scalingint10 dw 10 ; constant for scalingpzstrdb '+0.000000000000000000E+000' ; displayed ifpz_len equ $-pzstr ; ST(0) = +0mzstr db '-0.000000000000000000E+000' ; displayed ifmz_len equ $-mzstr ; ST(0) = -0pistr db '<+infinity>' ; displayed if ST(0)pi_len equ $-pistr ; is positive infinity

Computer Science, Prince of Songkhla University

19

Page 20: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________mistr db '<-infinity>' ; displayed if ST(0)mi_len equ $-mistr ; is negative infinitynanstr db '<NaN>' ; displayed if ST(0)nan_len equ $-nanstr ; contains Not-A-Numberunstr db '<unnormal>' ; displayed for positiveun_len equ $-unstr ; or negative unnormalsdestr db '<denormal>' ; displayed for positivede_len equ $-destr ; or negative denormalsempstr db '<empty>' ; displayed if ST(0)emp_len equ $-empstr ; is tagged "empty"

; number types from; condition code bits

typetab dw ftoa14 ; 0000 +unnormaldw ftoa15 ; 0001 +NaNdw ftoa14 ; 0010 -unnormaldw ftoa15 ; 0011 -Nandw ftoa1 ; 0100 +normaldw ftoa16 ; 0101 +infinitydw ftoa1 ; 0110 -normaldw ftoa17 ; 0111 -infinitydw ftoa10 ; 1000 +zerodw ftoa12 ; 1001 emptydw ftoa11 ; 1010 -zerodw ftoa12 ; 1011 emptydw ftoa13 ; 1100 +denormaldw ftoa12 ; 1101 emptydw ftoa13 ; 1110 -denormaldw ftoa12 ; 1111 empty

.codeextrn falog:near ; we need FALOG routinepublic ftoa

ftoa proc near ; floating point to ASCIIpushbx ; save registerspushcxpushdxpushdi

Computer Science, Prince of Songkhla University

20

Page 21: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________

pushespushds ; let ES point to _DATApop esfxam ; test type of numberfstsw sign ; unload FXAM statusfwaitmov bx,sign ; retrieve status andand bx,4700h ; shift C3, C2, C1, C0rol bx,1 ; status bits to formrol bx,1 ; value 0-15, thenshl bh,1 ; *2 for jump indexshl bh,1shl bh,1rol bx,1rol bx,1rol bx,1shl bx,1jmp [typetab+bx] ; branch by number type

ftoa1: fabs ; force number positivefxtract ; extract exponentfxch st(1) ; put exponent on topfldlg2 ; form power of 10fmulp st(1),st(0)fstcw oldcw ; save current rounding

modefwaitmov ax,oldcw ; set bit field for roundingor ax,0c00h ; mode to "chop"mov newcw,axfldcw newcw ; force new rounding

modefld st(0) ; duplicate power of tenfrndint ; find integer part offist exp ; exponent and save itfldcw oldcw ; restore old rounding

mode

Computer Science, Prince of Songkhla University

21

Page 22: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________

fsubp st(1),st(0) ; find fractional part ofcall falog ; power of tenfmulp st(1),st(0) ; then times mantissafmul fp1e18 ; scale mantissa for BCDfrndint ; zap any remaining fraction

ftoa2: fcomfp1e17 ; is mantissa < 1.0e17?fstsw statusfwaitmov ax,statussahfjae ftoa3 ; no, proceedfimulint10 ; yes, mantissa * 10dec exp ; and decrement exponentjmp ftoa2

ftoa3: fcomfp1e18 ; is mantissa < 1.0e18?fstsw statusfwaitmov ax,statussahfjb ftoa4 ; yes, proceedfidiv int10 ; yes, mantissa / 10inc exp ; and increment exponentjmp ftoa3

ftoa4: fbstpmant ; unload mantissa in BCDfwaitmov di,si ; address for ASCII stringmov al,'+' ; assume positivetest sign,200h ; check FXAM flagsjz ftoa5 ; jump, value was positivemov al,'-'

ftoa5: stosb ; store + or - signmov al,'0' ; store leading zerostosbmov al,'.' ; store decimal pointstosbmov bx,8 ; point to last BCD digits

Computer Science, Prince of Songkhla University

22

Page 23: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________ftoa6: mov al,byte ptr [bx+mant] ; convert BCD byte to

shr al,1 ; two ASCII digitsshr al,1shr al,1shr al,1call digit ; convert high nibblemov al,byte ptr [bx+mant]call digit ; convert low nibbledec bx ; back up through BCD valuejns ftoa6 ; until 18 digits convertedmov al,'E' ; store 'E' for exponentstosbmov bx,exp ; test sign of exponentmov al,'+'or bx,bxjns ftoa7 ; jump, exponent positivemov al,'-'neg bx ; abs. value of exponent

ftoa7: stosb ; store sign of exponentmov ax,bx ; convert exponent tocwd ; three ASCII digitsmov cx,100div cxcall digit ; exponent hundreds digitmov ax,dxcwdmov cx,10div cxcall digit ; exponent tens digitmov ax,dxcall digit ; exponent ones digit

ftoa8: mov ax,di ; return AX = string lengthsub ax,si ; and DS:SI = string addresspop es ; restore registerspop di

Computer Science, Prince of Songkhla University

23

Page 24: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________

pop dxpop cxpop bxret

ftoa10: mov di,offset pzstr ; +zero valuemov cx,pz_lenjmp ftoa20

ftoa11: mov di,offset mzstr ; -zero valuemov cx,mz_lenjmp ftoa20

ftoa12: mov di,offset empstr ; empty valuemov cx,emp_lenjmp ftoa20

ftoa13: mov di,offset destr ; denormal valuemov cx,de_lenjmp ftoa20

ftoa14: mov di,offset unstr ; unnormal valuemov cx,un_lenjmp ftoa20

ftoa15: mov di,offset nanstr ; NaN valuemov cx,nan_lenjmp ftoa20

ftoa16: mov di,offset pistr ; +infinity valuemov cx,pi_lenjmp ftoa20

ftoa17: mov di,offset mistr ; -infinity valuemov cx,mi_len

ftoa20: xchg si,di ; SI = canned stringpushdi ; DI = caller's bufferrep movsb ; copy canned stringpop si ; restore SIfstp st(0) ; discard original valuejmp ftoa8 ; go to common exit point

ftoa endp;; DIGIT: Convert low nibble of AL to ASCII digit

Computer Science, Prince of Songkhla University

24

Page 25: staff.cs.psu.ac.thstaff.cs.psu.ac.th/iew/cs344-321/chapter13.doc · Web viewB C3 ST C2 C1C0 IR PE UE OE ZE DE IE invalid operation denormalized operand zero divide overflow underflow

CS344-321 Assembly Language Programming chapter 13 page________________________________________________________________________; and store at address given by DS:DI; Call with: AL = value to be converted in bits 0-3; ES:DI = address to store ASCII character; Returns: AL = unchanged; ES:DI = address+1digit proc near ; nibble to ASCII digit

pushax ; save registerand al,0fh ; isolate nibbleadd al,'0' ; convert to ASCII charstosb ; store the characterpop ax ; restore registerret

digit endpend

Computer Science, Prince of Songkhla University

25