彙編練手題集二

來源:互聯網
上載者:User

7.將al中的第7位和第0位交換,第6位和第1位交換,第5位和第2位交換,第4位和第3位交換。
提示:其實就是將al中的各位元位逆序排列。

完整程式如下:

data segment
        array db 8 dup(0)
data ends

code segment
        assume cs:code,ds:data
main:
        mov ax,data
        mov ds,ax
       
        lea si,array
        mov bl,10000000B        ;初始測試位
        mov al,10110101B        ;設定al的值[可自己設定]

        mov cx,8
s1:     test al,bl
        jz next
        inc byte ptr [si]    ;相應位如果不為0,則置1
next:   shr bl,1
        inc si
        loop s1
          ;第一個迴圈體為得到逆序的[自左至右]al中各位元位的值

        lea si,array+1        ;第0位不用移位操作
        shl byte ptr [si],1    ;第1位左移1位
        inc si
        mov bl,2
        mov cx,6
s2:     push cx
        mov cl,bl
        shl byte ptr [si],cl
        inc si
        inc bl
        pop cx
        loop s2
          ;第二個迴圈體為第x位則左移x位,以實現逆序排列
       
        lea si,array
        mov al,0
        mov cx,8
s3:     or al,[si]    ;迴圈'或'操作,實現單位元組資料的位逆序排列
        inc si
        loop s3

        mov ah,4ch
        int 21h

code ends
end main

 

============================================
8.已知記憶體source_dat單元開始連續存放若干個字型單中繼資料,資料個數在counts單元中存放.編製程式求這些資料中出現次數最多的資料,將出現次數及資料分別存入mostTimes_data+2和mostTimes_data單元.

完整程式如下:

data segment
        mostTimes_data dw ?  ;存放出現次數最多的資料
                       db ?  ;存放該資料出現的次數
        source_dat dw 5,2,1,5,2,10,2
        counts db ($-source_dat)/2 dup (?,?,0)
                ;分配"($-source_dat)/2"個3個位元組的記憶體單元
                     ;每個資料的第3個記憶體單元存放其出現次數
data ends

code segment
        assume cs:code,ds:data
main:
        mov ax,data
        mov ds,ax

        lea si,source_dat
        lea bx,counts
        lea di,counts
       
        mov ax,[si]
        mov [di],ax
        inc byte ptr [di+2]
        add di,3
          ;以上四句將第一個資料及其出現次數存放在目的記憶體區
        add si,2 ;從第2個資料開始比較
       
        mov cx,offset counts-offset source_dat
        shr cx,1  ;因為一個資料佔2個位元組,所以除以2
        dec cx ;減1的目的是因為從第2個資料開始比較
        push cx  ;將cx進棧
        jcxz finish   ;如果只有1個資料則直接跳至標號finish處
       
s2:     push cx
        mov cx,di
        sub cx,bx
        mov al,cl
        mov cl,3
        div cl ;因為一個資料佔3個位元組[包括其出現次數],所以除以3
        mov cl,al
        s1: ;將從源記憶體區取出的資料與目的記憶體區中存在的資料進行逐個比較
                mov ax,[si]
                cmp ax,[bx]
                je equal ;等於則在將已知資料出現的次數加1
                add bx,3
        loop s1
        mov [di],ax
        inc byte ptr [di+2]
        add di,3
        jmp short nextdata
         ;以上四句是將一個資料存放在目的記憶體區中的一個新的位置中
equal:  inc byte ptr [bx+2]

nextdata:
        lea bx,counts
        add si,2
        pop cx
loop s2

finish: lea di,mostTimes_data
        lea si,counts

        mov ax,[si]
        mov [di],ax
        mov al,[si+2]
        mov [di+2],al
          ;以上四句為存放第一個資料及其出現次數[佔3位元組]
          ;至目的記憶體區[此位置存放出現次數最多的資料及其次數]
        add si,3

        pop cx  ;取出一開始進棧的值並存入cx中,以確實比較次數
lp:     mov al,[si+2]
        cmp al,[di+2]
        jle next      ;小於或等於則直接用下一個資料出現的次數進行比較
        mov [di+2],al
        mov ax,[si]   ;大於則將此時取出的資料取代存放在mostTimes_data的資料,
        mov [di],ax     ;以及取代相應出現的次數
next:   add si,3
        loop lp
       
quit:   mov ah,4ch
        int 21h

code ends
end main

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.