Skip to content

Instantly share code, notes, and snippets.

@mb6ockatf
Last active March 11, 2025 09:59
Show Gist options
  • Save mb6ockatf/c00f38d702a8c2965482e0c7ecc074e9 to your computer and use it in GitHub Desktop.
Save mb6ockatf/c00f38d702a8c2965482e0c7ecc074e9 to your computer and use it in GitHub Desktop.
global sys_read
global sys_write
global sys_errno
section .text
generic_syscall_3:
push ebp
mov ebp, esp
push ebx
mov ebx, [ebp + 8]
mov ecx, [ebp + 12]
mov edx, [ebp + 16]
int 80h
mov edx, eax
and edx, 0fffff000h
cmp edx, 0fffff000h
jnz .okay
mov [sys_errno], eax
mov eax, -1
.okay:
pop ebx
mov esp, ebp
pop ebp
ret
sys_read:
mov eax, 3
jmp generic_syscall_3
sys_write:
mov eax, 4
jmp generic_syscall_3
section .bss
sys_errno resd 1
int sys_write (int fd, const void *buf, int size);
static const char dunno[] = "I don't know how to greet you\n";
static const char hello[] = "Hello, dear ";
static int
string_length (const char *s)
{
int i = 0;
while (s[i])
i++;
return i;
}
int
main (int argc, char **argv)
{
if (argc < 2)
{
sys_write (1, dunno, sizeof (dunno) - 1);
return 1;
}
sys_write (1, hello, sizeof (hello) - 1);
char *argv0 = argv[0];
int length = string_length (argv0);
sys_write (1, argv0, length);
sys_write (1, "\n", 1);
return 0;
}
/*
...
Program received signal SIGSEGV, Segmentation fault.
0x080490e8 in main (argc=2, argv=0x6) at greet3.c:22
22 char *argv0 = argv[0];
*/
global _start
extern main
section .text
_start:
mov ecx, [esp] ; argc in ecx
mov eax, [esp] ;
add eax, 4 ; argv in eax
push eax
push ecx
call main
add esp, 8
mov ebx, eax
mov eax, 1
int 80h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment