Inline ASM register usage, need help

General FreeBASIC programming questions.
Post Reply
Emulog
Posts: 24
Joined: Jan 12, 2023 0:44

Inline ASM register usage, need help

Post by Emulog »

I have a "select case as static" with 328 cases.
I have iterated many steps to apply optimization to all cases,
have reduced scope of variables to minimum, everything compiles ok and run triple fast vs initial version.
All cases perform elementary actions like var=var, or a bit more complex like this
Case 267: If X.FLG And XFN Then X.ALU=((X.ALU-(X.FLG And 1)) And &HFF) Else X.ALU=((X.ALU+(X.FLG And 1)) And &HFF) EndIf
Ive came to situation when GCC yet still use this variable scope, that belongs to jumptable, relatively, as shown below :
.L1174:
test BYTE PTR X$[rip+148], 1
jne .L1175
mov rax, QWORD PTR MAIN$[rip]
cmp DWORD PTR X$[rip+136], 0
mov rdx, QWORD PTR [rax]
mov QWORD PTR X$[rip+72], rdx
jne .L1221
A lot of indirect access, causing slowdown. I have to overhaul all this with direct register usage, using inline ASM.
There are no calls or extra jumping anywhere. This code is a part of main program body, not a sub.
So all registers, in avail to free use, will be loaded before loop, then pure asm code executes, and after loop ends, flushed back.

Will GCC keep track of my register usage, will it notice that i put to use a register and compile accordingly ?
As stated in help file "KeyPgAsm" : When an Asm block is opened, the registers ebx, esi, and edi are pushed to the stack, when the block is closed, these registers are popped back from the stack.
Really ? 6 stack operations applied to any asm block will naturally kill the gain.

PS. Once i saw a nice feature in TMT Pascal, allowing direct register usage within small code chunk or a loop, by applying definition as REGISTER to a local variable
SARG
Posts: 1756
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Inline ASM register usage, need help

Post by SARG »

The push/pop of registers are only used with gas. With gcc64 or gas64 is not done.
Did you try gas64 to see the generated asm code ?
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Inline ASM register usage, need help

Post by srvaldez »

hello Emulog :)
if you are using the gcc backend with optimization and are using Intel syntax inline asm then you may get a surprise now and then, intel syntax inline asm with the gcc backend is not reliable if you use optimization, if you use the gcc style of inline asm then it's ok because gcc understands how to optimize it without messing things up
test to see if optimization is the problem by compiling your program with -O0
Emulog
Posts: 24
Joined: Jan 12, 2023 0:44

Re: Inline ASM register usage, need help

Post by Emulog »

SARG wrote: Feb 02, 2023 11:34 The push/pop of registers are only used with gas. With gcc64 or gas64 is not done.
Did you try gas64 to see the generated asm code ?
Hello and thank you for reply. Since i compile for AMD64, all generated ASM i do check quite often, but no inlines yet ever tried.
Just need to think it over.
Emulog
Posts: 24
Joined: Jan 12, 2023 0:44

Re: Inline ASM register usage, need help

Post by Emulog »

srvaldez wrote: Feb 02, 2023 12:51 hello Emulog :)
if you are using the gcc backend with optimization and are using Intel syntax inline asm then you may get a surprise now and then, intel syntax inline asm with the gcc backend is not reliable if you use optimization, if you use the gcc style of inline asm then it's ok because gcc understands how to optimize it without messing things up
test to see if optimization is the problem by compiling your program with -O0
Hello Srvaldez, i never used another syntax before. Compile without any -O for testing is ok.
GCC have VOLATILE keyword, how i can apply it to my inline asm in Freebasic ?
Ive read that GCC have two intermediate internal formats, and that LTO/GIMPLE is better somehow.
(Ive no idea how to supply these keys to FB compiler)
I guess that any inline ASM may be transcoded too, may be Intel syntax is not that bad.
May you describe a bit more exact about intel syntax behavior ?
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Inline ASM register usage, need help

Post by srvaldez »

Emulog wrote: Feb 02, 2023 12:57 Hello and thank you for reply. Since i compile for AMD64, all generated ASM i do check quite often, but no inlines yet ever tried.
Just need to think it over.
I am confused, are you using inline asm with the gcc backend or not?
Emulog
Posts: 24
Joined: Jan 12, 2023 0:44

Re: Inline ASM register usage, need help

Post by Emulog »

For now i only check asm output, to start actual writing i need some time, a lot of years passed, i forgot a lot.
Now i need to use ASM again, in Freebasic for my hobby project :D
Neither used GCC directly, just tune the keys in IDE and hit the button
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Inline ASM register usage, need help

Post by srvaldez »

Emulog, it's best to not use inline asm unless it's just for fun
if you want to try the gcc backend with optimization, then put the following at the top of your program

Code: Select all

#cmdline "-gen gcc -O 2"
Emulog
Posts: 24
Joined: Jan 12, 2023 0:44

Re: Inline ASM register usage, need help

Post by Emulog »

it's best to not use inline asm unless it's just for fun
Now it is time to. While now i still apply corrections to z80 emulation core, all optimizations are done. Cant force GCC to better register usage.

ATT syntax is a beast, like studying japanese from scratch :mrgreen:
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Inline ASM register usage, need help

Post by srvaldez »

Emulog, just in case you don't know, if you want to use the at&t syntax for inline asm then you must tell the compiler either from the compile command or via #cmdline -asm att
Post Reply