题目:数组A和数组B包含若千个数据(类型自己定,数组A、B可以固定死,也可以由用户输
入;以上未指定的内容根据自己能力自主选择)。编程求A∩B,AUB和A-B,分别将结果进行输出
(输出时有提示信息)。
思路:
首先显示两个数组
选择操作
A ∩ B
实现
A ∪ B
A ? B
; multi-segment executable file template.
data segment
; add your data here!
ArrayA db 21,2,43,23,5,65,76,1,55,66,'$'
ArrayB db 21,3,89,10,100,43,76,55,'$'
ArrayALength db 0;
ArrayBLength db 0;
ChoiceTips db 'ArrayA:21,2,43,23,5,65,76,1,55,66',0ah,0dh
db 'ArrayB:21,3,89,10,100,43,76,55',0ah,0dh
db 'Please choose your choice: ', 0ah,0dh
db '1. A JIAO B' , 0ah,0dh
db '2. A BING B' , 0ah,0dh
db '3. A JIAN B' , 0ah,0dh
db 'Please input your choice: $'
resultMsg db 'The Result : $'
newline db 0ah,0dh,'$'
ends
stack segment
dw 128 dup(0)
ends
code segment
printNumber PROC ; 子程序用于打印十进制数字
push ax ; 保存寄存器状态
push bx
push cx
push dx
mov bx, 10 ; BX = 10
xor cx, cx ; CX = 0,用于计数
NumLoop:
xor dx, dx ; DX = 0
div bx ; AX = AX / BX, DX = AX % BX
push dx ; 将余数压栈
inc cx ; 增加计数
test ax, ax ; 检查商是否为0
jnz NumLoop ; 如果不为0,继续循环
PrintLoop:
pop dx ; 弹出栈顶元素到 DX
add dl, '0' ; 将数字转换为字符
mov ah, 2 ; AH = 2,用于显示单个字符
int 21h ; 输出字符
loop PrintLoop ; 循环打印数字
pop dx ; 恢复寄存器状态
pop cx
pop bx
pop ax
ret
printNumber ENDP
GETAJIAOB PROC
push ax
push bx
push cx
push dx
push si
push di
lea si, ArrayA
loopInArrayA1:
mov al, [si]
cmp al, '$'
je endPro1;
lea di, ArrayB
loopInArrayB1:
mov bl, [di]
cmp bl, '$'
je notJIAOINB; 没有匹配的不输出
cmp al, bl
je JIAOINB ;匹配了进行输出
inc di;
jmp loopInArrayB1;
notJIAOINB:
inc si ;没有相交,那么就加si
jmp loopInArrayA1
JIAOINB:
and ax, 00ffh
call printNumber
mov dl, ' '
mov ah, 02h
int 21h
inc si;
jmp loopInArrayA1
endPro1:
pop di
pop si
pop dx
pop cx
pop bx
pop ax
ret
GETAJIAOB ENDP
GETABINGB PROC
push ax
push bx
push cx
push dx
push si
push di
lea si, ArrayA
printArrayAFirst:
mov al, [si]
cmp al, '$'
je toPrintArrayBSurplus
and ax, 00ffh
call printNumber
mov dl, ' '
mov ah, 02h
int 21h
inc si
jmp printArrayAFirst
toPrintArrayBSurplus:
lea si, ArrayB
loopBINGInArrayB:
mov al, [si]
cmp al, '$'
je endProc2
lea di, ArrayA
loopBINGInArrayA:
mov bl, [di]
cmp bl, '$'
je canPrintNumber ;打印第二个数组的时候,如果没有匹配到就打印,匹配到了说明已经有了就不打印了
cmp al, bl
je canNotPrintNumber
inc di;
jmp loopBINGInArrayA
canPrintNumber:
mov bl, al
xor ax, ax
mov al, bl
call printNumber
mov dl, ' '
mov ah, 02h
int 21h
inc si
jmp loopBINGInArrayB
canNotPrintNumber:
inc si
jmp loopBINGinArrayB
endProc2:
pop di
pop si
pop dx
pop cx
pop bx
pop ax
ret
GETABINGB ENDP
GETAJIANB PROC
push ax
push bx
push cx
push dx
push si
push di
lea si, ArrayA
loopJIANInArrayA:
mov al, [si]
cmp al, '$'
je endProc3
lea di, ArrayB;
loopJIANInArrayB:
mov bl, [di]
cmp bl, '$'
je canPrintJIAN ;如果在第二个数组中没有的话就可以输出啦
cmp al, bl
je canNotPrintJIAN
inc di
jmp loopJIANInArrayB;
canPrintJIAN:
mov bl, al
xor ax, ax
mov al, bl
call printNumber
mov dl, ' '
mov ah, 02h
int 21h
inc si
jmp loopJIANInArrayA
canNotPrintJIAN:
inc si
jmp loopJIANInArrayA
endProc3:
pop di
pop si
pop dx
pop cx
pop bx
pop ax
ret
GETAJIANB ENDP
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
; add your code here
lea dx, ChoiceTips
mov ah, 9
int 21h ; output string at ds:dx
; wait for any key....
mov ah, 1
int 21h
cmp al, '1'
je option1
cmp al, '2'
je option2
cmp al, '3'
je option3
jmp endProgram
option1:
lea dx, newline
mov ah,09h
int 21h
lea dx, resultMsg
mov ah, 09h
int 21h
call GETAJIAOB;
jmp endProgram
option2:
lea dx, newline
mov ah, 09h
int 21h
lea dx, resultMsg
int 21h
call GETABINGB;
jmp endProgram
option3:
lea dx, newline
mov ah, 09h
int 21h
lea dx, resultMsg
int 21h
call GETAJIANB;
jmp endProgram
endProgram:
mov ax, 4c00h ; exit to operating system.
int 21h
ends
end start ; set entry point and stop the assembler.