Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of Asm code

The following GCC inline asm is taken from LuaJit's coco library. Can someone provide a line by line explanation of what it does?

static inline void coco_switch(coco_ctx from, coco_ctx to)
{
  __asm__ __volatile__ (
    "movl $1f, (%0)\n\t" 
    "movl %%esp, 4(%0)\n\t" 
    "movl %%ebp, 8(%0)\n\t"
    "movl 8(%1), %%ebp\n\t" 
    "movl 4(%1), %%esp\n\t" 
    "jmp *(%1)\n" "1:\n"
    : "+S" (from), "+D" (to) : : "eax", "ebx", "ecx", "edx", "memory", "cc");
}

Thanks

like image 901
jameszhao00 Avatar asked Sep 03 '09 05:09

jameszhao00


People also ask

What is assembly code with example?

An assembly language statement is a line of text that translates into a single machine instruction. Assembly Language is expressed in a more human readable form than the binary instructions and names are allowed for memory locations, registers, operations etc. For example: ADD [result],[coursework],[exam]

What are the 4 parts of an assembly language statement?

Each source statement may include up to four fields: a label, an operation (instruction mnemonic or assembler directive), an operand, and a comment. The following are examples of an assembly directive and a regular machine instruction.

How does assembly language work?

Assembly language (or Assembler) is a compiled, low-level computer language. It is processor-dependent, since it basically translates the Assembler's mnemonics directly into the commands a particular CPU understands, on a one-to-one basis. These Assembler mnemonics are the instruction set for that processor.

What are the types of assembly language statements explain?

Kinds of statements in assembly language Each imperative statement typically translates into one machine instruction. The DS statement reserves areas of memory and associates names with them. The DC statement constructs memory words containing constants.


2 Answers

My ASM is a bit fuzzy about the details, but I think I can give you a general idea.

ESP: Stack pointer, EBP: Base pointer.

movl $1f, (%0)

Move address of label 1 (defined on last line) into parameter 0 (from).

movl %%esp, 4(%0)

Move the content of register ESP into (from + 4).

movl %%ebp, 8(%0)

Move the content of register EBP into (from + 8).

movl 8(%1), %%ebp

Move the content of (to + 8) into register EBP.

movl 4(%1), %%esp

Move the content of (to + 4) into register ESP.

jmp *(%1)

Jump to address contained in (to).

The "1:" is a jump label.

"+S" declares a "source" (read) parameter, "+D" a destination (write) parameter. The list of registers at the end of the statement is the "clobber" list, a list of registers possibly modified by the ASM code, so the compiler can take steps to maintain consistency (i.e., not relying on e.g. ECX still containing the same value as before).

I guess that coco_ctx means "coco context". So: The function saves the current stack frame in the "from" structure, and sets the stack frame to what's saved in the "to" structure. Basically, it jumps from the current function into another function.

like image 162
DevSolar Avatar answered Sep 20 '22 19:09

DevSolar


DevSolar has the right answer -- I'll just add that you can learn a little more about what EBP and ESP are for here.

like image 24
Crashworks Avatar answered Sep 20 '22 19:09

Crashworks