创建一个循环 "displays" ax 中的位。 "displays"这个词的意思是把这些位放到另一个寄存器中。另一个寄存器中的值为 0 或 1
Create a loop that "displays" the bits in ax. The word "displays" means to put the bits into another register. Values in the other register is 0 or 1
汇编语言编程
在 ax 中存储一个值。
创建一个“显示”ax 中的位的循环。
“显示”一词的意思是将位放入另一个寄存器。
另一个寄存器中的值只能为 0 或 1。
我是这样做的,但我不知道我这样做对不对。请帮忙。
.data
x:
.word 0b00101
.text
.globl _start
_start:
movw x, %ax
loop:
cmp %ax, %bx
je done
andw 0b1000000000000000, %ax
shl , %ax
jmp loop
done:
nop
Please use SHL
, and AND
instruction only
这种情况的麻烦在于我们无法知道您还可以使用哪些其他指令。您自己的示例已经使用 MOV
、CMP
、JE
、JMP
,甚至 NOP
.
因为该任务要求一个 循环恰好有 16 次迭代,所以很明显只使用 SHL
和 AND
是不可能的。我们至少需要一个条件跳转并使用一个独立的迭代计数器。
基本思想始终是 SHL
将移出的位放在进位标志中。从那里,像 ADC
这样的指令可以选择它并产生结果 BX=1
.
; IN (ax)
mov cx, 16
again:
xor bx, bx ; BX=0
shl ax, 1 ; -> CF
adc bx, bx ; BX=[0,1]
loop again
现在我将删除那些您自己没有使用的 XOR
、ADC
和 LOOP
指令。此示例仅使用 MOV
、SHL
和 JNC
:
; IN (ax)
mov cx, 1
again:
mov bx, 0
shl ax, 1
jnc cont
mov bx, 1
cont:
shl cx, 1 ; Produces CF=1 after 16 iterations
jnc again
假设 AX
中的值设置了最低位(如您的示例 0b00101),我们可以不用独立的迭代计数器。
; IN (ax)
again:
mov bx, 0
shl ax, 1
jnc cont \
mov bx, 1 | These don't change flags
cont: /
jnz again ; Still based on the flags from `shl ax, 1`
如果您在未设置最低位的 AX
值上使用此代码段,将会发生什么情况,即循环的迭代次数将少于 16 次!
如果你有至少一次使用AND
指令的冲动,你可以用它来清除BX
寄存器:
; IN (ax)
mov cx, 1
again:
and bx, 0 ; ANDing BX with zero produces BX=0
shl ax, 1 ; -> CF now has the bit that was shifted out at the high end of AX
jnc cont ; That bit was 0, so BX=0 is fine
mov bx, 1 ; Else make BX=1
cont:
shl cx, 1 ; Produces carry after 16 iterations
jnc again
在连续迭代中 CX
将保持 1、2、4、8、16、32、64、128、256、512、1024、2048、4096、8192、16384、32768、0
当值最终变为 0 时,CF 将被设置,因为将值(65536)加倍的真实结果不再适合 16 位寄存器。
汇编语言编程
在 ax 中存储一个值。 创建一个“显示”ax 中的位的循环。 “显示”一词的意思是将位放入另一个寄存器。 另一个寄存器中的值只能为 0 或 1。
我是这样做的,但我不知道我这样做对不对。请帮忙。
.data
x:
.word 0b00101
.text
.globl _start
_start:
movw x, %ax
loop:
cmp %ax, %bx
je done
andw 0b1000000000000000, %ax
shl , %ax
jmp loop
done:
nop
Please use
SHL
, andAND
instruction only
这种情况的麻烦在于我们无法知道您还可以使用哪些其他指令。您自己的示例已经使用 MOV
、CMP
、JE
、JMP
,甚至 NOP
.
因为该任务要求一个 循环恰好有 16 次迭代,所以很明显只使用 SHL
和 AND
是不可能的。我们至少需要一个条件跳转并使用一个独立的迭代计数器。
基本思想始终是 SHL
将移出的位放在进位标志中。从那里,像 ADC
这样的指令可以选择它并产生结果 BX=1
.
; IN (ax)
mov cx, 16
again:
xor bx, bx ; BX=0
shl ax, 1 ; -> CF
adc bx, bx ; BX=[0,1]
loop again
现在我将删除那些您自己没有使用的 XOR
、ADC
和 LOOP
指令。此示例仅使用 MOV
、SHL
和 JNC
:
; IN (ax)
mov cx, 1
again:
mov bx, 0
shl ax, 1
jnc cont
mov bx, 1
cont:
shl cx, 1 ; Produces CF=1 after 16 iterations
jnc again
假设 AX
中的值设置了最低位(如您的示例 0b00101),我们可以不用独立的迭代计数器。
; IN (ax)
again:
mov bx, 0
shl ax, 1
jnc cont \
mov bx, 1 | These don't change flags
cont: /
jnz again ; Still based on the flags from `shl ax, 1`
如果您在未设置最低位的 AX
值上使用此代码段,将会发生什么情况,即循环的迭代次数将少于 16 次!
如果你有至少一次使用AND
指令的冲动,你可以用它来清除BX
寄存器:
; IN (ax)
mov cx, 1
again:
and bx, 0 ; ANDing BX with zero produces BX=0
shl ax, 1 ; -> CF now has the bit that was shifted out at the high end of AX
jnc cont ; That bit was 0, so BX=0 is fine
mov bx, 1 ; Else make BX=1
cont:
shl cx, 1 ; Produces carry after 16 iterations
jnc again
在连续迭代中 CX
将保持 1、2、4、8、16、32、64、128、256、512、1024、2048、4096、8192、16384、32768、0
当值最终变为 0 时,CF 将被设置,因为将值(65536)加倍的真实结果不再适合 16 位寄存器。