Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6079317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:55:58+00:00 2026-05-23T10:55:58+00:00

I normally don’t spend much time reading assembly, so the following compiler output confused

  • 0

I normally don’t spend much time reading assembly, so the following compiler output confused me a little.

Say I compile this piece of C code on my Intel Core 2 Duo running OSX 10.6:

while (var != 69) // var is a global variable
{
    printf("Looping!\n");
}

The assembly for the “var != 69” comparison looks like:

cmpl    $69, _var(%rip)

I understand that it effectively means to compare the value “69” against the contents of the global variable “var”, but I’m having a tough time understanding the “_var(%rip)” part. Normally, I expect there to be a offset value, like for referring to local variables in the stack (eg: -4($ebp)). However, I don’t quite following how offsetting the instruction pointer with the “_var” declaration will give me the contents of the global variable “var”.

What exactly does that line mean?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:55:58+00:00Added an answer on May 23, 2026 at 10:55 am

    This works very nearly the same as addressing local variables in the stack with offset(%ebp). In this case, the linker will set the offset field of that instruction to the difference between the address of var, and the value that %rip will have when that instruction executes. (If I remember correctly, that value is the address of the next instruction, because %rip always points to the instruction after the one currently executing.) The addition thus gives the address of var.

    Why do it this way? This is a hallmark of position-independent code. If the compiler had generated

    cmpl $69, _var
    

    and the linker had filled in the absolute address of var, then when you ran the program, the executable image would always have to be loaded into memory at one specific address, so that all the variables had the absolute addresses that the code expects. By doing it this way, the only thing that has to be fixed is the distance between the code and the data; the code plus data (i.e. the complete executable image) can be loaded at any address and it’ll still work.

    … Why bother? Why is it bad to have to load an executable at one specific address? It isn’t, necessarily. Shared libraries have to be position-independent, because otherwise you might have two libraries that wanted to be loaded at overlapping addresses and you couldn’t use both of them in the same program. (Some systems have dealt with this by keeping a global registry of all libraries and the space they require, but obviously this does not scale.) Making executables position-independent is largely done as a security measure: it’s somewhat harder to exploit a buffer overflow if you don’t know where the program’s code is in memory (this is called address space layout randomization).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is normally rather stupid, but don't shoot me before reading the
I normally have several problems with how cron executes scripts as they normally don't
Normally, you don't actually edit a file in vim. If you run vim foo
I don't normally deal in databases (I've managed a few small ones for web
I don't normally make this sort of question / answer, but figured I'd do
I don't normally go to stackoverflow for sigsegv errors, but I have done all
I want to access certain form elements from classes that normally don't have access
The author of this article states that Normally you don't want to have access
I hope you guys can help me, normally I don't ask for help, but
I don't normally work with binary files so I apologize if the answer to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.