Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

randy61

Technical User
Jun 30, 2004
2
0
0
US
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
 
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.
 
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
 
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.
 
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.
 
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."
 
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!))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top