referencing C structure elements in assembly

This is a discussion on referencing C structure elements in assembly within the C Programming forums, part of the General Programming Boards category; I have an array of structure defined in C language. And I want to copy one of system register onto ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    8

    Question referencing C structure elements in assembly

    I have an array of structure defined in C language. And I want to copy one of system register onto structure element.

    I am writing the assembly code, using __asm block in C itself. Since, do not know the syntax of structre of in Assmebly. For example, I want to move content of DS register to structure element.

    So can anybody tell me, how to write code for that.
    I tried
    __asm mov structurename[0].member,DS
    but did not work.

    Otherwise, how to write same code in C?

    I am using VC++ 6.0 compiler.

    Faster responce is appreciated.
    Thanks,
    Al

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Is your member the correct size? This compiled -

    Code:
    #include <stdio.h>
    
    typedef struct atag
    {
    	short b;
    } a;
    
    
    int main(int argc, char* argv[]) 
    
    {
    	a c[5];
    	__asm mov c[0].b,DS
    	printf("%d",c[0].b);
    	return 0; 
    }

  3. #3
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    zen's code won't work either...

    The problem is that there is no equivalent of
    > mov [memorylocation], AX
    for segment registers. You can only move segment registers to/from general registers. Just do someting like
    > mov AX, DS
    > mov [memorylocation], AX
    instead.

    greetinx,
    alex

  4. #4
    Registered User spliff's Avatar
    Join Date
    Aug 2001
    Posts
    14
    [B]Is your member the correct size? -





    Bit of a personal question this,isn't it ???????????????


    may the source be with you...

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007

    Talking

    lol

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    8
    I tried moving to general purpose register, even that gives problem..

    And size is proper, It is unsigned short which is 16 bit. When It is word(32bit) compiler shouts.

    My statement is like this
    mov ax, state[state_ptr].stack_segment
    .. and when I compile using VC 6.0. the error is

    error: C2424 : '[' : improper operation in second operand

    please advise..
    Thanks..
    Al
    Last edited by BigAl; 09-10-2001 at 02:02 PM.

  7. #7
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Try to move the value to a simple variable first (in C code).

    state[state_ptr].stack_segment requires some calculation to find the address of the variable: you want it to address byte state + state_ptr*sizeof(state) + offsetoff(stack_segment). I think this is illegal in inline asm.

    alex
    Last edited by alex; 09-10-2001 at 02:20 PM.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    8
    Alex repsonce helped me and works for tesing purpose.. Thanks..

    Since this code is running in interrupt handler, I do not have luxury of time. So I have to come up with some alternative method for optimization, like building it in MASM.

    Any new ideas are most welcome.

    Also, I have question, Can I call C functions from assembly module, if yes how?

    Thanks,
    Al

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 02:34 PM
  2. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 05:17 AM
  3. structure ...solution plz????
    By hegdeshashi in forum C Programming
    Replies: 4
    Last Post: 07-24-2006, 09:57 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. referencing structure variables
    By sballew in forum C Programming
    Replies: 1
    Last Post: 11-01-2001, 12:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21