汇编语言实现偏移量二进制倒序并与原偏移量下数据交换

类别:编程语言 点击:0 评论:0 推荐:
.MODEL SMALL
.STACK 200H
.DATA
ERRMSG DB 'The data are insufficient,unable to change$' ;当LEN设置过大而BUF1中的数据较少时提示错误
BUF1 DB '0123456789ABCDEFGHIJKLMNOPQRSTUV',0DH,0AH,'$'  ;欲交换的数据,最大65536个
DATALEN EQU $-BUF1-3                                        ;数据长度
LEN EQU 4                                                ;数据表示位数,最大为16
.CODE
SWAP MACRO PARAMETER1,PARAMETER2                        ;两个字节的交换
        MOV AL,PARAMETER1
        MOV AH,PARAMETER2
        MOV PARAMETER2,AL
        MOV PARAMETER1,AH
ENDM
REVERSE MACRO PARAMETER1,PARAMETER2                        ;实现二进制数据倒序
;如欲交换数据长度大于65536可将对应寄存器改用32位积存器,为避免程序混乱,参数1和参数2不能为本宏中的用到的寄存器
LOCAL   LOP,OVER                                        ;为避免重复调用该宏时程序发生混乱,应将标识本地化
        PUSH CX                                                ;保存CX,因为该宏可能在一个loop循环中被调用
        MOV AX,0                                        ;AX初值赋0
        MOV DX,PARAMETER1                                ;欲倒序的数据存入DX
        MOV CX,LEN-1                                        ;
        MOV BX,1                                        ;
        SHL BX,CL                                        ;使BX中低LEN位中的最高位为1
        MOV CX,LEN                                        ;欲循环次数送入CX
LOP:    SHR DX,1                                        ;欲交换数据最低位送入CF
        JNC OVER                                        ;如果CF不为1则不对AX进行操作
        OR AX,BX                                        ;如果CF为1则设置AX中对应(逆值后)位置为1
OVER:   SHR BX,1                                        ;BX右移,准备设置下一位
        LOOP LOP
        MOV PARAMETER2,AX                                ;结果存入参数2中
        POP CX                                                ;恢复CX的值
ENDM
PUTBUF  MACRO                                                ;输出BUF1中的值
        MOV AX,@DATA
        MOV DS,AX                                        ;数据段对齐
        LEA DX,BUF1                                        ;BUF1偏移地址送入DX
        MOV AH,9
        INT 21H                                                ;开始输出
ENDM                                                        ;宏结束
MAIN    PROC FAR                                        ;主程序开始
        MOV CL,LEN                                        ;
        MOV BX,1                                        ;
        SHL BX,CL                                        ;设置BX为2的LEN次方
        CMP BX,DATALEN                                        ;比较欲交换数据个数和实际数据个数的大小
        JA  ERROR                                        ;给出的实际数据不足,跳出
        PUTBUF                                                ;输出交换前缓冲区数据
        SHR BX,1                                        ;设置循环次数为欲交换的数据个数的一半
        MOV CX,BX                                        ;循环次数次数送入CX
        MOV DI,0                                        ;设置偏移量(欲二进制逆置的数据)的初始值
LOP:    REVERSE DI,SI                                        ;对DI逆置并送入SI(注意:实际参数不可使用REVERSE宏中用到的寄存器)
        CMP DI,SI                                        ;比较DI和SI的大小,防止逆置重复
        JLE NEXT                                        ;没有重复则跳过下面代码段
        PUSH CX                                                ;保存循环寄存器
        MOV CL,LEN                                        ;欲交换数据表示位数送入CL
        MOV DX,1                                        ;DX送入值1
        SHL DX,CL                                        ;
        DEC DX                                                ;置DX中低LEN位为1
        XOR DI,DX                                        ;对DI中低LEN位取反
        XOR SI,DX                                        ;对SI中低LEN位取反
        SWAP BUF1[DI],BUF1[SI]                                ;交换该偏移位置处的数据
        XOR DI,DX                                        ;还原DI中的数据
        XOR SI,DX                                        ;还原SI中的数据
        POP CX                                                ;恢复循环寄存器
        JMP NEXT2                                        ;跳到NEXT2准备下次循环
NEXT:   JE NEXT2                                        ;如果相等则不操作,跳到NEXT2准备下次循环
        SWAP BUF1[DI],BUF1[SI]                                ;交换该偏移位置处的数据
NEXT2:  INC DI                                                ;偏移位置加1
        LOOP LOP                                        ;如果CX不等于0进入下次循环
        PUTBUF                                                ;输出BUF1中的数据
EXIT:   MOV AH,1                                        ;
        INT 21H                                                ;暂停
        MOV AX,4C00H
        INT 21H                                                ;返回操作系统
ERROR:  MOV AX,@DATA
        MOV DS,AX                                        ;数据段地址送入DS
        MOV AH,9
        LEA DX,ERRMSG
        INT 21H                                                ;输出错误信息
        JMP EXIT                                        ;跳出,按任意键返回操作系统
MAIN    ENDP                                                ;主函数结束
        END  MAIN                                          ;主程序结束
       

本文地址:http://com.8s8s.com/it/it21689.htm