NASM - The Netwide Assembler
NASM Forum => Using NASM => Topic started by: puttyios on May 24, 2012, 12:59:01 AM
-
what is the relationship between the address of a varible in a elf and memory?
before a executable file is loaded into memory and after , how a varible address map to physical memory address?
-
Take for example source code such as this
mov rsi, Prompt
push rcx ; We're going to need this at end
.ShowNme mov al, cl ; Get position number
or al, 48 ; Make uppercase A - Z
mov byte [rsi + 1], al
call Console
push rsi
mov rsi, [rbx+rcx] ; get pointer to name
call Console
pop rsi
loop .ShowNme
After assembling nasm -f elf64 you get this result in the object file
32: 48 be 00 00 00 00 00 movabs rsi,0x0
39: 00 00 00
3c: 51 push rcx
3d: 88 c8 mov al,cl
3f: 0c 30 or al,0x30
41: 88 46 01 mov BYTE PTR [rsi+0x1],al
44: e8 00 00 00 00 call 49 <ParseCL.ShowNme+0xc>
49: 56 push rsi
4a: 48 8b 34 0b mov rsi,QWORD PTR [rbx+rcx*1]
4e: e8 00 00 00 00 call 53 <ParseCL.ShowNme+0x16>
53: 5e pop rsi
54: e2 e7 loop 3d <ParseCL.ShowNme>
Notice how the code in purple doesn't make any sense and it won't until linked with all other components and then
400212: 48 be 50 12 60 00 00 movabs rsi,0x601250
400219: 00 00 00
40021c: 51 push rcx
40021d: 88 c8 mov al,cl
40021f: 0c 30 or al,0x30
400221: 88 46 01 mov BYTE PTR [rsi+0x1],al
400224: e8 97 fe ff ff call 4000c0 <Console>
400229: 56 push rsi
40022a: 48 8b 34 0b mov rsi,QWORD PTR [rbx+rcx*1]
40022e: e8 8d fe ff ff call 4000c0 <Console>
400233: 5e pop rsi
400234: e2 e7 loop 40021d <ParseCL.ShowNme>
This is where the linker has resolved it is in virtual memory. Where it actually reside in physical memory is really
not that important and the OS looks after that.
-
In fact, when an operating system implements virtual memory, you can not even determine ahead of time where in physical memory your code will be located even after it begins to run. The contents of memory can be shuffled to and from disk as needed when task switches occur, and it is possible for code to wind up at different PHYSICAL memory locations at different times. The VIRTUAL memory addresses however, remain the same. In effect, we often pretend that our program is the only non system code present.