【问题标题】:What if an immediate number is out of range before signed number operations如果在有符号数操作之前立即数超出范围怎么办
【发布时间】:2019-11-03 13:13:42
【问题描述】:

我的一个家庭作业如下:

mov al,77h  
sub al,80h  
AL=_______  
CF=_______  
OF=_______  

当我第一次看到它时,我以为正数减去正数的结果不会溢出。我刚刚使 OF 等于 0。
但是我的汇编代码告诉我 OF=1。

我使用的是 MASM6.15,32 位控制台环境

这是我的代码和输出:
代码 1:

; eg000000.asm in Windows Console  
    include io32.inc  
    .data  
    .code  
start:  
        mov al,77h ;119  
        sub al,80h ;128  
        call disprf ;show those 6 flags  
        call dispbd ;binary  
        call dispcrlf  
        call disphd ;hexadecimal  
    exit 0  
    end start  

输出 1:

OF=1, SF=1, ZF=0, AF=0, PF=0, CF=1  
000000000001100111111111 1111 0111  
0019FFF7  

代码 2:

; eg000000.asm in Windows Console  
    include io32.inc  
    .data  
    .code  
start:  
        mov al,77h  
        add al,-80h ;-128  
        call disprf  
        call dispbd  
        call dispcrlf  
        call disphd  
    exit 0  
    end start

输出 2:

OF=0, SF=1, ZF=0, AF=0, PF=0, CF=0  
000000000001100111111111 1111 0111  
0019FFF7  

代码 3:

; eg0000.asm in Windows Console
    include io32.inc
    .data
    .code
start:
        mov al,77h
        sub al,7fh ;127
        call disprf
        call dispbd
        call dispcrlf
        call disphd
    exit 0
    end start

输出 3:

OF=0, SF=1, ZF=0, AF=1, PF=0, CF=1
000000000001100111111111 1111 1000
0019FFF8

在“代码 1:”中,我期望 OF=0 和 CF=1
在“代码 2:”中,我希望 CF=1
'Code 3:' 它的输出是正确的

有人能告诉我为什么会这样吗?如果在计算机完成计算之前立即数超出范围(如“代码 1:”)怎么办。我知道,无论有符号或无符号数字,计算机都不知道。

顺便说一句:我在这里的第一个问题。如果我做错了什么,如果您能指出并告诉我正确的方法,我将不胜感激。:)

【问题讨论】:

    标签: assembly x86 twos-complement immediate-operand


    【解决方案1】:

    80h 位模式的有符号 2 的补码解释是 -128。这对于sub 设置OF 的方式很重要。另见Understanding Carry vs. Overflow conditions/flags for signed vs. unsigned.

    -80h 是相同的值,将汇编成相同的机器码。

    【讨论】:

      猜你喜欢
      • 2016-11-10
      • 1970-01-01
      • 2022-01-14
      • 2023-03-27
      • 2019-10-27
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2017-05-26
      相关资源
      最近更新 更多