From: ovek AT arcticnet DOT no (Ove Kaaven) Newsgroups: comp.os.msdos.djgpp Subject: Re: Weirdness: inline asm and inlining differences Date: Sun, 26 Oct 1997 03:26:47 GMT Organization: Vplan Programvare AS Lines: 91 Message-ID: <62vfn3$tlb$1@troll.powertech.no> References: NNTP-Posting-Host: alwayscold.darkness.arcticnet.no To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk ggerard AT larissa DOT cs DOT trinity DOT edu (Gregory Gerard) wrote: >typedef struct >{ > uint16 limit __attribute__ ((packed)); > uint32 offset __attribute__ ((packed)); >} mem48, *mem48Ptr; >mem48 theIDT = {0x3412, 0xefbeadde}; >__inline__ void lidt (mem48Ptr m); >inline void >lidt (mem48Ptr the) >{ > __asm__ __volatile__ ("lidt %0" : : "m" (the)); >} Here you define lidt to take the structure as a *value*... >and now the reference to it: >void >theRef(void) >{ > lidt (&theIDT); > __asm__ __volatile__ ("lidt %0" : : "m" (theIDT)); > __asm__ __volatile__ ("lidt %0" : : "m" (theIDT)); > __asm__ __volatile__ ("jmp . + 1024" : :); >} ...but you call it with the structure by *reference*... >produces assembly: >theRef: > pushl %ebp > movl %esp,%ebp > subl $4,%esp > movl $theIDT,-4(%ebp) >#APP > lidt -4(%ebp) > lidt theIDT > lidt theIDT > jmp . + 1024 >#NO_APP > leave > ret ...which, of course, gives incorrect code. Didn't you use -Wall ? It should have given you a type mismatch kind of warning (incompatible pointer type, I'd guess. Didn't test). >Questions: >1) Do I want to pass the address of the idt structure I've created or the >value? I would think by value since that would seem to parallel the >straight inline asm case more closely when inline, but it does not produce >the same asm. It might not matter which one you use, but try it to be sure. But don't mix it like you did here... >2) Why did GCC copy the address to the stack and then use the -4(%ebp) to >reference it? It was clearly a global and the inlines did not require it >at all (they use the address of the global.) It copied your reference and used it as a value. Because the value was supposed to be in memory according to your inline, it allocated stack space for it. As you can see, gcc is an intelligent compiler and did whatever was necessary to do what you asked it to do, but like any other computer software, it's not psychic and cannot guess what you actually wanted it to do. >3) Does anyone have a collection of macros/functions for dealing with the >machine at this level? It's not so much a question of using asm for >efficiency, but because C cannot express the operation of the 386. All the >tutorials I found seem more interested in the standard instructions used in >a more efficient manner rather than the system level functions. I have some macros in the djgpp-OS project I was working on... though not too many interesting macros or inlines of this kind, I'd think. Once you have this working, you can easily make the rest yourself. >4) Am I nuts for trying to do this with inline functions? Should I just >use macros and be done with it? Depends. Both have their advantages. Inline functions have types and local variables and such... but using the preprocessor, you can do a few more interesting tricks.