execute binary machine code from C

The code must be in a page with execute permission. By default, stack and read-write static data (like non-const globals) are in pages mapped without exec permission, for security reasons. The simplest way is to compile with gcc -z execstack, which links your program such that stack and global variables (static storage) get mapped in … Read more

Successive sys_write syscalls not working as expected, NASM bug on OS X?

NASM 2.11.08 and 2.13.02+ have bugs with macho64 output. What you are observing seems to be something I saw specifically with 2.13.02+ recently when using absolute references. The final linked program has incorrect fixups applied so the reference to str2 is incorrect. The incorrect fixup causes us to print out memory that isn’t str2. NASM … Read more

Assembly compiled executable using INT 0x80 on Ubuntu on Windows Subsystem for Linux doesn’t produce output

Related: WSL2 does allow 32-bit user-space programs, WSL1 doesn’t. See Does WSL 2 really support 32 bit program? re: making sure you’re actually using WSL2. The rest of this answer was written before WLS2 existed. The issue is with Ubuntu for Windows (Windows Subsystem for Linux version 1). It only supports the 64-bit syscall interface … Read more

RDTSCP in NASM always returns the same value (timing a single instruction)

Your first code (leading to the title question) is buggy because it overwrites the rdtsc and rdtscp results with the cpuid results in EAX,EBX,ECX and EDX. Use lfence instead of cpuid; on Intel since forever and AMD with Spectre mitigation enabled, lfence will serialize the instruction stream and thus do what you want with rdtsc. … Read more