zoukankan      html  css  js  c++  java
  • 汇编语言数组操作

    删除

    待删除元素在AX中

    数组元素无序,在附加段,首地址为DI,第一个元素为数组长度,比如:

    1 cnt dw 6
    2 arr dw 1, 2, 3, 4, 5, 6
    3 ......
    4 lea di, cnt
     1 del_elem    proc    near
     2         cld                        ; 正向扫描(DF=0)
     3         push    di
     4         mov        cx, es:[di]       ; 数组长度
     5         add        di, 2             ; 现在DI指向第一个元素
     6         repne    scasw               ; 串扫描(字)
     7         je        delete             ; 找到后ZF=0,DI指向匹配元素的下一个地址,CX为剩余元素个数
     8         pop        di                ; 未找到则退出
     9         jmp     exit
    10 delete:
    11         jcxz    dec_cnt              ; CX=0表明是最后一个元素,只要将数组长度减1即可
    12 next_elem:                           ; 将后面的元素逐个前移
    13         mov        bx, es:[di]
    14         mov        es:[di-2], bx
    15         add        di, 2
    16         loop    next_elem
    17 dec_cnt:                             ; 将数组元素个数减1
    18         pop        di
    19         dec        word ptr es:[di]
    20 exit:
    21         ret        
    22 del_elem    endp

    插入

    待插入的数为n(正数)

    数组元素递增,首地址为arr_head,尾地址为arr_tail,比如:

    1 x        dw ?    ; 占位用
    2 arr_head dw 1, 2, 3, 4, 5
    3 arr_tail dw 6
     1 ins_elem    proc    near
     2         mov ax, n
     3         mov arr_head-2, 0ffffh    ; 占位为-1,n为正数,n最小时将n放到占位的地方
     4         mov si, 0
     5 compare:
     6         cmp arr_tail[si], ax      ; 从后向前查找正确位置
     7         jle insert                ; 找到小于等于n的就插入
     8         mov bx, arr_tail[si]      ; 否则将大的元素后移
     9         mov arr_tail[si+2], bx
    10         sub si, 2
    11         jmp compare
    12 insert:
    13         mov arr_tail[si+2], ax
    14         ret
    15 ins_elem    endp

    排序

    两种冒泡排序,第二种加入了标记,效率高一些

    递减排序

    数组首地址为arr,n个元素,比如:

    1 arr dw n dup(?)
     1 arr_sort    proc    near
     2         mov cx, n
     3         dec cx                ; 外循环n-1趟
     4 loop1:                        ; 外循环,循环变量保存在di
     5         mov di, cx        
     6         mov bx, 0
     7 loop2:                        ; 内循环,循环变量就是CX
     8         mov ax, arr[bx]
     9         cmp ax, arr[bx+2]     ; 比较相邻元素
    10         jge continue          ; 符合递减顺序
    11         xchg ax, arr[bx+2]    ; 不递减则交换
    12         mov arr[bx], ax
    13 continue:
    14         add bx, 2             ; 内循环
    15         loop loop2
    16         mov cx, di            ; 外循环
    17         loop loop1
    18         ret
    19 arr_sort    endp

    递增排序

    数组在附加段中,DI保存数组首地址,第一个元素为数组长度,比如:

    1 cnt dw 6
    2     dw 3, 5, 2, 4, 1, 6
    3 ......
    4 lea di cnt
     1 arr_sort    proc    near
     2         mov arr, di            ; arr为数据段定义的变量,保存数组首地址
     3         mov cx, es:[di]
     4         mov cnt, cx            ; 数组元素个数
     5 init:
     6         mov bx, 1              ; 用作这一趟是否进行了交换的标记
     7         dec cnt                ; 外循环cnt-1趟
     8         jz sorted
     9         mov cx, cnt            ; 内循环
    10         mov di, arr    
    11 next:
    12         add di, 2            
    13         mov ax, es:[di]
    14         cmp es:[di+2], ax
    15         jae continue           ; 符合递增顺序
    16         xchg es:[di+2], ax
    17         mov es:[di], ax
    18         sub bx, bx             ; 交换标记
    19 continue:
    20         loop next
    21         cmp bx, 0
    22         je init                ; 没有交换过就表示已经排好序了
    23 sorted:
    24         mov di, arr
    25         ret
    26 arr_sort    endp
  • 相关阅读:
    leetcode 18 4Sum
    leetcode 71 Simplify Path
    leetcode 10 Regular Expression Matching
    leetcode 30 Substring with Concatenation of All Words
    leetcode 355 Design Twitte
    leetcode LRU Cache
    leetcode 3Sum
    leetcode Letter Combinations of a Phone Number
    leetcode Remove Nth Node From End of List
    leetcode Valid Parentheses
  • 原文地址:https://www.cnblogs.com/yl-xy/p/14217955.html
Copyright © 2011-2022 走看看