Author Topic: How NASM compute the address of one varible?  (Read 12932 times)

Offline puttyios

  • Jr. Member
  • *
  • Posts: 26
How NASM compute the address of one varible?
« on: May 23, 2012, 02:32:07 PM »
How NASM compute the address of one varible?
Why can not I find this topic from the document of the NASM?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How NASM compute the address of one varible?
« Reply #1 on: May 23, 2012, 04:25:48 PM »
In the case of flat binary output, it's file offset plus origin. In linkable formats, it's up to the linker - all Nasm knows is offset into section. How does it know the offset? It counts bytes... so you don't have to.

What would you expect the Manual to say about this?

Best,
Frank


Offline codeFoil

  • Jr. Member
  • *
  • Posts: 13
Re: How NASM compute the address of one varible?
« Reply #2 on: May 23, 2012, 09:37:44 PM »
Why can not I find this topic from the document of the NASM?
Because assembly language does not really provide an equivalent what you would call a variable in a higher level language. (Well, MASM and a mode of TASM did, but it made the syntax very difficult).  In an assembly source, all of your symbols represent numbers known at assembly time.  It is up to the programmer to implement variables.

Expanding on Mr. Kotler's explanation, Nasm maintains a byte counter for each section.  As it assembles a line of the source code, it updates the byte counter by the size of the machine instruction or data that results from that line.  When you define a label, Nasm equates that label with the value of the byte counter.  When you use that label in an instruction, Nasm simply uses the value associated with that label, and if necessary, builds a table that tells a linker which parts of your object code need to be adjusted when it puts all the different pieces together. (In flat binary output, this is not needed. There is only one section which won't be combined with anything else).

Code: [Select]
section .data                            ;counter for data is 0

firstDWORD dd 20                         ;symbol firstDWORD = 0,
                                         ;counter is increased by 4
firstBYTE  db 1                          ;symbol firstBYTE  = 4,
                                         ;location counter is increased by 1
secndBYTE  db 2                          ;symbol secndBYTE  = 5,
                                         ;location counter is increased by 1

section .text                            ;location counter for text is 0

Start:  MOV AL, firstBYTE                ;The symbol Start = 0
                                         ;location counter is updated by the
                                         ;size of machine instruction
                                         ;(perhaps 2 bytes) Now equals 2
                                         ;The instruction loads AL with the
                                         ;immediate value of the symbol
                                         ;firstBYTE which is 4
        MOV AH, [firstBYTE]              ;location counter is updated by
                                         ;perhaps 6 bytes. Now equals 8
                                         ;The instruction loads AL with the
                                         ;contents of memory at the address
                                         ;of firstBYTE relative to the
                                         ;beginning of the section data,
                                         ;which is 4.  During linking,
                                         ;this will be rewritten with
                                         ;the final location calculated
                                         ;by the linker, and it will
                                         ;be adjusted again when the OS
                                         ;loads the file into memory.
    MOV AH, [AL]                           ;Again, counter is updated (perhaps by 2)
                                         ;This instruction loads AH with the value
                                         ;stored at the memory addressed by
                                         ;the contents of the AL registers
                                         ;(in this case, address 4, which stores the byte value 1)               


In the end, it is the CPU itself which finally computes the address of the variable.
« Last Edit: May 23, 2012, 09:44:28 PM by codeFoil »

Offline puttyios

  • Jr. Member
  • *
  • Posts: 26
Re: How NASM compute the address of one varible?
« Reply #3 on: May 24, 2012, 12:44:33 AM »
Thanks!
your email? I think I have some other questions which confuse me .
« Last Edit: May 24, 2012, 01:01:14 AM by puttyios »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How NASM compute the address of one varible?
« Reply #4 on: May 24, 2012, 01:58:07 AM »
My email is fbkotler(pig's tail)myfairpoint(decimal point)net - but ask your questions here, if possible, so that others can learn. You're not the only one who is confused, trust me!

Best,
Frank