首页 > 代码库 > 例6:设缓冲区DATA中有一组单字节有符号数,以0为结束标志。写一个程序实现如下功能:把前5个正数依次送入缓冲区PDATA,把前5个负数依次送入缓冲区MDATA;如正数或负数不足5个,则用0补足。

例6:设缓冲区DATA中有一组单字节有符号数,以0为结束标志。写一个程序实现如下功能:把前5个正数依次送入缓冲区PDATA,把前5个负数依次送入缓冲区MDATA;如正数或负数不足5个,则用0补足。

自解:

 1 DSEG SEGMENT
 2 DATA DB -1,1,-2,2,3,-3,-4,4,5,-5,-6,6,0
 3 PDATA DB 0,0,0,0,0
 4 MDATA DB 0,0,0,0,0
 5 DSEG ENDS
 6 ;------------------------------------
 7 CSEG SEGMENT
 8     ASSUME DS:DSEG,CS:CSEG
 9 START:
10     MOV AX,DSEG
11     MOV DS,AX
12     
13     MOV BX,OFFSET DATA
14     XOR SI,SI
15     XOR DI,DI
16     MOV AL,0
17         DEC BX
18    UP:  INC BX
19   AGAIN:CMP AL,[BX]
20     JZ OK
21         JG NEXT
22     CMP DI,5
23         JAE UP
24         INC DI
25     MOV DH,[BX]
26     MOV MDATA[DI],DH
27         INC BX
28         JMP AGAIN
29  NEXT:
30     CMP SI,5
31         JAE UP
32         INC SI
33     MOV DL,[BX]
34     MOV PDATA[SI],DL
35         INC BX
36         JMP AGAIN
37 
38 OK:     MOV AH,4CH
39     INT 21H
40 CSEG ENDS 
41     END START

例解:

  

 1 MAX_COUNT = 5
 2 DSEG SEGMENT
 3 DATA DB 3,-4,5,6,-7,8,-9,-10,-1,-32,-123,27,58,44,-12,0
 4 PDATA DB MAX_COUNT DUP(?)
 5 MPATA DB MAX_COUNT DUP(?)
 6 DSEG ENDS
 7 ;------------------------------------------------------
 8 CSEG SEGMENT
 9         ASSUME DS:DSEG,CS:CSEG
10 START:
11         MOV AX,DSEG
12         MOV DS,AX
13 
14         MOV CX,MAX_COUNT
15         MOV SI,OFFSET PDATA
16         MOV DI,OFFSET MPATA
17         MOV AL,0
18  NEXT1: MOV [SI],AL
19         MOV [DI],AL
20         INC SI
21         INC DI
22         LOOP NEXT1
23 
24         MOV BX,OFFSET DATA
25         XOR DI,DI
26         XOR SI,SI
27  NEXT2: MOV AL,[BX]
28         INC BX
29         CMP AL,0
30         JZ  OK
31         JG  PLUS
32         CMP DI,MAX_COUNT
33         JAE CONT
34         MOV MPATA[DI],AL
35         INC DI
36         JMP SHORT CONT
37  PLUS:  CMP SI,MAX_COUNT
38         JAE CONT
39         MOV PDATA[SI],AL
40         INC SI
41         JMP SHORT CONT
42  CONT:  MOV AX,SI
43         ADD AX,DI
44         CMP AX,MAX_COUNT+MAX_COUNT
45         JB  NEXT2
46    OK:  MOV AH,4CH
47         INT 21H
48 CSEG ENDS
49      END START