Page 1 of 1
Does using extern in assembly declare a pointer?
Posted: Sat Jun 01, 2013 1:16 am
by gsingh2011
In
this tutorial, there is some assembly code that looks like this:
Code: Select all
[global gdt_flush] ; make 'gdt_flush' accessible from C code
[extern gp] ; tells the assembler to look at C code for 'gp'
; this function does the same thing of the 'start' one, this time with
; the real GDT
gdt_flush:
lgdt [gp]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x08:flush2
If I used the extern keyword in C, I would declare the variable exactly as it was originally declared. So if it wasn't a pointer, it wouldn't be declared as a pointer when I used the extern keyword. However, in the above assembly code, gp is actually a pointer, right? Otherwise we'd be calling lgdt gp instead of lgdt [gp].
Re: Does using extern in assembly declare a pointer?
Posted: Sat Jun 01, 2013 2:15 am
by dozniak
gp is an external symbol.
assembler doesn't really care if that's a pointer or not.
assembly instruction syntax generally doesn't depend on what the variable type is - in reality, a variable type is dependent on how you use it in the instructions.
in this case gp is a pointer to gdt structure, so your assumption is correct, this time.
Re: Does using extern in assembly declare a pointer?
Posted: Sat Jun 01, 2013 4:14 am
by Combuster
Using an extern simply means that you don't have to define that particular label in the current source file. It still acts as if it were a label.
Re: Does using extern in assembly declare a pointer?
Posted: Sat Jun 01, 2013 1:07 pm
by gsingh2011
Just to clarify, so whenever I have this in my C code:
And I want to use it in my assembly so I do this in the assembly code:
Then if I use bar in my assembly code, it's actually a pointer to bar, and if I use [bar] then it's bar itself?
That would actually make sense, the only reason I wasn't completely sure was if I did this in another C file
Then bar wouldn't be a pointer to a foo_t, it would actually be a foo_t. But in assembly it would make sense that bar is a pointer.
Re: Does using extern in assembly declare a pointer?
Posted: Sat Jun 01, 2013 5:16 pm
by Combuster
Every name, variable function or otherwise in C is just a number referring to where in memory it is stored, the type you associate with it defines the amount of space that is actually reserved there. In assembly every use of a label results in it being substituted with the corresponding address. The only difference is that assembly knows no data types.
For example:
Code: Select all
mystruct_t bar;
mystruct_t * foo;
(...)
extern mystruct_t bar;
extern mystruct_t * foo;
foo = &bar;
*foo = bar;
the assembly equivalent of that would roughly be the following:
Code: Select all
; space for an entire struct
bar: times LENGTH_OF_MYSTRUCT_T DB 0
; space for just a pointer
foo: DD 0
(...)
extern bar
extern foo
; write at foo, the address of bar
mov [foo], bar
; copy to the location stored in foo, the content of bar.
; note the two memory indirections for foo and only one for bar.
mov edi, [foo]; mov esi, bar; mov ecx, LENGTH_OF_MYSTRUCT_T; rep movsb es:[edi], ds:[esi];