windows - How to call C extern function and get return struct? -


i have extern function , struct defined in token.c:

#include "stdio.h"  typedef struct token {     int start;     int length; } t;  extern t get_token(int, int);  t get_token(int s, int l) {     printf("[c] new token: start [%d]  length [%d]\n\n", s, l);      t m_t = {};     m_t.start = s;     m_t.length = l;      return m_t; } 

... can call _get_token assembly , new token. in make_token.asm have following:

section .data       ; initialized data     mtkn:       db  "call: token(%d, %d)", 10, 0     mlen        db  "length: %d", 10, 0     mstt:       db  "start: %d", 10, 0      mend:       db  10, "*** end ***", 10, 0  section .text       ; code     extern _get_token      extern _printf      global _main      _main:         ;   stash base stack pointer         push    ebp         mov     ebp,        esp          mov     eax,        5         mov     ebx,        10         push    ebx                     ; length         push    eax                     ; start         call    _get_token              ; token         mov     [tkn],      eax          add     esp,        8          ;   test token properties         push    dword [tkn]         push    mstt         call    _printf          push    dword [tkn + 4]         push    mlen         call    _printf          add     esp,        16          .end:         push    dword   mend         call    _printf         ;   restore base stack pointer         mov     esp,        ebp         pop     ebp  section .bss        ; uninitialized data     tkn:        resd        1 

the output is:

[c] new token: start [5] length [10]

start: 5
length: 0

what missing both start , length? output verifies extern function in c getting called , values pushed function.

i believe problem lies in .bss section:

section .bss        ; uninitialized data     tkn:        resd        1 

here, set aside single dword (one integer's worth of memory) of memory token. however, in c code, define token struct having 2 ints (start , length), or 2 dwords worth of memory. means able write part of token struct (start), , member length treated non-existent. problem can solved defining tkn as

    tkn:        resd        2 

or

    tkn:        resq        1     ;; 1 qword == 2 dwords 

hope helps ;)


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -