Author Topic: Simple Windows64 console program crashes  (Read 4745 times)

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Simple Windows64 console program crashes
« on: May 02, 2013, 06:49:43 AM »
I'm trying to port some of Paul Carter's example program for my students into the Win64 world. But the following simple test program crashes.

The simple C frame:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

extern unsigned long long int assembly(void);

int main(int argc, char *argv[])
{
    unsigned long long int retval;
    retval = assembly();
    return retval;
}

That's the simple assembly language source which should print a zero terminated C string via libc:
Code: [Select]
        [BITS 64]                        ; 64 bit segment

        extern     printf

        global     assembly

        section    .data

msg1               db 10, "That is a C string (zero terminated).", 10, 0
str_format         db "%s", 0            ; string format 

        section    .text

assembly:
        mov        rcx, str_format       ; rcx -> format string
        mov        rdx, msg1             ; rdx -> string to print       
        xor        r8, r8                ; nothing else to pass
        xor        r9, r9 
        call       printf                ; call libc
        xor        rax, rax              ; rax = function result
        ret
That's the batch file which builds the running EXE:
Code: [Select]
yasm -f win64 assembly.asm
gcc -c cskel.c
gcc -o cskel.exe cskel.o assembly.obj
NASM will do the same job. The program displays the string, but doesn't end clean.

Has anyone a clue what's wrong with the code or the link process?

Gunther
Get your facts first, and then you can distort them.

qWord

  • Member
  • *****
  • Posts: 1473
  • The base type of a type is the type itself
    • SmplMath macros
Re: Simple Windows64 console program crashes
« Reply #1 on: May 02, 2013, 07:12:01 AM »
you must take care of stack: it must be aligned by 16 and the shadow space for the register arguments must be allocated( =4*8 ).
Code: [Select]
sub rsp,(8+4*8)
;... call
add rsp,...

BTW: why not using jWasm with WinInc?
BTW2: there is no need to zero unused parameters.
MREAL macros - when you need floating point arithmetic while assembling!

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: Simple Windows64 console program crashes
« Reply #2 on: May 02, 2013, 07:38:01 AM »
Hi qWord,

thank you for the fast reply.

you must take care of stack: it must be aligned by 16 and the shadow space for the register arguments must be allocated( =4*8 ).

That was my fault.

BTW: why not using jWasm with WinInc?
BTW2: there is no need to zero unused parameters.

I'll think about that.

Gunther
Get your facts first, and then you can distort them.