Find the answer to your Linux question:
Results 1 to 2 of 2
I have a simple question about relative addressing and it is, how does relative addressing work with pointers? I understand how it works with values, how it calculates the distance ...
  1. #1
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    Intel/AMD 64 bit Relative Addressing %rip question

    I have a simple question about relative addressing and it is, how does relative addressing work with pointers?

    I understand how it works with values, how it calculates the distance from the next %rip value and uses this as an offset to get the value – see code below:

    Code:
    .section .text
    	myint1: .quad 12
    	myint2: .quad 45
    
    .global myfunc
    myfunc:
    		pushq	%rbp
    		movq	%rsp, %rbp
    
    		xorq	%rax, %rax
    
    		addq	myint1(%rip), %rax
    		addq	myint2(%rip), %rax
    
    		movq	%rbp, %rsp
    		popq	%rbp
    		ret
    This example (above) will use the instruction pointer and a offset to calculate the address of the variable – and from this get the variable value and add it to the value of %rax...simple

    Now if I write a program that has to retrieve the pointer of a value I have to do certain tricks to get the address – see code below

    Code:
    .section .text
    	mydata: .ascii "this is the message to display!\n"
    	.equ mylen, . - mydata
    
    .global myfunc
    myfunc:
    
    		call	tohere
    tohere:
    		popq	%rsi
    		.equ myoff, . - mydata
    		addq	$-myoff + 0x1, %rsi
    
    		pushq	%rbp
    		movq	%rsp, %rbp
    
    		movq	$1, %rax
    		movq	$1, %rdi
    		movq	$mylen, %rdx
    		syscall
    
    		movq	%rbp, %rsp
    		popq	%rbp
    		ret
    Is this the proper method of retrieving the address of a value when you have relative addressing at your disposal?(The above code just displays the message “this is the message to display!\n”)..

    Any responses will be appreciated...Yes its the proper way...No its not...Gerard4143
    Make mine Arch Linux

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Found my own answer..use leaq with relative addressing to retrieve a pointer value....G4143

    Code:
    .section .text
    	mydata: .ascii "this is the message to display!\n"
    	.equ mylen, . - mydata
    
    .global myfunc
    myfunc:
    
    
    		pushq	%rbp
    		movq	%rsp, %rbp
    
    		movq	$1, %rax
    		movq	$1, %rdi
    		leaq	mydata(%rip), %rsi   /*retrieve the pointer for mydata*/
    		movq	$mylen, %rdx
    		syscall
    
    		movq	%rbp, %rsp
    		popq	%rbp
    		ret
    Make mine Arch Linux

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...