Created
July 23, 2020 23:31
-
-
Save six519/f68eadb880124da767b28d40cff77fea to your computer and use it in GitHub Desktop.
Terminate a process using a system call in Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;usage: ./kill2 <PID> | |
;same as `kill -n 9 <PID>` command in Linux | |
;just practicing programming in assembly language (Ferdinand Silva) | |
section .data | |
SYS_EXIT: equ 60 | |
SYS_KILL: equ 62 | |
SYS_WRITE: equ 1 | |
SIGKILL: equ 9 | |
STD_IN: equ 1 | |
INVALID: db "Usage: kill2 <PID>", 10 | |
global _start | |
section .text | |
_start: | |
pop rcx | |
cmp rcx, 2 | |
jne invalid_cmd | |
pop r14 | |
pop rsi | |
call str_to_int | |
mov r15, rax | |
call killprocess | |
jmp exit | |
;;str_to_int by: 0xAX | |
str_to_int: | |
xor rax, rax | |
mov rcx, 10 | |
next: | |
cmp [rsi], byte 0 | |
je return_str | |
mov bl, [rsi] | |
sub bl, 48 | |
mul rcx | |
add rax, rbx | |
inc rsi | |
jmp next | |
return_str: | |
ret | |
killprocess: | |
mov rax, SYS_KILL | |
mov rdi, r15 | |
mov rsi, SIGKILL | |
syscall | |
ret | |
print: | |
mov rax, SYS_WRITE | |
mov rdi, STD_IN | |
mov rsi, r13 | |
mov rdx, 20 | |
syscall | |
ret | |
invalid_cmd: | |
mov r13, INVALID | |
call print | |
exit: | |
mov rax, SYS_EXIT | |
mov rdi, 0 | |
syscall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment