Does using extern in assembly declare a pointer?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
gsingh2011
Member
Member
Posts: 83
Joined: Tue Feb 03, 2009 11:37 am

Does using extern in assembly declare a pointer?

Post 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].
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Does using extern in assembly declare a pointer?

Post 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.
Learn to read.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Does using extern in assembly declare a pointer?

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
gsingh2011
Member
Member
Posts: 83
Joined: Tue Feb 03, 2009 11:37 am

Re: Does using extern in assembly declare a pointer?

Post by gsingh2011 »

Just to clarify, so whenever I have this in my C code:

Code: Select all

foo_t bar;
And I want to use it in my assembly so I do this in the assembly code:

Code: Select all

[extern bar]
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

Code: Select all

extern foo_t bar;
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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Does using extern in assembly declare a pointer?

Post 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];
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply