简体   繁体   中英

Working with 64bit numbers under 32bit WinXP

I am new to assembly and I wonder why this function for adding two 64bit ints doesn't calculate properly:

add.asm

bits 32
section .text
global _add64
_add64: ; adding a and b
    enter 0,0
    mov eax,[ebp+8]  ; la
    mov edx,[ebp+12] ; ha
    add eax,[ebp+16] ; la+= lb
    adc edx,[ebp+20] ; ha+= hb with cf
    leave
ret

I use NASM compiler under WinXP 32bit, compiled as

nasm -f win32 add.asm

used together with c program

add64.c

#include <stdio.h>

long long add64(long long a, long long b);

void main() {
    printf("%Ld",add64(100000000000LL,100000000000LL));
}

compiled together under gcc as

gcc add64.obj add64.c -o add64.exe

the result is -1863462912

How so and what to do to get the expected 200000000000 ?

The assembly function looks OK to me. I strongly suspect the %Ld specifier is wrong. -1863462912 is the low 32 bits of 200000000000.

Try %lld .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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