Relocatable Functions in NASM
Posted: Mon Aug 25, 2003 11:03 am
Hi,
I have a question about relocating assembler code, if I write a function like this:
once this is assembled can I copy this code to anywhere in memory and have it function as expected or would the jumps go to the wrong place?
When I disassemble this it produces this:
as you can see the jumps have immediate memory locations, this means if I moved this code to say 0x08000 then the jumps would go to the wrong place??
can someone show me how to write relocatable functions? I did try a test but it didn't appear to work, also I don't know if I need it but would the org directive work? I would rather not need it as I want to be able to move the code in memory to any location without knowing where that location is.
thanks.
I have a question about relocating assembler code, if I write a function like this:
Code: Select all
[bits 32]
_some_func:
mov eax, 0x45
cmp eax, 0x46
je .sf_locala
mov ebx, 0x46
jmp short .sf_localb
.sf_locala:
mov ebx, 0x46
.sf_localb:
add eax, ebx
ret
When I disassemble this it produces this:
Code: Select all
00000000 B845000000 mov eax,0x45
00000005 3D46000000 cmp eax,0x46
0000000A 7407 jz 0x13
0000000C BB46000000 mov ebx,0x46
00000011 EB05 jmp short 0x18
00000013 BB46000000 mov ebx,0x46
00000018 01D8 add eax,ebx
0000001A C3 ret
can someone show me how to write relocatable functions? I did try a test but it didn't appear to work, also I don't know if I need it but would the org directive work? I would rather not need it as I want to be able to move the code in memory to any location without knowing where that location is.
thanks.