--- I wrote:
If I could do just one modification to the standard, I'd add an overflow
macro, like errno.
--- peternilsson42 replied:
The behaviour on integer overflow is undefined. Hence,
implementations already have the freedom to do precisely
that if they so choose. [That they don't is indicitative!]
And what the absence of this feature indicates?
Regarding my original problem:
I simplified the problem, compiled to assembly and modified the assembly to
get my desired result. It would be very simple to implement the right math,
that is, to consider the result of the multiplication of two integers as a long
long int, as shown below:
[EMAIL PROTECTED] ~/programming/c/problem/simple
$ cat problem.c
#include limits.h
#include stdio.h
int main (void) {
int a = USHRT_MAX;
long long int b;
b = (a*a);
printf (%d * %d = %lld ?\n, a, a, b);
return 0;
}
[EMAIL PROTECTED] ~/programming/c/problem/simple
$ cat Makefile
all : problem.exe
problem.s : problem.c
gcc -Wall -Wconversion -O0 problem.c -S
problem.exe : problem.s
gcc -Wall problem.s -o problem.exe
[EMAIL PROTECTED] ~/programming/c/problem/simple
$ cat problem.s
.file problem.c
.def___main;.scl2; .type 32; .endef
.section .rdata,dr
LC0:
.ascii %d * %d = %lld ?\12\0
.text
.globl _main
.def_main; .scl2; .type 32; .endef
_main:
pushl %ebp
movl%esp, %ebp
subl$56, %esp
andl$-16, %esp
movl$0, %eax
addl$15, %eax
addl$15, %eax
shrl$4, %eax
sall$4, %eax
movl%eax, -20(%ebp)
movl-20(%ebp), %eax
call__alloca
call___main
movl$65535, -4(%ebp)
movl-4(%ebp), %eax
imull -4(%ebp), %eax
cltd
movl%eax, -16(%ebp)
movl%edx, -12(%ebp)
movl-16(%ebp), %eax
movl-12(%ebp), %edx
movl%eax, 12(%esp)
movl%edx, 16(%esp)
movl-4(%ebp), %eax
movl%eax, 8(%esp)
movl-4(%ebp), %eax
movl%eax, 4(%esp)
movl$LC0, (%esp)
call_printf
movl$0, %eax
leave
ret
.def_printf;.scl3; .type 32; .endef
[EMAIL PROTECTED] ~/programming/c/problem/simple
$ cat pedro.diff
23a24
movl$0, %edx
26d26
cltd
[EMAIL PROTECTED] ~/programming/c/problem/simple
$ cat pedro.s
.file problem.c
.def___main;.scl2; .type 32; .endef
.section .rdata,dr
LC0:
.ascii %d * %d = %lld ?\12\0
.text
.globl _main
.def_main; .scl2; .type 32; .endef
_main:
pushl %ebp
movl%esp, %ebp
subl$56, %esp
andl$-16, %esp
movl$0, %eax
addl$15, %eax
addl$15, %eax
shrl$4, %eax
sall$4, %eax
movl%eax, -20(%ebp)
movl-20(%ebp), %eax
call__alloca
call___main
movl$65535, -4(%ebp)
movl$0, %edx
movl-4(%ebp), %eax
imull -4(%ebp), %eax
movl%eax, -16(%ebp)
movl%edx, -12(%ebp)
movl-16(%ebp), %eax
movl-12(%ebp), %edx
movl%eax, 12(%esp)
movl%edx, 16(%esp)
movl-4(%ebp), %eax
movl%eax, 8(%esp)
movl-4(%ebp), %eax
movl%eax, 4(%esp)
movl$LC0, (%esp)
call_printf
movl$0, %eax
leave
ret
.def_printf;.scl3; .type 32; .endef
[EMAIL PROTECTED] ~/programming/c/problem/simple
$ ./problem.exe
65535 * 65535 = -131071 ?
[EMAIL PROTECTED] ~/programming/c/problem/simple
$ ./pedro.exe
65535 * 65535 = 4294836225 ?