this program (i.e sum_remainder) computes the sum of remainders of each element of src[i]. this way:
- sum += src[i] % divisor
this program doesn’t have any error/warning based on the debugger (F5, using visual studio), but when I debug line by line by setting a breakpoint it starts automatically from "return 0" in the main. It’s like the function doesn’t exist in memory, in fact, I’ve checked in the disassembly, and function doesn’t exist,
when I try to set a breakpoint in the first line of sum_remainder function (i.e the function definition) I read this error message (note: this error message isn’t listed in the error list):
-
the breakpoint will not currently be hit. no executable code of the debugger’s target code type is associated with this line. possible causes include: "conditional compilation, or the target architecture of this line is not supported by the current debugger code type"
int sum_remainder(int* src, int length, int* dst, int divisor) { int sum = 0; for (int i = 0; i < length; i++) { dst[i] = src[i] % divisor; sum += dst[i]; } return sum; } int main(void) { int src[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int dst[10] = { 0 }; int ret = sum_remainder(src, 10, dst, -2); return 0; }
>Solution :
You probably build with the optimizations enabled.
If you compile with -O2 or -O3 your whole program will be reduced to the return instruction as you do not use the calculated values. So you will not have code associated with this line in the executable file.
main:
xor eax, eax
ret
But even if you use it the function will not be called as compiler most likely will inline it and vectorize.
int main(void) {
int src[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int dst[10] = { 0 };
int ret = sum_remainder(src, 10, dst, -2);
printf("%d\n", ret);
return 0;
}
main:
sub rsp, 56
movdqa xmm0, XMMWORD PTR .LC0[rip]
mov rax, QWORD PTR .LC2[rip]
pxor xmm2, xmm2
movq xmm3, QWORD PTR .LC3[rip]
movdqa xmm4, xmm2
movaps XMMWORD PTR [rsp], xmm0
movdqa xmm0, XMMWORD PTR .LC1[rip]
mov QWORD PTR [rsp+32], rax
xor eax, eax
movaps XMMWORD PTR [rsp+16], xmm0
.L8:
movq xmm0, QWORD PTR [rsp+rax]
movdqa xmm1, xmm4
add rax, 8
pcmpgtd xmm1, xmm0
pand xmm1, xmm3
paddd xmm0, xmm1
pand xmm0, xmm3
psubd xmm0, xmm1
paddd xmm2, xmm0
cmp rax, 40
jne .L8
movd esi, xmm2
pshufd xmm5, xmm2, 0xe5
movd eax, xmm5
mov edi, OFFSET FLAT:.LC4
add esi, eax
xor eax, eax
call printf
xor eax, eax
add rsp, 56
ret
You need to use the value and compile using -O0 or -Og optimization options.
-Og output:
main:
sub rsp, 104
mov DWORD PTR [rsp+48], 1
mov DWORD PTR [rsp+52], 2
mov DWORD PTR [rsp+56], 3
mov DWORD PTR [rsp+60], 4
mov DWORD PTR [rsp+64], 5
mov DWORD PTR [rsp+68], 6
mov DWORD PTR [rsp+72], 7
mov DWORD PTR [rsp+76], 8
mov DWORD PTR [rsp+80], 9
mov DWORD PTR [rsp+84], 10
pxor xmm0, xmm0
movaps XMMWORD PTR [rsp], xmm0
movaps XMMWORD PTR [rsp+16], xmm0
mov QWORD PTR [rsp+32], 0
mov ecx, -2
mov rdx, rsp
mov esi, 10
lea rdi, [rsp+48]
call sum_remainder
mov esi, eax
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
mov eax, 0
add rsp, 104
ret