首页 > 解决方案 > 如何设置进位标志添加两个数字(32位)

问题描述

我使用 read_int 得到了两个数字,并添加了两个数字。最后我检查了 EFLAGS (dump_regs)。

所以,要设置进位标志,我尝试了“4,294,967,295 + 1”,但是没有设置进位标志。('CF'没有显示在屏幕上)

如果我想设置进位标志,我需要什么数字?

    call read_int
    mov ebx, eax

    call read_int
    mov ecx, eax

    mov edx, ebx             ; add the two numbers, edx = ebx - ecx
    add edx, ecx

    mov eax, edx
    call print_int
    call print_nl
    dump_regs 1

我输入了 4294967295 和 1

标签: assemblyx86carryflag

解决方案


如果您运行以下代码,您可以说服自己设置了进位标志:

call read_int    ;Input say 150
mov ebx, eax
call read_int    ;Input say 180

add al, bl       ;This produces a carry because 150+180=330 DOESN'T FIT the 8-bit register AL

setc al          ;AL becomes 1
movzx eax, al    ;EAX becomes 1
call print_int   ;Show it

使用不产生进位的数字进行验证:

call read_int    ;Input say 80
mov ebx, eax
call read_int    ;Input say 125

add al, bl       ;This produces NO carry because 80+125=205 DOES FIT the 8-bit register AL

setc al          ;AL becomes 0
movzx eax, al    ;EAX becomes 0
call print_int   ;Show it

推荐阅读