NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: Zasti on November 11, 2011, 11:54:29 AM

Title: How to accessing items of an c - structure using nasm code in shared libraries
Post by: Zasti on November 11, 2011, 11:54:29 AM
Hello,

operating system : openSUSE 11.4 (32 bit)
file system:           ELF

I have an single tset.so file which contains two object files. One of these object files was successfully generated
from c-sourcecode using gcc. The other object file was successfully generated from an assembler sourcecode,
which was generated using nasm (latest version). These two object files are linked together using

ld -shared  -o test.so cobject.o asmobject.so.

Now, I have an c-structure defined and an instance of this structure inside the c-sourcecode.
So, an implemented c-function calls an function, which is placed inside the assemblercode,
As the argument, the assembler function takes the address of the c-structure, because
I have to access the members of the c-structure using this pointer.

I read the nasm documentation trying to find information for this example but I couldn't find
an detailed description.

No I will place the example code for better understanding:

Code: [Select]
//************************************************************************************************
// Start c - code

typedef struct t1
{
int int1;
int int2;
int int3;

  struct
{
int s0[2];
int s1[10];
int s2;
} b[2];

int int4;
int int5;
int int6;
};

struct t1 my_struct;

extern int asm_function(struct t1* pt1);

void initialize_members_of_structure(struct t1* pt1);
 
void caller_function_c_code(void
{
int result = 0;

initialize_members_of_structure(&my_struct)

result = asm_function(&my_struct);

return;
}

void initialize_members_of_structure(struct t1* pt1)
{

// Initializes the members of the structure referenced by pt1
// The values of the members inside the structure are not of
// special interest for this example

// so we only have to return
return;
}

// End c - code
//************************************************************************************************

;*************************************************************************************************
; Start of assembler code

cpu 586

bits 32

global asm_function:function ; declare it as a function
extern _GLOBAL_OFFSET_TABLE_


section .bss

; some uninitialized data

bss_pointer_2_structure: resd1


section .data

; some initialized data


teststr: db "Hello world !"
db 10, 0

section .text


;*********************************
; store GOT to EBX
;*********************************
%macro get_GOT_EBX 0

call %%getgotebx

%%getgotebx:

pop ebx

add ebx, _GLOBAL_OFFSET_TABLE_+$$-%%getgotebx wrt ..gotpc

%endmacro


asm_function:

push ebp   ; Save ebp

mov ebp, dword esp   ; esp to ebp

add ebp, dword 8   ; ebp points to the one and only parameter

get_GOT_EBX   ; store GOT to ebx

mov eax, dword [dword ebp]   ; Adress of the the c - structure to eax

mov [dword ebx+bss_pointer_2_structure wrt ..gotoff], eax ; Store Adress of the the c - structure to bss_pointer_2_structure

;???????????????????????????????????????????????????????????????????????????????????????????????????????????????
; Now, how can I access the data members of th c - structure using the address stored in bss_pointer_2_structure
; I want to write a value in each member of the c - structure
; How do I have to do this ?
;???????????????????????????????????????????????????????????????????????????????????????????????????????????????


;********************************************************
; asm Code should be placed here
;********************************************************






;********************************************************
; function exit

xor eax, eax ; return code is zero

pop dword ebp

; the c - function should clear the stack frame

ret

; End of assembler code
;*************************************************************************************************

Thank you for help !
Title: Re: How to accessing items of an c - structure using nasm code in shared librari
Post by: Frank Kotler on November 12, 2011, 09:29:30 AM
Wow!

I'm not sure I've got this right, and I'm not sure how to test it. Perhaps something like this?

Code: [Select]
;???????????????????????????????????????????????????????????????????????????????????????????????????????????????
; Now, how can I access the data members of th c - structure using the address stored in bss_pointer_2_structure
; I want to write a value in each member of the c - structure
; How do I have to do this ?
;???????????????????????????????????????????????????????????????????????????????????????????????????????????????

struc b
.s0 resd 2
.s1 resd 10
.s2 resd 1
endstruc

struc t1
.int1 resd 1
.int2 resd 1
.int3 resd 1
.b1 resb b_size
.b2 resb b_size
.int4 resd 1
.int5 resd 1
.int6 resd 1
endstruc



;********************************************************
; asm Code should be placed here
;********************************************************

mov dword [eax + t1.int1], 1
mov dword [eax + t1.int2], 2
mov dword [eax + t1.int3], 3
mov dword [eax + t1.b1 + b.s2], 42
; etc
mov dword [eax + t1.int4], 4
mov dword [eax + t1.int5], 5
mov dword [eax + t1.int6], 6


Nasm's native "struc" utility doesn't handle nested structures very well. That's how I'd do it, I think. It may be that the NASMX structures would do it more neatly. I'm not very familiar with 'em.

Best,
Frank