假设有一个字类型的数值 arry1,试编写程序统计 arry1 数值及其后若干数值,在字单元中存储时每个数据中含“0”数据位的个数,并将统计结果保存在 res1 数组中。数据段的代码定义如下:
data segment
arry1 dw 223,4037,5635,8226,11542,14430,45257,811
len equ $-arry1
res1 db len/2 dup(0)
data ends
注:可结合 shl(或 shr)指令和条件转移指令来实现对字节数据中“0”的计数。(提示:使用 shl 或 shr 指令,移出位将写入标志寄存器中的 CF 标志位,而条件转移指令中正好有根据 CF 的值进行操作跳转的指令)
源代码
assume ds:data, ss:stack, cs:code
data segment
arr1 dw 223,4037,5635,8226,11542,14430,45257,811
len equ $-arr1
res1 dd len dup(0)
data ends
stack segment
dw 128 dup(0)
stack ends
code segment
start:
;初始化段寄存器
mov ax, data
mov ds, ax
mov ax, stack
mov ss, ax
mov sp, 256
;lop1:(arr1的字数)8次
mov cx, 8
mov bx, 0 ;arr1内部字节偏移
lop1:
push cx
mov ax, 0 ;初始化ax, 用于记录arr1每个字中0的个数
;lop2: (每个字的位数)16次
mov cx, 16
jcxz next
mov dx, word ptr[arr1][bx] ;dx存当前的字
lop2:
shl dx, 1
jc notz ;非零跳到notz,之间的代码不执行
inc ax
notz:
loop lop2
mov word ptr [res1][bx], ax
add bx, 2
pop cx
loop lop1
next:
mov ax, 4c00h ; exit to operating system.
int 21h
code ends
end start ; set entry point and stop the assembler.