Skip to content

Instantly share code, notes, and snippets.

@lagagain
Last active February 14, 2020 08:30
Show Gist options
  • Save lagagain/b807bffca4858a38e44d20d71d1f9f3f to your computer and use it in GitHub Desktop.
Save lagagain/b807bffca4858a38e44d20d71d1f9f3f to your computer and use it in GitHub Desktop.
Some C(atexit and on_exit) and Assembly Examples
// about atexit and on_exit: https://www.gnu.org/software/libc/manual/html_node/Cleanups-on-Exit.html
#include <stdio.h>
void byebye();
void seeyouagain();
int main()
{
void *p = NULL;
atexit(byebye);
on_exit(seeyouagain, p);
//printf("Hello World");
return 0;
}
void byebye()
{
//printf("\nBye bye~\n");
}
void seeyouagain()
{
//printf("\nWant see you again.~\n");
}
all: main
start.o:
@echo "----------Compile ASM----------"
nasm -f elf64 start.asm -o start.o
main.o:
@echo "----------Compile C------------"
gcc -c main.c -o main.o
main: start.o main.o
@echo "----------LINK OBJ(will error)------------"
@echo "----------not stisfy----------------------"
ld main.o start.o /usr/lib/x86_64-linux-gnu/libc.a
clean:
rm -fr main.o start.o
start: start.o
ld start.o -o start
global _start
section .data
start_str: db "Start Program\n", 0
start_len: equ $-start_str
section .text
_start:
mov edx,start_len
mov ecx,start_str
mov ebx,1
mov eax,4
int 0x80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment