0

Professor Mune had wirtten a compiler by ocmal ssee it on https://www.ed.tus.ac.jp/j-mune/ccp/

I had some test cases.

A test program for output integer, The source code like the following:

    {
    int a, b;
        scan  (a);
    }

The generated assembly code like the following:

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

main:

    pushq %rbp
    movq %rsp, %rbp
    subq $32, %rsp
    movq %rbp, %rax
    leaq -8(%rax), %rax
    movq %rax, %rsi
    leaq IO(%rip), %rdi
    movq $0, %rax
    callq scanf
    leaveq
    retq

Then gcc input.s a a.exe file is generated, run it in Windows console window, crashed, why?

i installed Embarcadero_Dev-Cpp_6.3_TDM-GCC_9.2_Setup on my Windows, therefore I have gcc 64-bit.

I fix the issue in code emitter, the new code like the following

$ cat -n tmp.s
 1  IO:
 2          .string "%lld"
 3          .text
 4          .globl main

 5  main:
 6          pushq %rbp
 7          movq %rsp, %rbp
 8          subq $32, %rsp
 9          movq %rbp, %rax
10          leaq -8(%rax), %rax
11          movq %rax, %rdx
12          leaq IO(%rip), %rcx
13          subq $32, %rsp
14          callq scanf
15          addq $32, %rsp
16          leaveq
17          retq
ecm
  • 2,583
  • 4
  • 21
  • 29
  • 1
    Suggest to use C as intermediate: translate your lang sample to C, manualy if need be, then compile & run the C to see if it works. If it doesn't, fix that; else compare your machine code with the working generated C machine code. – Erik Eidt Jan 13 '22 at 03:17
  • 2
    Exactly the same problem as your last question: you're using the Linux calling convention with Windows libraries. Jester already gave you the answer in comments before you posted this. – Peter Cordes Jan 13 '22 at 03:18
  • 1
    Also, `%lld` is the wrong format for an `int`, unless your language's `int` is a C `int64_t`, not a C `int` (which is `int32_t` in both mainstream x86-64 ABIs). – Peter Cordes Jan 13 '22 at 03:20

0 Answers0