Hello i'm struggling with a problem in college. We are learning x86 asm, and so far i have taken many hours to understand properly the code. I'm trying to add a+b -c +d in a signed fashion and returning as a 64bit value (long long)
Problem is, the code actually works and i don't understand why it does. The following variables are declared in main.c as globals
char op8 = 0;
short op16 = 0;
int op32a=0,op32b=0;
int main() {
// (op1 + op2) - op3 + op4
op8 = 127;
op16 = 30767;
//op8 + op16 = 30894
op32a = 1;
op32b = 2147483647;
//(op8 + op16 - op32a) =30893;
//30893 + 2147483647 = 2146514540
long long result = specialsum();
printf("%lld\n",result);
}
.section .data
.global op8
.global op16
.global op32a
.global op32b
.section .text
.global specialsum
specialsum:
# prologue
pushl %ebp
movl %esp,%ebp
# inicializar tudo a zero
movl $0,%eax
movl $0,%ebx
movl $0,%ecx
movl $0,%edx
# carregar os valores para os registos
mov op16,%ax
mov op8,%bl
# adicionar a eax o ebx
# A + B
add %bx,%ax
adc $0,%eax
# remover op32a a eax
# (A+B) - C
movl op32a,%ebx
subl %ebx,%eax
# (A+B-C) + D
movl op32b,%ebx
addl %ebx,%eax
jmp fim
fim:
movl %ebp,%esp
popl %ebp
# o retorno de 64 bits é
# EDX:EAX
# H:L
ret
The following code actualy produces the right output. But i though that i add adc $0,%edx in the last operation so it would return an actual 64bit number
# (A+B-C) + D
movl op32b,%ebx
addl %ebx,%eax
adc $0,%edx
I don't understand why it produces the right output even though i'm not adding the carry to edx
Can someone explain?