Porting Inline GASM to x64 Error accessing access to MASM

I am currently porting some code to MS Windows x64 from the https://github.com/mono project , which was written for GCC Linux, and I am having some problems.

Currently, I'm not sure if my transfer from x64 AT&T embedded ASM to x64 MASM is correct. It compiles fine, but my test case fails because it memcpythrows memory access exceptions / violations after executing my ASM function. Is my translation correct?

One of the things that I was really set on was the fact that it was ripnot available on Windows x64 MASM? I really don't know how to translate the remaining AT & T syntax lines (see below). But I gave him a better try. Did I handle the lack of access correctly rip?

If my job is right, then why memcpynot?

Here is the related C ++:

void mono_context_get_current(MonoContext cnt); //declare the ASM func

//Pass the static struct pointer to the ASM function mono_context_get_current
//The purpose here is to clobber it
#ifdef _MSC_VER
#define MONO_CONTEXT_GET_CURRENT(ctx) do { \
    mono_context_get_current(ctx);  \
    } while (0)
#endif

static MonoContext cur_thread_ctx = {0};

MONO_CONTEXT_GET_CURRENT (cur_thread_ctx);
memcpy (&info->ctx, &cur_thread_ctx, sizeof (MonoContext)); //memcpy throws Exception.

Here is the current ASM function.

mono_context_get_current PROTO
.code
mono_context_get_current PROC
mov rax, rcx ;Assume that rcx contains the pointer being passed
mov [rax+00h], rax
mov [rax+08h], rbx
mov [rax+10h], rcx
mov [rax+18h], rdx ;purpose is to offset from my understanding of the GCC assembly
mov [rax+20h], rbp
mov [rax+28h], rsp
mov [rax+30h], rsi
mov [rax+38h], rdi
mov [rax+40h], r8
mov [rax+48h], r9
mov [rax+50h], r10
mov [rax+58h], r11
mov [rax+60h], r12
mov [rax+68h], r13
mov [rax+70h], r14
mov [rax+78h], r15
call $ + 5
mov rdx, [rax+80h]
pop rdx
mono_context_get_current ENDP
END

As far as I understand, the register should contain a pointer to the structure and what should I use - . rcxrdxpop

As I mentioned, I have GCC ASM for platforms other than Win64 that seem to work on those platforms. Here is what this code looks like:

#define MONO_CONTEXT_GET_CURRENT(ctx)        \
        __asm__ __volatile__(        \
                "movq $0x0, 0x00(%0)\n"        \
                "movq %%rbx, 0x08(%0)\n"        \
                "movq %%rcx, 0x10(%0)\n"        \
                "movq %%rdx, 0x18(%0)\n"        \
                "movq %%rbp, 0x20(%0)\n"        \
                "movq %%rsp, 0x28(%0)\n"        \
                "movq %%rsi, 0x30(%0)\n"        \
                "movq %%rdi, 0x38(%0)\n"        \
                "movq %%r8, 0x40(%0)\n"        \
                "movq %%r9, 0x48(%0)\n"        \
                "movq %%r10, 0x50(%0)\n"        \
                "movq %%r11, 0x58(%0)\n"        \
                "movq %%r12, 0x60(%0)\n"        \
                "movq %%r13, 0x68(%0)\n"        \
                "movq %%r14, 0x70(%0)\n"        \
                "movq %%r15, 0x78(%0)\n"        \
                "leaq (%%rip), %%rdx\n"        \
                "movq %%rdx, 0x80(%0)\n"        \
                :         \
                : "a" (&(ctx))        \
                : "rdx", "memory")

Thanks for any help you can offer! I will be the first to admit that my build is pretty rusty.

+4
2

gcc asm (gcc MASM):

gcc -S -masm=intel myfile.c
0

, -, :

movq $0x0, 0x00(%0)

, rax , .

leaq (%%rip), %%rdx

intel synatx:

lea rdx, [rip]

, 64- .

att:

call $ + 5
mov rdx, [rax+80h] ; looks reversed
pop rdx

:

mov qword ptr [rcx], 0
mov [rcx + 0x08], rbx
mov [rcx + 0x10], rax 
mov [rcx + 0x18], rdx 
mov [rcx + 0x20], rbp 
mov [rcx + 0x28], rsp 
mov [rcx + 0x30], rsi 
mov [rcx + 0x38], rdi 
mov [rcx + 0x40], r8
mov [rcx + 0x48], r9
mov [rcx + 0x50], r10
mov [rcx + 0x58], r11
mov [rcx + 0x60], r12
mov [rcx + 0x68], r13
mov [rcx + 0x70], r14
mov [rcx + 0x78], r15
lea rdx, [rip]
mov [rcx + 0x80], rdx
mov rdx, [rcx + 0x18]  ; restore old rdx since it on clobber list

, rcx rax mov. , rax rcx . , .

- , .

0

All Articles