程序集 (x86):十六进制数的二进制代码中“01”的重复次数
Assembly (x86): Number of '01' repetitions in binary code of HEX number
我是 Assembly 的新手,我的任务是确定 16b 十六进制数的二进制代码中 01 的重复次数。例如:
编号AB34为:1010 1011 0011 0100
由于上面加粗的是01,下面的代码中需要将它们统计到RESULT中。诀窍是只使用逻辑指令,例如AND、NOT、OR、XOR、移动(SHL、SHR)、旋转(ROR/RCR、ROL/RCL)和逻辑比较。
这是我的资料:
name program
data segment
BR dw 0AB34H ;
RESULT db 0
data ends
code segment
assume CS:code, DS:data
start:
mov ax, data
mov ds, ax
mov ax, BR
mov cx, 16
mov bx, 0
lwhile:
; something with logical instructions
loop lwhile
mov RESULT, bl
mov ah, 4ch
int 21h
code ends
end start
我假设在循环内部需要完成一些工作,通过像 bx 这样的寄存器进行计数,然后移至 RESULT。
我愿意接受任何建议,在此先感谢。
您应该充分利用 and
、cmp
和 shr
指令。查找并试用它们。
总体而言,您的代码将具有如下结构:
- 将
BR
值复制到工作寄存器(例如、bx
)
- 检查 01 的最低两位,如果是,则递增计数器
- 工作寄存器右移一位
- 转到 2 [执行此操作 15 次,因为
BR
中有 16 位并且您正在检查位对]
我给你留下了一些细节。第 2 步中的部分代码可能如下所示:
mov ax, bx ; Current shifted value of BR
and ax, 03h ; Mask out (zero) all but the lowest two bits
cmp ax, 1 ; Check if the lower two bits are 01
jne next_bits ; Jump if nonzero (no, they are not 01)
; increment your counter here
next_bits:
...
cmp
会检查当前已经下移到低位两位的位对是否为01
。要检查下一对位,请使用 shr
指令。您的循环将继续移动一位,直到您用完工作寄存器中的所有位。
我是 Assembly 的新手,我的任务是确定 16b 十六进制数的二进制代码中 01 的重复次数。例如:
编号AB34为:1010 1011 0011 0100
由于上面加粗的是01,下面的代码中需要将它们统计到RESULT中。诀窍是只使用逻辑指令,例如AND、NOT、OR、XOR、移动(SHL、SHR)、旋转(ROR/RCR、ROL/RCL)和逻辑比较。
这是我的资料:
name program
data segment
BR dw 0AB34H ;
RESULT db 0
data ends
code segment
assume CS:code, DS:data
start:
mov ax, data
mov ds, ax
mov ax, BR
mov cx, 16
mov bx, 0
lwhile:
; something with logical instructions
loop lwhile
mov RESULT, bl
mov ah, 4ch
int 21h
code ends
end start
我假设在循环内部需要完成一些工作,通过像 bx 这样的寄存器进行计数,然后移至 RESULT。
我愿意接受任何建议,在此先感谢。
您应该充分利用 and
、cmp
和 shr
指令。查找并试用它们。
总体而言,您的代码将具有如下结构:
- 将
BR
值复制到工作寄存器(例如、bx
) - 检查 01 的最低两位,如果是,则递增计数器
- 工作寄存器右移一位
- 转到 2 [执行此操作 15 次,因为
BR
中有 16 位并且您正在检查位对]
我给你留下了一些细节。第 2 步中的部分代码可能如下所示:
mov ax, bx ; Current shifted value of BR
and ax, 03h ; Mask out (zero) all but the lowest two bits
cmp ax, 1 ; Check if the lower two bits are 01
jne next_bits ; Jump if nonzero (no, they are not 01)
; increment your counter here
next_bits:
...
cmp
会检查当前已经下移到低位两位的位对是否为01
。要检查下一对位,请使用 shr
指令。您的循环将继续移动一位,直到您用完工作寄存器中的所有位。