Jan Turoň
Jan Turoň

Reputation: 32912

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?

Upvotes: 0

Views: 96

Answers (1)

gsg
gsg

Reputation: 9377

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.

Upvotes: 3

Related Questions