how to assemble and link:
nasm -f elf32 -o <filename>.o <filename>.asm
ld -m elf_i386 -o <filename> <filename>.o
template code (hello world):
section .text
global _start
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
int main(){ | |
// use current time to seed the prng used by rand() | |
// if srand is not called (default: srand(1)), rand() will always generate the same sequence | |
srand(time(0)); | |
for(int i=0; i<3; i++) | |
printf("%d\n", rand()); |
// how to compile: gcc dynamic_loading.c -o dynamic_loading -ldl | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <dlfcn.h> | |
#include <string.h> | |
int main(int argc, char **argv) { | |
void *handle; | |
void (*go)(char *); | |
Definition: assembly routines written as an inline function. Allows one to access architecture dependent instructions not available in C.
Form: must be inside of a function beginning with either 'asm' or '__asm__'
int main(){
// volatile keyword tells the compiler to not optimize this area of code. The compiled binary will contain this exact sequence of code
// optional: output operands, input operands, or clobbered registers
__asm__ __volatile__ (
<assembler template>
;