Created
July 20, 2025 18:48
-
-
Save rrampage/7f7001d70b32cd38d9e18145f61a9d96 to your computer and use it in GitHub Desktop.
Simple implementation of `kill -9`
This file contains hidden or 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
/* | |
A toy implementation of the kill command with a few simplifications | |
- Passing PID in argv[1] | |
- Hard code the signal number to 9 (for SIGKILL) | |
- Exit the program with the return value of the kill system call | |
Solving https://blog.codingconfessions.com/p/x86-assembly-exercise-1 , but in ARM64 assembly | |
Compiles to a 211 byte binary! | |
`as -o kill.o kill.S && ld -s -o kk kill.o` to compile. Run strip / sstrip to remove the section headers | |
*/ | |
.equ SYS_KILL, 129 | |
.equ SIGKILL, 9 | |
.equ SYS_EXIT, 93 | |
.text | |
.global _start | |
// Convert string to unsigned int. No bounds check!! | |
str_to_u: | |
mov w10, wzr | |
mov w9, #10 | |
loop: | |
ldrb w11, [x0] | |
mov w8, w10 | |
cbz w11, fin | |
sub w11, w11, #48 // sub '0' | |
madd w10, w8, w9, w11 | |
add x0, x0, #1 | |
cmp w11, #10 | |
b.lo loop | |
fin: | |
mov w0, w8 | |
ret | |
_start: | |
ldr x19, [sp] // argc | |
cmp x19, #2 | |
b.lt exit // we need at least 1 argv ... | |
ldr x0, [sp, #16] // move argv[1] to x0 | |
bl str_to_u | |
mov x8, #SYS_KILL | |
mov x1, #SIGKILL | |
svc #0 | |
exit: | |
mov x8, #SYS_EXIT | |
svc #0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment