在数组中删除所有-1元素,然后输出删除后的长度
程序运行:
代码:
datas segmentLink dw 15 ,1,2,3,4,5,6,-9,-1,3,4,5,3,-1,-2,3ouput db 'Link length:$'
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 dx,0ffffh ;要删除的数mov cx,Link[0] ;取数组的长度mov si,2 ;数组的第0个元素mov di,-1 ;初始化移动指针的值为-1 s:mov ax,Link[si]cmp ax,dx ;判断是否为-1 jz delete ;若是-1,则跳转cmp di,-1 ;若不是-1,判断di是否有效jz next ;若无效,跳转mov Link[di],ax ;若有效,移动非-1内容至目的单元 add di,type Link ;di指向下一单元jmp next ;跳转delete:dec Link[0] ;数组的长度减1cmp di,-1 ;判断di是否有效jnz next ;若有效,跳转mov di,si ;若无效,保存第一个-1单元的偏移地址next:add si,type Link ;索引移至下一单元loop s lea dx,ouputmov ah,9int 21hmov ax,Link[0]call decimalretmain endpdecimal proc near push axpush cxpush dx push bxcmp ax,0 jge no_negativemov bx,ax mov dl,'-'mov ah,2int 21h neg bx mov ax,bx no_negative:mov cx,0 mov bx,10 de:xor dx,dx div bx push dx inc cx cmp ax,0jnz de de1:pop dx add dl,30h mov ah,2int 21hloop de1pop bxpop dx pop cx pop ax retdecimal endpcodes endsend main