News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Simple Windows64 console program crashes

Started by Gunther, May 02, 2013, 06:49:43 AM

Previous topic - Next topic

Gunther

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:

#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:

        [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:

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
You have to know the facts before you can distort them.

qWord

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 ).
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

Hi qWord,

thank you for the fast reply.

Quote from: qWord 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 ).

That was my fault.

Quote from: qWord on May 02, 2013, 07:12:01 AM
BTW: why not using jWasm with WinInc?
BTW2: there is no need to zero unused parameters.

I'll think about that.

Gunther
You have to know the facts before you can distort them.