How can i do inline C pure Assembly language? I开发者_如何学Python wanted to do for loop but does not works.
#include <stdio.h>
int main()
{
asm
{
for(int i=0; i<10; i++)
{
// is this how i will do the assembly language as inline C?
// is this how the for loop looks as inline c?
}
} // ?
}
Something similar to,
__asm{
xor esi,esi
go:
inc esi
cmp esi,10
jnz go
}
It doesn't work because that isn't assembly. IIRC, the simplest for
-like loop in assembly is something like
.L3:
addl $1, -4(%ebp)
cmpl $9, -4(%ebp)
jle .L3
When you use asm keyword you aren't allow to write any C code. You can only use assembly language keywords and the way of writing assembly code in C depends on complier. Every compiler allow you in different way. For Example Turbo uses this syntax
void main()
{
asm
{
mov ax,4C00h
int 21h
}
}
I think what he's asking is if it's possible to write "inline C" in an assembly language program. The answer would be no, you'll have to compile your C program in to a library and call it from your assembly language program. I suppose you could also write a macro to make implementing the for loop more convienient in assembly, but that would be highly assembler dependant and you would have to let us know which you are using. I apoligize if I'm wrong, I'm just taking a shot in the dark here, your question wasn't quite clear.
Check the flow control instructions to know how to implement loops with assembly language. Check out the x86 conditional jump instructions, and the LOOPX
instructions. Have a look http://en.wikibooks.org/wiki/X86_Assembly/Control_Flow . For better and in-detail description check intel's manual.
精彩评论