0

Fro run gcc s2.asm in windows7 console window; then a exe file is generated. run a.exe,then crash, why.

s2.asm code is generated from source code following:

{
int m;
    m = 1;
    iprint (m) ;
    

}

s2.asm eplease refer to the following:

IO:
.string "%lld"
.text
.globl main

main:

pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
pushq $1
movq %rbp, %rax
leaq -8(%rax), %rax
popq (%rax)
movq %rbp, %rax
leaq -8(%rax), %rax
movq (%rax), %rax
pushq %rax
popq  %rsi
leaq IO(%rip), %rdi
movq $0, %rax
callq printf
leaveq
retq

I installed tdm64-gcc-10.3.0-21.exe on my windows, therefore I have a gcc 64 bits.

But why a.exe crashed? thank you.

hi all, thank your reply and ... I am a fan of compiler technology, I want to realize my toy compiler by my self, it's very sample but support 64bits, which can be compiled by gcc on windows OS,and run on dindows console. I meet a compiler which written by ocaml by Mune Professor on site , the generated assembly seems very sample.

ocaml64 is setup on my PC. but only a ocaml compiler in it, there is no gcc. then but by the toy compiler, 80x86 assembly code can be generated,

To convert assembly code to execute file, then Embarcadero_Dev-Cpp_6.3_TDM-GCC_9.2 is setup, then gcc tmp.s, a a.exe file is generated, but a.exe cannot be run successfully on windows.

the code is provided on site.1 But I have limited knowledge on assembly. On this site, the assembly code emmiter module: for linux, for cygwin, for old ocaml.

At last I have to reconsider the code again: I select cygwin emitter. then generate assembly like the folowwing, I run the output a.exe file final succcesslly .

IO:
.string "%lld"
.text
.globl main

main:

pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
pushq $1
movq %rbp, %rax
leaq -8(%rax), %rax
popq (%rax)
movq %rbp, %rax
leaq -8(%rax), %rax
movq (%rax), %rax
pushq %rax
popq  %rdx
leaq IO(%rip), %rcx
subq $32, %rsp
callq printf
addq $32, %rsp          
leaveq
retq

$ ./a.exe

1

Note: the above assembly was not optimized.

After optimization, the code become the following:

IO:

.string "%lld"
.text
.globl main

main:

pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
pushq $1
movq %rbp, %rax
leaq -8(%rax), %rax
popq (%rax)
movq %rbp, %rax
leaq -8(%rax), %rax
movq (%rax), %rax
movq %rax,  %rdx
leaq IO(%rip), %rcx
subq $32, %rsp
callq printf
addq $32, %rsp
leaveq
retq

I found the two rows

pushq %rax
popq  %rdx

become one row.

movq rax, rdx

Here, the issue was resolved, it's caused by my mistake, since I am not really clear about the assembly code emitter module, Thank all of you.

https://www.ed.tus.ac.jp/j-mune/ccp/

  • 3
    You used the sysv calling convention instead of the microsoft one. – Jester Jan 13 '22 at 02:07
  • 1
    What is all that `popq (%rax)` nonsense after copying the frame pointer? Is this intentionally obfuscated code? You know you can just `movq $1, -8(%rbp)` like a normal person, right? (Or like a compiler would with optimization disabled so it won't do constant-propagation to `mov $1, %esi`.) `pushq` supports the same imm32 value-range as `movq`, so you're not avoiding special cases where your compiler might need to use `movabs`. If you're going to use push at all, use it to allocate + initialize space for `m` in one instruction. – Peter Cordes Jan 13 '22 at 02:44
  • Hello all, who can please modify to Microsoft convention, thank you. I really don't know. – Zhang Jason Jan 13 '22 at 04:05
  • the assebly code likes for linux, – Zhang Jason Jan 13 '22 at 09:56
  • Please see my reply and explaination, – Zhang Jason Jan 13 '22 at 13:44
  • [How to write hello world in assembler under Windows?](https://stackoverflow.com/q/1023593) has some x86-64 answers. Use the right registers, and allocate shadow space. Your "optimized" version is still extremely un-optimized, e.g. allocating shadow space around one function call, and still using that crazy mov/lea/push instead of `mov -8(%rbp), %rdx` like you'd expect from `gcc -O0`, let alone `mov $1, %edx` like you'd expect with any level of optimization enabled for a C compiler that does constant-propagation through `m=1` / `iprint(m)`. – Peter Cordes Jan 13 '22 at 21:16

0 Answers0