找出两个数组中的相同元素,并且输出
程序运行:
代码:
datas segmentA dw -1,-2, 3, 4, 5, 6, 7, 8, 9, 10,-11, 12, 13, 14, 15ACount dw ($-A)/2B dw -1, 2,-3, 4,-5, 6,-7, 8, 9,-10, 11,-12, 13,-14, 15,16,17,18,19,0BCount dw ($-B)/2C1 dw 30 dup(?)C1Count dw 0ouputlengthOfC1 db 'length of C1=$'ouputC1 db 0dh,0ah,'the same elemt(C1):$'datas endsstacks segment stackdb 100h dup(?)stacks endscodes segmentassume cs:codes,ds:datas,ss:stacks
main proc far
start:push dsmov ax,0hpush axmov ax,datas ;初始化dsmov ds,axmov cx,ACountmov bx,0 ;数组A的索引mov di,0 ;di数组C1的索引s: mov ax,A[bx] ;将数组A的元素移至axcall saveSameElemt ;保存ax与B中相同的元素add bx,type A ;数组A的索引移至下一个元素loop sshr di,1 ;di为数组C1的长度mov C1Count,di ;保存数组C1的长度lea dx,ouputlengthOfC1mov ah,9int 21hmov ax,C1Countcall unsignedPrintlea dx,ouputC1mov ah,9int 21hmov cx,C1Countmov bx,0s1:mov ax,C1[bx]call printmov dl,' 'mov ah,2int 21hadd bx,type C1 loop s1 retmain endpsaveSameElemt proc near;保存寄存器push cxmov cx,BCount mov si,0 ;数组B的索引save:cmp ax,B[si] ;比较ax与数组B的元素jne notEqual ;若不等,则跳转mov C1[di],ax ;若相当,则保存于数组C1add di,type C1 ;数组C1下标移至下一个元素notEqual:add si,type B ;数组B的下标移至下一个元素loop save;恢复寄存器pop cx ret
saveSameElemt endpprint proc near;ax=source;保存寄存器push cx push dxpush bxcmp ax,0 ; 比较ax与0大小jge notNegative ;若非0,则跳转push ax ;若为0,保存axmov dl,'-' ;输出负号mov ah,2 int 21hpop ax ;恢复axneg ax ;取ax的绝对值notNegative:mov bx,10 ;除数mov cx,0 ;计数器,记录十进制数的位数ps:mov dx,0 ;无符号扩展div bx ;除10push dx ;保存余数inc cx ;计数器加1cmp ax,0h ;比较ax与0大小jnz ps ;若不等,则跳转ps1:pop dx ;恢复余数add dl,30h ;将数字转换为数字字符mov ah,2 ;输出数字字符int 21hloop ps1 ;恢复寄存器pop bxpop dxpop cxret print endpunsignedPrint proc near;ax=source;保存寄存器push cx push dxpush bxmov bx,10 ;除数mov cx,0 ;计数器,记录十进制数的位数ps:mov dx,0 ;无符号扩展div bx ;除10push dx ;保存余数inc cx ;计数器加1cmp ax,0h ;比较ax与0大小jnz ps ;若不等,则跳转ps1:pop dx ;恢复余数add dl,30h ;将数字转换为数字字符mov ah,2 ;输出数字字符int 21hloop ps1 ;恢复寄存器pop bxpop dxpop cxret unsignedPrint endpcodes endsend main