如果我在一个已经有最大值的16位寄存器中递增一个值,会发生什么?(Assembly 8086)

时间:2018-04-13 20:35:33

标签: assembly x86-16

我已将BX寄存器的值放到FFFF并执行命令inc BX,该值变为0000。价值在哪里?我检查了旗帜,但我仍然无法提出解释。

1 个答案:

答案 0 :(得分:1)

0xFFFF + 1 = 0x10000并且您将结果存储到16位寄存器中,因此它被截断为0x0000(结果至少需要17位以避免截断)。

就像你mov bx,0x8000 add bx,0x8000时一样 - > bx == 0x0000。

标志根据英特尔手册中的定义反映了这种情况,add将相应地设置所有标志,inc保留进位标志并修改所有其他标志。

在这种零结果的情况下,两者都将设置ZF = 1,但只有add将修改CF = 1.

add/adc x,1inc xsub/sbb x,1dec x之间的这种不一致是英特尔工程师故意设计的,允许简单创建两个指令协同工作的循环, inc/dec不会破坏adc/sbb指令链,例如添加存储在内存中的任意长整数:

  ; 16 bit x86 real mode code, will do "destination += source;"
  ; ds:si = source integer, ds:di = destination integer
  ; cx = length in words of integers (bytes = 2*cx)
  ; modifies all involved registers and ax
  clc      ; clear carry flag to not affect result of addition
add_loop:
  lodsw    ; ax = another word of source integer, si+=2
  adc  [di], ax    ; destination_word += source_word + carry
  ; this did also set new carry for next word, so it must be preserved
  lea  di,[di+2]   ; advance also di by 2, with LEA to preserve carry
  dec  cx          ; test if all word have been processed (preserves carry)
  jnz  add_loop    ; but zero flag is being set by the `dec` = loop condition
  ret      ; here the long integers addition is done