Bad Instruction - Inline Assembly Language in C Code

perror undefined variable un

I'm trying to exit a program with assembly instructions, but when I compile with gcc it says that mov is a bad instruction, even when I use movl which I don't even know what it is. Is it even possible to exit a program with assembly instructions?

int main(void)
{
    __asm__("movl %rax, $60\n\t"
        "movl %rdi, $0\n\t"
        "syscall\n");
}
// cc main.c -o main && ./main
Joshua

You need movq for 64 bit. Also, your operations are not in the correct order.

The following compiles:

int main(void)
{
    __asm__("movq $60, %rax\n\t"
        "movq $0, %rdi\n\t"
        "syscall\n");
}

Note that for any other system call (which doesn't terminate the whole program), it's necessary to tell the compiler which registers are clobbered, and usually to use a "memory" clobber to make sure memory is in sync with C values before a system call reads or writes memory.

Also, to pass operands, you'll need Extended asm syntax. See How to invoke a system call via sysenter in inline assembly? for an example my_write wrapper. (Which has only "syscall" inside the asm template; we ask the compiler to put the call number and args in the right registers instead of writing mov)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Assembly Language implementation of C instruction

From Dev

multiplication instruction error in inline assembly

From Dev

LDSET ARM instruction as inline assembly

From Dev

Converting from C Loop to MIPS assembly language (bad address error)

From Dev

Calculating cost of an instruction in assembly language

From Dev

Converting C code to MIPS Assembly Language

From Dev

Inline assembly in C code using TI code composer studio (for ARM)

From Dev

Inline Assembly in C

From Dev

Instruction Encoding relating to MARIE Assembly language

From Dev

Assembly Language, what exactly is a specialized instruction?

From Dev

Instruction Encoding relating to MARIE Assembly language

From Dev

A strcpy gcc inline assembly code

From Dev

Inline assembly code, what is ebp?

From Dev

What assembly language does C code compile into in Visual Studio?

From Dev

map c language into assembly language

From Dev

Assembly language with C++

From Dev

Assembly Language to C equivalent

From Dev

PCLMULQDQ instruction in C inline asm

From Dev

Converting Visual C/C++ inline assembly code to GCC equivalent code

From Dev

C51 C compiler inline assembly to SDCC inline assembly

From Dev

Converting Assembly Language to Python Code

From Dev

Inline assembly to bytes in C++

From Dev

Inline assembly in C not working properly

From Dev

Comparing numbers in inline c assembly

From Dev

Using FPU with C inline assembly

From Dev

Inline assembly in C not working properly

From Dev

Inline assembly instruction on ARM64 (iOS) fails

From Dev

Assembly language with C or C++

From Dev

What kind of C++ code would generate this x86 assembly instruction?

Related Related

HotTag

Archive