kohlrak

asm.S (GTx0 32bit x86 sample)

Oct 12th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .intel_syntax noprefix //Use Intel syntax, which is more readable, instead of that ridiculous GAS syntax that is the default
  2. .code32 //32bit code/ABI
  3.  
  4. .global asm_caller
  5. .global asm_data //Tell assembler to export these symbols referencing their addresses.
  6. .global asm_func
  7.  
  8. .section .text //Code must be in a .text section, why "text" is beyond me.
  9.  
  10. asm_func:
  11.         mov eax, [esp+4] //ESP holds return address, +4 holds first param, +8 holds second param, etc
  12.         shl eax, 2 //Assembly trick to multiply by 4
  13.         ret //EAX holds return value.
  14.  
  15. asm_caller:
  16.         mov eax, [esp+4]
  17.         mov ebx, [esp+8]
  18.         push ebx
  19.         push eax //Pushing is in reverse order in the ABI
  20.         call _Z7cppfuncii
  21.         add esp, 8 //ABI makes it our job to clean up the mess on the callstack.
  22.         ret //EAX still holds previous return value
  23.  
  24. .section .data //Must be in a .data section if you want it to be writable without throwing a segfault/access exception.
  25.  
  26. asm_data: .long 0xdeaddead //.long is for long int (4 bytes), as opposed to short int (2 bytes) since "int" was never properly standardized.
Advertisement
Add Comment
Please, Sign In to add comment