I have this code:
#include <iostream>
int main(int x=1) {
while (x <= 1000 && std::cout << x++ << std::endl) {}
}
I wrote it in Gedit, using Debian, and compiled it with Debian's own g ++, without ever changing anything. The compiler never presented problems with other codes. But in my code the output I get is the range from 2 to 1000.
And if I compile using this site link , for example, the output is from 1 to 1000.
Something interesting is that if I change the type of validation within while
, less than or equal to 1000 to less than 1000, only, the program starts to display the number 1.
What's going on?
NOTE: The code was developed for a challenge. Therefore, there is no better way to develop the same output.
Assembly Code:
.file "tes.cpp"
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.text
.globl main
.type main, @function
main:
.LFB969:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
andl $-16, %esp
subl $16, %esp
.L4:
cmpl $1000, 8(%ebp)
jg .L2
movl 8(%ebp), %eax
addl $1, 8(%ebp)
movl %eax, 4(%esp)
movl $_ZSt4cout, (%esp)
call _ZNSolsEi
movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
movl %eax, (%esp)
call _ZNSolsEPFRSoS_E
movl (%eax), %edx
subl $12, %edx
movl (%edx), %edx
addl %edx, %eax
movl %eax, (%esp)
call _ZNKSt9basic_iosIcSt11char_traitsIcEEcvPvEv
testl %eax, %eax
je .L2
movl $1, %eax
jmp .L3
.L2:
movl $0, %eax
.L3:
testb %al, %al
jne .L4
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE969:
.size main, .-main
.type _Z41__static_initialization_and_destruction_0ii, @function
_Z41__static_initialization_and_destruction_0ii:
.LFB978:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $24, %esp
cmpl $1, 8(%ebp)
jne .L6
cmpl $65535, 12(%ebp)
jne .L6
movl $_ZStL8__ioinit, (%esp)
call _ZNSt8ios_base4InitC1Ev
movl $__dso_handle, 8(%esp)
movl $_ZStL8__ioinit, 4(%esp)
movl $_ZNSt8ios_base4InitD1Ev, (%esp)
call __cxa_atexit
.L6:
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE978:
.size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii
.type _GLOBAL__sub_I_main, @function
_GLOBAL__sub_I_main:
.LFB979:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $24, %esp
movl $65535, 4(%esp)
movl $1, (%esp)
call _Z41__static_initialization_and_destruction_0ii
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE979:
.size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main
.section .init_array,"aw"
.align 4
.long _GLOBAL__sub_I_main
.hidden __dso_handle
.ident "GCC: (Debian 4.7.2-5) 4.7.2"
.section .note.GNU-stack,"",@progbits