简体   繁体   English

更改程序集8086中寄存器的符号位

[英]Changing the sign bit of a register in assembly 8086

I'm trying to write assembly code that does a fairly simple thing, it changes the sign bit (just the sign bit) of the register AL. 我正在尝试编写一个相当简单的汇编代码,它会更改寄存器AL的符号位(仅符号位)。

I need to find two different ways to do this, but sadly the solutions I came up with don't seem to be working. 我需要找到两种不同的方法来执行此操作,但是遗憾的是,我想出的解决方案似乎没有用。

1) I tried subtracting AL from itself twice: 1)我尝试两次从自身减去AL:

mov BL,AL
sub AL,BL
sub AL,BL

But this changes more than just the sign bit. 但这变化不只是符号位。 And I understand why. 我知道为什么。

2) I also tried adding 10000000 to AL. 2)我还尝试将10000000添加到AL。 This indeed solves the problem, but if the sign bit of AL is 1, then I get an overflow and carry. 这确实解决了问题,但是如果AL的符号位为1,那么我会溢出并进位。

Is there a way to change just the sign bit, without running into overflow / carry problems? 有没有一种方法可以只更改符号位,而不会遇到溢出/进位问题?

How about just doing: 怎么做:

XOR AL, 80H

?

also: 也:

MOV BL, AL
MOV AL, 0
SUB AL, BL
AND BL, 7FH
AND AL, BL

Another thing to preserve the flags you can always go 保留您永远可以使用的标志的另一件事

   PUSHF
       // whatever
    POPF

Shift it into the carry flag, complement the flag and shift it back: 将其移入进位标志,对标志进行补充并移回:

shl al, 1
cmc
rcr al, 1

To preserve the flags you can use pushf and popf . 要保留标志,可以使用pushfpopf

XOR operation the constant 0x8000 (for 16bit) with the register you want to change. 对要更改的寄存器进行XOR操作常数0x8000(用于16位)。

Bit of googling: 谷歌搜索:

xor Bitwise logical XOR
  Syntax:   xor dest, src
  dest: register or memory
  src: register, memory, or immediate
  Action: dest = dest ^ src
  Flags Affected: OF=0, SF, ZF, AF=?, PF, CF=0

To change sign use NEG instruction 要更改标志,请使用NEG指令

for example 例如

MOV AX,0101        - AX = 0101
NEG AX             - AX = 1010
                      because of the NEG instruction


NEG instruction affects these flags only:
    CF, ZF, SF, OF, PF, AF.

NEG - Make operand negative (two's complement).
      Actually it reverses each bit of operand and then adds 1 to
      it. For example 5 will become -5, and -2 will become 2. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM