溢出时跳转(装配)

问题描述 投票:1回答:2

在这段代码中,我试图在32位整数溢出时打印1,如果不溢出则打0。但是不管它是否溢出,我都会一直收到0。这是我的代码,其中有一个应该溢出并打印的示例1.感谢任何帮助。

    .section .data
    x:
        .int 4100000000          
    y:
        .int 400000000

.section .text
.global _start

_start:

    movl y, %eax            #moves the value y to %eax
    addl x, %eax            #adds the value x to %eax

    jo output_with_overflow #jump if overflow

    movl $0, %eax
    jmp end

output_with_overflow:
    movl $1, %eax
    jmp end

end:

    movl %eax, %ebx
    movl $1, %eax
    int $0x80
assembly x86 overflow att
2个回答
1
投票

请注意,溢出标志用于签名算术。你的操作数,当被认为是签名时,不会溢出,因为4100000000-194967296并且添加400000000不会溢出。如果您想要无符号溢出,请测试进位标志。如果要签名溢出,请使用不同的数字,接近32位的最大有符号数,即2^31-1=2147483647


0
投票

看起来这是一个linux sys_exit系统调用与eax=1ebx=<exit code> ...

您的程序检查溢出标志,但对于无符号值,应检查进位。

我也简化了一点,以避免不必要的移动 - 你可以在检查后准备qazxsw poo中的退出代码。

ebx

结果在进位标志中有一个技巧:

    .section .data
    x:
        .int 4100000000
    y:
        .int 400000000

.section .text
.global _start

_start:
    movl y, %eax #moves the value y to %eax
    addl x, %eax #adds the value x to %eax

    jc output_with_overflow #jump if carry

    movl $0, %ebx
    jmp end

output_with_overflow:
    movl $1, %ebx
    jmp end

end:
    movl $1, %eax
    int $0x80
© www.soinside.com 2019 - 2024. All rights reserved.