kohlrak icon

asm.S (GTx0 32bit x86 sample)

kohlrak | PRO | 10/12/17 05:09:13 AM UTC | 0 ⭐ | 321 👁️ | Never ⏰ | []
ASM (NASM) |

0 B

|

None

|

0 👍

/

0 👎

.intel_syntax noprefix //Use Intel syntax, which is more readable, instead of that ridiculous GAS syntax that is the default
.code32 //32bit code/ABI
 
.global asm_caller
.global asm_data //Tell assembler to export these symbols referencing their addresses.
.global asm_func
 
.section .text //Code must be in a .text section, why "text" is beyond me.
 
asm_func:
        mov eax, [esp+4] //ESP holds return address, +4 holds first param, +8 holds second param, etc
        shl eax, 2 //Assembly trick to multiply by 4
        ret //EAX holds return value.
 
asm_caller:
        mov eax, [esp+4]
        mov ebx, [esp+8]
        push ebx
        push eax //Pushing is in reverse order in the ABI
        call _Z7cppfuncii
        add esp, 8 //ABI makes it our job to clean up the mess on the callstack.
        ret //EAX still holds previous return value
 
.section .data //Must be in a .data section if you want it to be writable without throwing a segfault/access exception.
 
asm_data: .long 0xdeaddead //.long is for long int (4 bytes), as opposed to short int (2 bytes) since "int" was never properly standardized.

Comments