Àðèôìåòè÷åñêèé ïðèìåð
Àðèôìåòè÷åñêèé ïðèìåð
×òîáû ïðîèëëþñòðèðîâàòü ôóíêöèè, êîòîðûå ìû ðàññìîòðåëè â
ïðåäûäóùèõ ðàçäåëàõ, äàâàéòå ðåøèì àðèôèåòè÷åñêóþ çàäà÷ó íà ÿçûêå
àññåìáëåðà. Ïðèìåð ïðîñò, íî èñïîëüçóåò ìíîãèå êîìàíäû. Çàäà÷à
çàêëþ÷àåòñÿ â âû÷èñëåíèè ÷àñòíîãî äâóõ àðèôìåòè÷åñêèõ âûðàæåíèé, â
êîòîðûõ íåêîòîðûå ÷èñëà ïîñòîÿííû, à äðóãèå ïåðåìåííû. Âñå ÷èñëà
ÿâëÿþòñÿ 16=áèòîâûìè öåëûìè ÷èñëàìè ñî çíàêîì.
Ôîðìóëà âû÷èñëåíèé:
A * 2 + B * C
X = ------------------
D - 3
Ýòà çàäà÷à ðåøàåòñÿ ïîäïðîãðàììîé íà ÿçûêå àññåìáëåðà,
èçîáðàæåííîé íà Ôèã. 4.16. Ïîäïðîãðàììà ñíà÷àëà âûïîëíÿåò äâà
óìíîæåíèÿ. Òàê êàê ìèêðîïðîöåññîð 8088 âñåãäà ïîìåùàåò ðåçóëüòàò
16=áèòîâîãî óìíîæåíèÿ â ïàðó ðåãèñòðîâ DX:AX, â ïðèìåðå ðåçóëüòàò
ïåðâîãî óìíîæåíèÿ ïåðåíîñèòñÿ â ïàðó ðåãèñòðîâ BX:CX ïåðåä
âûïîëíåíèåì âòîðîãî óìíîæåíèÿ. Êîãäà îáà óìíîæåíèÿ çàâåðøåíû,
ïðîãðàììà âûïîëíÿåò ñëîæåíèå ÷èñëèòåëÿ. Ïîñêîëüêó óìíîæåíèå äàåò
32=áèòîâûå ðåçóëüòàòû, â ïðîãðàììå òðåáóåòñÿ ñëîæåíèå ïîâûøåííîé
òî÷íîñòè. Ýòî ñëîæåíèå îñòàâëÿåò ðåçóëüòàò â DX:AX.  ïðèìåðå
çíàìåíàòåëü âû÷èñëÿåòñÿ â ðåãèñòðå CX, à çàòåì íà íåãî äåëèòñÿ
÷èñëèòåëü. Ïðîãðàììà çàïèñûâàåò ÷àñòíîå èç ðåãèñòðà AX â ïåðåìåííóþ
ðåçóëüòàòà X. Îñòàòîê â ýòîé çàäà÷å èãíîðèðóåòñÿ.
Microsoft (R) Macro Assembler Version 5.00 1/1/80 04:01:10
Ôèã. 4.16 Ïðèìåð àðèôìåòè÷åñêèõ âû÷èñëåíèé Page 1-1
PAGE ,132
TITLE Ôèã. 4.16 Ïðèìåð àðèôìåòè÷åñêèõ âû÷èñëåíèé
;-------------------------------------------------------------
; Ïðîèçâîäÿòñÿ âû÷èñëåíèÿ ïî ôîðìóëå
;
; A * 2 + B * C
; X = -------------------
; D - 3
;
; Âñå ïåðåìåííûå - 16-ðàçðÿäíûå öåëûå ÷èñëà ñî çíàêîì
;-------------------------------------------------------------
Ôèã. 4.16 Àðèôìåòè÷åñêèé ïðèìåð (íà÷àëî)
0000 CODE SEGMENT
ASSUME CS:CODE,DS:CODE
0000 ???? X DW ? ; Ïàìÿòü äëÿ ïåðåìåííûõ
0002 ???? A DW ?
0004 ???? B DW ?
0006 ???? C DW ?
0008 ???? D DW ?
000A FIG4_16 PROC NEAR
000A B8 0002 MOV AX, 2 ; Çàãðóçêà êîíñòàíòû
000D F7 2E 0002 R IMUL A ; DX:AX = A * 2
0011 8B DA MOV BX, DX
0013 8B C8 MOV CX, AX ; BX:CX = A * 2
0015 A1 0004 R MOV AX, B
0018 F7 2E 0006 R IMUL C ; DX:AX = B * C
001C 03 C1 ADD AX, CX
001E 13 D3 ADC DX, BX ; DX:AX = A * 2 + B * C
0020 8B 0E 0008 R MOV CX, D
0024 83 E9 03 SUB CX, 3 ; CX = D - 3
0027 F7 F9 IDIV CX ; AX = (A*2 + B*C) / (D-3)
0029 A3 0000 R MOV X, AX ; Ñîõðàíåíèå ðåçóëüòàòà
002C C3 RET
002D FIG4_16 ENDP
002D CODE ENDS
END
Ôèã. 4.16 Àðèôìåòè÷åñêèé ïðèìåð (ïðîäîëæåíèå)