does Nimrod supports inline Assembly code ?
if so , is there any example please?
thanks very much.
Edited:
I did not success to compile the example you have mentioned above.
i got this error
error:expected string literal before 'mov'
any help please?
thanks Araq,
I have enclosed the asm statment with "" but when liking got this error
undefined reference to 'a'
so who can we solve this?
Have a look at:
https://github.com/Araq/Nimrod/blob/master/lib/system/arithm.nim#L191
But as I said, the "...\n" is not necessary anymore.
Ok , i have did it , but have a hint
this works well
echo( addInt(10,5) )
Nimrod converts the previous function to that
return result;
}
look at the end of c function , all languages return the result value into register EAX , so there is no need to use the last statement " return result;" which c compiler will return the variable result to EAX register again .
i think you can do that with Pragma that will not emit result variable in c function.
oh , that is nice .
I was thinking that nostackframe pragma will produce a naked c function which has no asm stackframe.
Hi
it seems that i have a logical error. see this code
var k : int = myaddInt(10,5) echo( k )
always the result is zero when run it !!!!
proc addInt(a, b: int): int {.codegenDecl: "$# $#$# __attribute__((naked))", noStackFrame.} =
asm """ .... """
thanks for that solution , but gcc compiler only need this term __attribute__((naked)) at proto type deceleration of function.
nimrod added the term in function body also , so gcc gave error when compiling.
any help please?
Oh..... has bad news , GCC will ignore the term __attribute__((naked)) in x86 system , and supported in others.
so the last update does not make any changes .
the good news , after some searching the net , and with some trick and using previous nimrod that without last change of nostackframe , i have successfully did it.
here is the code
proc myaddInt(a, b: int): int {. importc: "myaddInt" .}
var k : int = myaddInt(10,5) echo( k )
it prints 15 , so okay but with ugly work around.
proc raiseOverflow2() {.exportc.} =
echo "OVERFLOW"
asm """
@myAddInt@8:
add %EDX, %EAX
jno 1
call @raiseOverflow2@0
1:
ret
"""
proc myAddInt(a, b: int): int {.importc, fastcall.}
echo myAddInt(34, 5666)
However it crashes at runtime and I have no idea why.
using the asm term inside function body will allow us to insert some nimrod statement , something like this vc C language
static Xvar;
void asmANDc()
mov eax , 10
mov dword ptr[Xvar] , eax
}
MessageBox(0,str(Xvar),"test",0);
}
so here we can use MessageBox c function after our asm block.
hoop this feature to be implemented with nimrod language.
proc outb1*(port , value: uint32) =
asm """
outb %%al, %%dx
:
:"a"(`port`), "d"(`value`)
"""
In the generated C code, the first : which is supposed to indicate that there is no return value to be captured gets quoted and gcc complains about it.
N_NIMCALL(void, outb1_24007)(NU32 port, NU32 value) {
nimfr("outb1", "x86asm.nim")
nimln(34, "x86asm.nim");
asm(" outb %%al, %%dx\n"
" :\n"
:"a"(port), "d"(value)
);
popFrame();
}