×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Storing contents of a register into variable of high-level language

Storing contents of a register into variable of high-level language

Storing contents of a register into variable of high-level language

(OP)
Is it possible to do the following in say, MIPS or X86 Assembly:

Say I'm coding in a high-level language like java or c, and i've declared a temporary integer variable i. I want to set
i to be equal to the value contained in some machine register, say $r1, whatever the value is. (I don't know what it is.) Is it possible to write a function that copies the contents of $r1 into i?

Randy

RE: Storing contents of a register into variable of high-level language

Not being an expert on Assembly, but could you perhaps copy the data into a specific area of memory, and then use the high level language's memory related functions to grab that specific piece of memory ? Eg. VB can use the Windows API's CopyMemory for things like that etc.

RE: Storing contents of a register into variable of high-level language

(OP)
That's an interesting suggestion, though what I'm wondering is whether grabbing the data from the register can itself be done in the high level language. Since registers are in fixed locations, I'm wondering if there are specific addresses just to access the register file. The high level
function could then be passed these addresses as params and
grab the data directly from the registers, without having to wait for the data to be loaded into memory first.
Randy61

RE: Storing contents of a register into variable of high-level language

I really dont think a high level language is going to be able to do that. I know the likes of Java and VB dont.

Anyway, the time it would take to copy a value from a register into main memory would be tiny.

RE: Storing contents of a register into variable of high-level language

I can speak only for C++ and Pascal.
  These two languages use the stack to pass variables.  Pascal pushes the variables in the order that they are declared, C++ reverses this.  The thing you can't forget is that the calling function also pushes the the return address onto the stack after pushing any parameters, offset for a near call, seg then offset for a far call.
   Now, in Pascal Terms, To pass a VAR parameter( in other words, you want to maintain any changes to the variables made by the called function) the calling funcion passes the address of the variable on the stack.  To pass just a parameter,(all changes are lost once the called function exits) The exact value of the data is pushed onto the stack, with the exception of string data. String Data is copied to a new memory location and then that address is passed on the stack.  I'm not as sure about C++, but I beleive the conventions are similar.  Pascal functions return values in the registers.  A byte sized data value is returned in al, word sized  in AX, Dword in AX:DX, Reals are Passed in AX:DX:BX. Strings are passed by pointer in AX:DX, as are any structures larger than word
  This is why you see a lot of this code:

  PUSH BP    ;save callers BP
  MOV BP,SI  ;Set BP to Current Stack Frame
  ...
  POP BP     ;restore Callers BP
  RET

This allows you to access the passed parameters on the stack like so:
  ...
  MOV AX,[bp-4]  ;Access the data pushed last by calling function for a far call
  ...
 
Hope this helps.

RE: Storing contents of a register into variable of high-level language

You definitely cannot do that in JAVA.  JAVA is not supposed to get tied down to any architecture.

Nice post Prattaratt.

Anyway, there are several ways for HLL's to pass arguments, called C, Pascal/Fortran, fastcall, and one more I forgot.  I have details for C and Pascal/Fortran, if anyone's interested.

"Information has a tendency to be free.  Which means someone will always tell you something you don't want to know."

RE: Storing contents of a register into variable of high-level language

Greetinx!

Following example demonstrates using assembly instructions in c++ program:

void Test()
{
  int i = 1, j = 2;
  asm {
    mov eax,i
    mov ebx,j
    add eax,ebx
    mov i,eax
    }
  j = i;
}


Note: It is need under 32-bit applications to use 32-bit registers (eax,ebx et.c.), under 16-bit applications(simple DOS) you shold use 16-bit regiters (ax,bx et.c.)

Happy programming!))

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close